Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get error text with elm 0.19? #28

Open
PascalLeMerrer opened this issue Aug 25, 2018 · 4 comments
Open

How to get error text with elm 0.19? #28

PascalLeMerrer opened this issue Aug 25, 2018 · 4 comments

Comments

@PascalLeMerrer
Copy link

Hi

The documentation still contains:

Failure err -> text ("Error: " ++ toString err)

However toString is not available any more in elm 0.19. It was replaced with Debug.toString, String.fromInt and String.fromFloat. Debug.toString is not available outside of debug mode.

So how can I get the error now?

@tbash
Copy link
Contributor

tbash commented Sep 11, 2018

It will be up to the developer to create a custom toString for the error type being dealt with.
So if you have RemoteData Http.Error News (WebData News), you'll need to create the function: toString : Http.Error -> String.

@dawehner
Copy link

Seems like a good place for a package :)

@domenkozar
Copy link

domenkozar commented Jul 16, 2019

Something like:

module Http.Extra exposing (errorToString)


import Http exposing (Error(..))
import Json.Decode as Json

parseError : String -> Maybe String
parseError =
    Json.decodeString (Json.field "error" Json.string) >> Result.toMaybe


errorToString : Http.Error -> String
errorToString err =
    case err of
        Timeout ->
            "Timeout exceeded"

        NetworkError ->
            "Network error"

        BadStatus resp ->
            parseError resp.body
                |> Maybe.withDefault (String.fromInt resp.status.code ++ " " ++ resp.status.message)

        BadPayload text resp ->
            "Unexpected response from api: " ++ text

        BadUrl url ->
            "Malformed url: " ++ url

@andys8
Copy link

andys8 commented Aug 6, 2019

With elm/[email protected] it's kind of what @domenkozar proposed, but there are two different return types, depending on how you're going to request data.

Error and Response

And the docs contain a similar example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants