onasty/web/src/Api.elm (view raw)
| 1 | module Api exposing (Error(..), Response(..), errorMessage, is404) |
| 2 | |
| 3 | import Http |
| 4 | import Json.Decode |
| 5 | |
| 6 | |
| 7 | type Error |
| 8 | = HttpError |
| 9 | { message : String |
| 10 | , reason : Http.Error |
| 11 | } |
| 12 | | JsonDecodeError |
| 13 | { message : String |
| 14 | , reason : Json.Decode.Error |
| 15 | } |
| 16 | |
| 17 | |
| 18 | type Response value |
| 19 | = Loading |
| 20 | | Success value |
| 21 | | Failure Error |
| 22 | |
| 23 | |
| 24 | errorMessage : Error -> String |
| 25 | errorMessage error = |
| 26 | case error of |
| 27 | HttpError err -> |
| 28 | err.message |
| 29 | |
| 30 | JsonDecodeError err -> |
| 31 | err.message |
| 32 | |
| 33 | |
| 34 | is404 : Error -> Bool |
| 35 | is404 error = |
| 36 | case error of |
| 37 | HttpError { reason } -> |
| 38 | reason == Http.BadStatus 404 |
| 39 | |
| 40 | _ -> |
| 41 | False |