elm/http 1.0.0 定义Http.Error为
type Error
= BadUrl String
| Timeout
| NetworkError
| BadStatus (Response String)
| BadPayload String (Response String)
Run Code Online (Sandbox Code Playgroud)
但2.0.0将其更改为
type Error
= BadUrl String
| Timeout
| NetworkError
| BadStatus Int
| BadBody String
Run Code Online (Sandbox Code Playgroud)
接收时BadStatus我无法获取请求的正文,只能获取状态代码。在文档中,埃文提出了一个解决方案,但我不明白如何使其发挥作用。
如果我们定义自己的expectJson类似
expectJson : (Result Http.Error a -> msg) -> D.Decoder a -> Expect msg
expectJson toMsg decoder =
expectStringResponse toMsg <|
\response ->
case response of
Http.BadStatus_ metadata body ->
Err (Http.BadStatus metadata.statusCode)
...
Run Code Online (Sandbox Code Playgroud)
然后我们就可以访问元数据和正文,但如何使用它们呢?我应该定义自己的myBadStatus并返回它吗?
Http.BadStatus_ metadata body ->
Err (myBadStatus metadata.statusCode body)
Run Code Online (Sandbox Code Playgroud)
这行得通吗?
我需要的是转换以下代码:
myErrorMessage : Http.Error -> String
myErrorMessage error =
case error of
Http.BadStatus response ->
case Decode.decodeString myErrorDecoder response.body of
Ok err ->
err.message
Err e ->
"Failed to parse JSON response."
...
Run Code Online (Sandbox Code Playgroud)
谢谢。
编辑 22/4/2019:我更新了 http-extras 2.0+ 版本的答案,其中有一些 API 更改。感谢 Berend de Boer 指出了这一点!
下面的答案给出了使用我编写的包(根据请求)的解决方案,但您不必使用该包!我写了一篇关于如何从 HTTP 响应中提取详细信息的整篇文章,其中包括多个不需要该包的 Ellie 示例,以及一个使用该包的示例。
正如弗朗西斯科提到的,我正是为此目的创建了一个包,使用问题中描述的类似方法:https: //package.elm-lang.org/packages/jzxhuang/http-extras/latest/。
具体来说,要使用的模块Http.Detailed。它定义了一个错误类型,使原始正文在出现错误时保持不变:
type Error body
= BadUrl String
| Timeout
| NetworkError
| BadStatus Metadata body Int
| BadBody Metadata body String
Run Code Online (Sandbox Code Playgroud)
像这样提出请求:
type Msg
= MyAPIResponse (Result (Http.Detailed.Error String) ( Http.Metadata, String ))
sendRequest : Cmd Msg
sendRequest =
Http.get
{ url = "/myapi"
, expect = Http.Detailed.expectString MyAPIResponse
Run Code Online (Sandbox Code Playgroud)
在您的更新中,处理结果,包括在 BadStatus 时解码正文:
update msg model =
case msg of
MyAPIResponse httpResponse ->
case httpResponse of
Ok ( metadata, respBody ) ->
-- Do something with the metadata if you need! i.e. access a header
Err error ->
case error of
Http.Detailed.BadStatus metadata body statusCode ->
-- Try to decode the body the body here...
...
...
Run Code Online (Sandbox Code Playgroud)
感谢弗朗西斯科就此事与我联系,希望这个答案可以帮助任何面临与 OP 相同问题的人。
| 归档时间: |
|
| 查看次数: |
1315 次 |
| 最近记录: |