X-Git-Url: https://git.xandkar.net/?p=hope.git;a=blobdiff_plain;f=src%2Fhope_result.erl;h=dec6e799d1a97b7be5cb260224c8fbbec2b99df6;hp=7cae0a355b463c7ed143475afad234d8a893048b;hb=a80ca0b203dc3398a0117d2e1fc6ae5d77250783;hpb=532e045d6052ffcb0dc778ac013658083d1df466 diff --git a/src/hope_result.erl b/src/hope_result.erl index 7cae0a3..dec6e79 100644 --- a/src/hope_result.erl +++ b/src/hope_result.erl @@ -9,6 +9,8 @@ -export( [ return/1 , map/2 + , map_error/2 + , tag_error/2 , pipe/2 , lift_exn/1 , lift_exn/2 @@ -33,6 +35,20 @@ map({ok, X}, F) -> map({error, _}=Error, _) -> Error. +-spec map_error(t(A, B), fun((B) -> (C))) -> + t(A, C). +map_error({ok, _}=Ok, _) -> + Ok; +map_error({error, Reason}, F) -> + {error, F(Reason)}. + +-spec tag_error(t(A, Reason), Tag) -> + t(A, {Tag, Reason}). +tag_error({ok, _}=Ok, _) -> + Ok; +tag_error({error, Reason}, Tag) -> + {error, {Tag, Reason}}. + -spec pipe([F], X) -> t(Ok, Error) when X :: any() @@ -76,6 +92,6 @@ lift_exn(F, Label) when is_function(F, 1) -> try {ok, F(X)} catch Class:Reason -> - {error, {Label, {Class, Reason}}} + tag_error({error, {Class, Reason}}, Label) end end.