List elm:从列表中获取第一条错误消息
给出模型(与以下内容相关): 我想创建一个函数,从ErrorResponse返回forst错误消息。以下是我尝试过的:List elm:从列表中获取第一条错误消息,list,elm,List,Elm,给出模型(与以下内容相关): 我想创建一个函数,从ErrorResponse返回forst错误消息。以下是我尝试过的: firstErrorMessage : Decoder CardEngineErrorResponse -> String firstErrorMessage decoder response = case List.head response.validationErrors of Just something ->
firstErrorMessage : Decoder CardEngineErrorResponse -> String
firstErrorMessage decoder response =
case List.head response.validationErrors of
Just something ->
something.errorMessage
Nothing ->
toString ""
但我得到了一个错误:
The definition of `firstErrorMessage` does not match its type annotation.
The type annotation for `firstErrorMessage` says it always returns:
String
But the returned value (shown above) is a:
{ b | validationErrors : List { a | errorMessage : String } } -> String
Hint: It looks like a function needs 1 more argument.
你们有谁知道我做错了什么吗?如果您只是试图从
错误响应
值中获取第一条错误消息,您不需要引用解码器
:
firstErrorMessage:ErrorResponse->String
firstErrorMessage响应=
案例列表.head response.validationErrors的
只是一些->
什么,错误消息
没有->
""
或者更简洁地说:
firstErrorMessage:ErrorResponse->String
firstErrorMessage响应=
List.head response.validationErrors
|>也许吧。地图。错误信息
|>可能是默认值“”
如果您想在解码器的上下文中完成这一切,可以使用Json.Decode.map
:
firstErrorMessageDecoder:解码器字符串
firstErrorMessageDecoder=
解码错误响应
|>映射firstErrorMessage
还有一点需要注意:当某件事情有可能失败时,最好保留Maybe
的概念。您可以通过返回Maybe string
,构建一个更强大的API,而不是默认为调用者必须知道的空字符串:
firstErrorMessage:ErrorResponse->Maybe String
firstErrorMessage响应=
List.head response.validationErrors
|>也许吧。地图。错误信息
如果您只是试图从错误响应
值中获取第一条错误消息,则无需参考解码器
:
firstErrorMessage:ErrorResponse->String
firstErrorMessage响应=
案例列表.head response.validationErrors的
只是一些->
什么,错误消息
没有->
""
或者更简洁地说:
firstErrorMessage:ErrorResponse->String
firstErrorMessage响应=
List.head response.validationErrors
|>也许吧。地图。错误信息
|>可能是默认值“”
如果您想在解码器的上下文中完成这一切,可以使用Json.Decode.map
:
firstErrorMessageDecoder:解码器字符串
firstErrorMessageDecoder=
解码错误响应
|>映射firstErrorMessage
还有一点需要注意:当某件事情有可能失败时,最好保留Maybe
的概念。您可以通过返回Maybe string
,构建一个更强大的API,而不是默认为调用者必须知道的空字符串:
firstErrorMessage:ErrorResponse->Maybe String
firstErrorMessage响应=
List.head response.validationErrors
|>也许吧。地图。错误信息
如果你来马德里,请告诉我,因为我拥有你一箱箱啤酒;)如果你来马德里,请告诉我,因为我拥有你一箱箱啤酒;)
The definition of `firstErrorMessage` does not match its type annotation.
The type annotation for `firstErrorMessage` says it always returns:
String
But the returned value (shown above) is a:
{ b | validationErrors : List { a | errorMessage : String } } -> String
Hint: It looks like a function needs 1 more argument.