FabienHenon / jsonapi-http / Http.Error

The Http.Error module defines a new kind of http error related to json api

Definitions


type RequestError
    = HttpError Http.Error
    | JsonApiError (List JsonApi.Decode.Error)
    | CustomError String

You can have:


type alias ErrorConfig =
{ httpErrorToString : Http.Error -> String
, errorClass : Maybe String
, detailClass : Maybe String 
}

Configuration used to display a RequestError

Functions

displayError : ErrorConfig -> RequestError -> Html msg

Displays a RequestError. You must provide a configuration to be able to display correctly any kind of error.

Errors are displayed like this:

<span class="error">
    <span class "error-detail error-detail-username1">Error on username</span>
    <span class "error-detail error-detail-password2">Error on password</span>
</span>

getErrorForField : String -> List JsonApi.Decode.Error -> Maybe String

Returns the jsonapi error message for the given field name

Let's say you have the following json api error:

{
   "errors":[
      {
         "detail":"Password should be at least 8 character(s)",
         "source":{
            "pointer":"/data/attributes/password"
         },
         "title":"should be at least 8 character(s)"
      }
   ],
   "jsonapi":{
      "version":"1.0"
   }
}

You can get the detail error message "Password should be at least 8 character(s)" like this:

getErrorForField "password" error

getJsonApiErrors : RequestError -> List ( String, String )

Retuens a list of the json api errors or an empty list if there is no error.

Let's say you have the following json api errors:

{
   "errors":[
      {
         "detail":"Password should be at least 8 character(s)",
         "source":{
            "pointer":"/data/attributes/password"
         },
         "title":"should be at least 8 character(s)"
      },
      {
         "detail":"Username must no be empty",
         "source":{
            "pointer":"/data/attributes/username"
         },
         "title":"should not be empty"
      }
   ],
   "jsonapi":{
      "version":"1.0"
   }
}

Calling getJsonApiErrors errors will return:

[ ( "password", "Password should be at least 8 character(s)" )
, ( "username", "Username must no be empty" )
]