Closed knuterikl closed 4 years ago
Hi @knutbekk , nice feature. I'll review / merge / release during the weekend. Thanks for your contribution!
Hi @knutbekk ,
I want to make sure I understand the purpose of this function.
Why is it useful that mapError
can return a different type than the decoder type? i.e. <a | b>
? Why not just <a>
(the original decoder's expected return type).
Hi @knutbekk , I want to make sure I understand the purpose of this function. Why is it useful that
mapError
can return a different type than the decoder type? i.e.<a | b>
? Why not just<a>
(the original decoder's expected return type).
So that you can "detect" that the decoder has failed. In usage with an object decoder inside an array decoder one might want to map to null
instead of the a
object type, to filter out the failed values instead of using fallback to a default value. As well as having access to the error of the failing decoder:
const tagsDecoder = JsonDecoder.object({
id: JsonDecoder.string,
title: JsonDecoder.string,
)}.mapError((error) => {
// Log the error to remote logger so we can investigate whats wrong
remoteLogger.log(error)
return null
})
const userDecoder = JsonDecoder.object({
tags: JsonDecoder.array(tagsDecoder, "TagsDecoder").map(arr => arr.filter(notNull))
}, "UserDecoder")
Thanks again for your contribution!
mapError
Transforms an error into a value.