the-sett / elm-error-handling / ResultME

ResultME is a variation on Result, where the err is a non-empty list of errors. This is useful in situations where multiple errors can be detected in a single pass, and it is preferable to report all errors detected, and not to fail only on the first error.

Some examples; when parsing a form with multiple inputs and possibly multiple errors to report to the user; when parsing some source code which may contain multiple syntax errors.

Type and Constructors


type alias ResultME err a =
Result (List.Nonempty.Nonempty err) a

The result-possible-with-multiple-errors type.

error : err -> ResultME err a

Produces a ResultME from a single error.

errors : err -> List err -> ResultME err a

Produces a ResultME from a list of errors.

fromResult : Result err a -> ResultME err a

Turns a Result into ResultME with one error.

Mapping

map : (a -> b) -> ResultME err a -> ResultME err b

Applies a function to the result value in a ResultME, if there is one.

map2 : (a -> b -> c) -> ResultME err a -> ResultME err b -> ResultME err c

Combines 2 ResultMEs together. If any of them have errors all the errors will be gathered. Otherwise the supplied function will be used to combine the result values as Ok.

map3 : (a -> b -> c -> d) -> ResultME err a -> ResultME err b -> ResultME err c -> ResultME err d

Combines 3 ResultMEs together. If any of them have errors all the errors will be gathered. Otherwise the supplied function will be used to combine the result values as Ok.

map4 : (a -> b -> c -> d -> e) -> ResultME err a -> ResultME err b -> ResultME err c -> ResultME err d -> ResultME err e

Combines 4 ResultMEs together. If any of them have errors all the errors will be gathered. Otherwise the supplied function will be used to combine the result values as Ok.

map5 : (a -> b -> c -> d -> e -> f) -> ResultME err a -> ResultME err b -> ResultME err c -> ResultME err d -> ResultME err e -> ResultME err f

Combines 5 ResultMEs together. If any of them have errors all the errors will be gathered. Otherwise the supplied function will be used to combine the result values as Ok.

map6 : (a -> b -> c -> d -> e -> f -> g) -> ResultME err a -> ResultME err b -> ResultME err c -> ResultME err d -> ResultME err e -> ResultME err f -> ResultME err g

Combines 6 ResultMEs together. If any of them have errors all the errors will be gathered. Otherwise the supplied function will be used to combine the result values as Ok.

map7 : (a -> b -> c -> d -> e -> f -> g -> h) -> ResultME err a -> ResultME err b -> ResultME err c -> ResultME err d -> ResultME err e -> ResultME err f -> ResultME err g -> ResultME err h

Combines 7 ResultMEs together. If any of them have errors all the errors will be gathered. Otherwise the supplied function will be used to combine the result values as Ok.

andMap : ResultME e a -> ResultME e (a -> b) -> ResultME e b

Combines 2 ResultMEs together. If any of them have errors all the errors will be gathered. Otherwise the function supplied in the second one will be applied to the first, to produce the result value as Ok.

mapError : (x -> y) -> ResultME x a -> ResultME y a

Applies a function to the errors in a ResultME, if there are any.

Combining errors over collections

combineList : List (ResultME err a) -> ResultME err (List a)

Combines all errors in a List of ResultMEs. All errors will be gathered in the case where there are any errors, otherwise a List of the result values will be returned as Ok.

import ResultME

ResultME.combineList
    [ Ok "this is fine"
    , ResultME.errors "failure" [ "problem" ]
    , ResultME.error "error"
    , Ok "done"
    ]
--> ResultME.errors "failure" [ "problem", "error" ]

ResultME.combineList
    [ Ok "ok"
    , Ok "result"
    , Ok "value"
    ]
--> Ok [ "ok", "result", "value" ]

combineDict : Dict comparable (ResultME err v) -> ResultME err (Dict comparable v)

Combines all errors in a Dict of ResultMEs. All errors will be gathered in the case where there are any errors, otherwise a Dict of the result values will be returned as Ok.

import ResultME
import Dict

ResultME.combineDict
    (Dict.fromList
        [ ( "3", Ok 3 )
        , ( "-4", Ok -4 )
        , ( "20xFF", Ok 255 )

        ]
    )
--> Ok
-->     (Dict.fromList
-->         [ ( "3", 3 )
-->         , ( "2", 2 )
-->         , ( "1", 1 )
-->         ]
-->     )

ResultME.combineDict
    (Dict.fromList
        [ ( "3", Ok 3 )
        , ( "1.2", ResultME.error "can't contain '.'" )
        , ( "-4", Ok -4 )
        , ( "#:-3"
          , ResultME.errors
              "can't contain '#'"
              [ "can't contain ':'" ]
          )
        ]
    )
--> ResultME.errors
-->     "can't contain '.'"
-->     [ "can't contain '#'"
-->     , "can't contain ':'"
-->     ]

combineNonempty : List.Nonempty.Nonempty (ResultME err a) -> ResultME err (List.Nonempty.Nonempty a)

Combines all errors in a Nonempty list of ResultMEs. All errors will be gathered in the case where there are any errors, otherwise a Nonempty list of the result values will be returned as Ok.

import ResultME
import List.Nonempty

ResultME.combineNonempty
    (List.Nonempty.Nonempty
        (Ok "this is fine")
        [ ResultME.errors "failure" [ "problem" ]
        , ResultME.error "error"
        , Ok "done"
        ]
    )
--> ResultME.errors "failure" [ "bad", "problem" ]

ResultME.combineNonempty
    (List.Nonempty.Nonempty
        (Ok "ok")
        [ Ok "result"
        , Ok "value"
        ]
    )
--> Ok
-->     (List.Nonempty.Nonempty
-->         "ok"
-->         [ "result", "value" ]
-->     )

Chaining

andThen : (a -> ResultME err b) -> ResultME err a -> ResultME err b

Chain together a sequence of computations that may fail. This is identical to Result.andThen.

flatten : ResultME err (ResultME err a) -> ResultME err a

Flattens the structure if one ResultME is nested inside another.