all repos

onasty @ 327757c9371a333d3436f0a204677742a04236cf

a one-time notes service

onasty/web/src/Api.elm(view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Api exposing (HttpRequestDetails, Response(..), errorToFriendlyMessage)

import Http
import Json.Decode


type Response value
    = Loading
    | Success value
    | Failure Http.Error


type alias HttpRequestDetails msg =
    { endpoint : String
    , method : String
    , body : Http.Body
    , decoder : Json.Decode.Decoder msg
    , onHttpError : Http.Error -> msg
    }


errorToFriendlyMessage : Http.Error -> String
errorToFriendlyMessage httpError =
    case httpError of
        Http.BadUrl _ ->
            "This page requested a bad URL"

        Http.Timeout ->
            "Request took too long to respond"

        Http.NetworkError ->
            "Could not connect to the API"

        Http.BadStatus code ->
            case code of
                404 ->
                    "Not found"

                401 ->
                    "Unauthorized"

                _ ->
                    "API returned an error code"

        Http.BadBody _ ->
            "Unexpected response from API"