Skip to content

Commit

Permalink
extract reportGitHubError, simplify update
Browse files Browse the repository at this point in the history
  • Loading branch information
brandly committed Apr 18, 2020
1 parent 19e5a29 commit 32e721a
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/Generators/ElmDep.elm
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ getPathsOfElmFiles m token =
gitHubGet
{ path = "/repos/" ++ m.repoNameInput ++ "/git/trees/master?recursive=1"
, token = token
, expect = Http.expectJson GotPathsOfElmFiles pathsOfElmFilesDecoder
, expect = Http.expectJson (Result.mapError reportGitHubError >> GotPathsOfElmFiles) pathsOfElmFilesDecoder
}


Expand All @@ -154,14 +154,31 @@ gitHubGet { token, path, expect } =
}


reportGitHubError : Http.Error -> String
reportGitHubError error =
case error of
Http.BadStatus 404 ->
"Repo not found. Is it a private repo? Use an access token!"

Http.NetworkError ->
String.join " "
[ "Network Error."
, "This can happen when hitting GitHub's rate limits."
, "Using an access token increases these limits!"
]

_ ->
"Couldn't connect to github."


pathsOfElmFilesDecoder : Decoder (List String)
pathsOfElmFilesDecoder =
JD.field "tree" (JD.list (JD.field "path" JD.string))
|> JD.map (List.filter (String.endsWith ".elm"))


type Msg
= GotPathsOfElmFiles (Result Http.Error (List String))
= GotPathsOfElmFiles (Result String (List String))
| GotRawElmFile (Result Http.Error String)
| ChangeRepo String
| ChangeToken String
Expand Down Expand Up @@ -267,24 +284,8 @@ update msg m =
, Cmd.none
)

Err e ->
( { m
| state =
Error <|
case e of
Http.BadStatus 404 ->
"Repo not found. Is it a private repo? Use an access token!"

Http.NetworkError ->
String.join " "
[ "Network Error."
, "This can happen when hitting GitHub's rate limits."
, "Using an access token increases these limits!"
]

_ ->
"Couldn't connect to github."
}
Err error ->
( { m | state = Error error }
, Cmd.none
)

Expand Down

0 comments on commit 32e721a

Please sign in to comment.