Skip to content

Instantly share code, notes, and snippets.

View jzxhuang's full-sized avatar

Jeff Huang jzxhuang

View GitHub Profile
@jzxhuang
jzxhuang / Metadata.elm
Last active March 28, 2019 09:52
Handling Detailed HTTP Responses in Elm
type alias Metadata =
{ url : String
, statusCode : Int
, statusText : String
, headers : Dict String String
}
@jzxhuang
jzxhuang / FullExampleExpectStringDetailed.elm
Last active March 28, 2019 13:40
Full example of sending an HTTP request that returns a detailed Response in Elm using expectStringDetailed
module Main exposing (main)
{-
This example demonstrates how to send an HTTP request and get back
a detailed response using expectStringDetailed. The response will be
more detailed for both successful and unsuccessful requests.
From this post: https://medium.com/@jzxhuang/handling-detailed-http-responses-in-elm-6ddd02322e
-}
@jzxhuang
jzxhuang / HttpError.elm
Last active March 28, 2019 14:19
The type definition for Http.Error
type Error
= BadUrl String
| Timeout
| NetworkError
| BadStatus Int
| BadBody String
@jzxhuang
jzxhuang / ExpectStringDetailed.elm
Last active April 4, 2019 07:39
An expectString function with a more detailed response on the sucess case
expectStringDetailed : (Result Error ( Metadata, String ) -> msg) -> Expect msg
expectStringDetailed msg =
Http.expectStringResponse msg convertResponseString
expectStringResponse : (Result x a -> msg) -> (Response String -> Result x a) -> Expect msg
type Response body
= BadUrl_ String
| Timeout_
| NetworkError_
| BadStatus_ Metadata body
| GoodStatus_ Metadata body
@jzxhuang
jzxhuang / ExpectStringDetailed_Improved.elm
Created April 4, 2019 07:43
ExpectStringDetailed handling success and error case
expectStringDetailed : (Result DetailedError ( Metadata, String ) -> msg) -> Expect msg
@jzxhuang
jzxhuang / ExpectStringDetailed.elm
Last active April 4, 2019 07:45
An expectString function with a more detailed successful response and error.
-- Change this to return Result ErrorDetailed ( Http.Metadata, String ) instead of Result Http.Error ( Http.Metadata, String )
convertResponseString : Response String -> Result ErrorDetailed ( Metadata, String )
convertResponseString httpResponse =
case httpResponse of
Http.BadUrl_ url ->
Err (BadUrl url)
Http.Timeout_ ->
Err Timeout
@jzxhuang
jzxhuang / ErrorDetailed.elm
Last active April 5, 2019 03:44
Detailed error
-- The only difference between this and Http.Error is the BadStatus constructor
type ErrorDetailed
= BadUrl String
| Timeout
| NetworkError
| BadStatus Http.Metadata String
| BadBody String
@jzxhuang
jzxhuang / ConvertResponseString.elm
Last active April 5, 2019 03:45
Convert a string Http.Response to a Tuple (Http.Metadata, String)
convertResponseString : Response String -> Result Error ( Metadata, String )
convertResponseString httpResponse =
case httpResponse of
Http.BadUrl_ url ->
Err (Http.BadUrl url)
Http.Timeout_ ->
Err Http.Timeout
Http.NetworkError_ ->
{
"data": {
"id": 2,
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
}
}