Make DataSource.Port error catchable.

This commit is contained in:
Dillon Kearns 2022-12-31 13:24:00 -08:00
parent 1f109fca7c
commit 6bd91eb68e
4 changed files with 8 additions and 4 deletions

File diff suppressed because one or more lines are too long

View File

@ -100,6 +100,7 @@ data routeParams =
(DataSource.Port.get "getItems"
Encode.null
(Decode.list Decode.string)
|> DataSource.throw
|> DataSource.map
(\items ->
Server.Response.render
@ -127,6 +128,7 @@ action routeParams =
DataSource.Port.get "addItem"
(Encode.string newItem)
(Decode.list Decode.string)
|> DataSource.throw
|> DataSource.map
(\_ ->
Server.Response.render ActionData
@ -136,6 +138,7 @@ action routeParams =
DataSource.Port.get "deleteAllItems"
Encode.null
(Decode.list Decode.string)
|> DataSource.throw
|> DataSource.map
(\_ ->
Server.Response.render ActionData

View File

@ -53,7 +53,7 @@ data : DataSource Throwable Data
data =
DataSource.map2 Data
(DataSource.File.rawFile "greeting.txt" |> DataSource.throw)
(DataSource.Port.get "hello" (Encode.string "Jane") Decode.string)
(DataSource.Port.get "hello" (Encode.string "Jane") Decode.string |> DataSource.throw)
head :

View File

@ -14,6 +14,7 @@ module DataSource.Port exposing
import DataSource
import DataSource.Http
import DataSource.Internal.Request
import Exception exposing (Catchable)
import Json.Decode exposing (Decoder)
import Json.Encode as Encode
@ -79,7 +80,7 @@ prefer to add ANSI color codes within the error string in an exception and it wi
As with any JavaScript or NodeJS code, avoid doing blocking IO operations. For example, avoid using `fs.readFileSync`, because blocking IO can slow down your elm-pages builds and dev server.
-}
get : String -> Encode.Value -> Decoder b -> DataSource.DataSource Error b
get : String -> Encode.Value -> Decoder b -> DataSource.DataSource (Catchable Error) b
get portName input decoder =
DataSource.Internal.Request.request
{ name = "port"
@ -93,7 +94,7 @@ get portName input decoder =
decoder
|> DataSource.Http.expectJson
}
|> DataSource.onError (\_ -> DataSource.fail Error)
|> DataSource.onError (\_ -> DataSource.fail (Exception.Catchable Error "Port Error - TODO better error message"))
{-| -}