Skip to content

Commit 466e15a

Browse files
author
Elias Mulhall
committed
Add mapError decoder method
1 parent 2877e99 commit 466e15a

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/decoder.ts

+8
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,14 @@ export class Decoder<A> {
730730
map = <B>(f: (value: A) => B): Decoder<B> =>
731731
new Decoder((json, context) => Result.map(f, this.decode(json, context)));
732732

733+
/**
734+
* Construct a new decoder that applies a transformation to an error message
735+
* on failure. If the decoder fails then `f` will be applied to the error. If
736+
* it succeeds the value will propagated through.
737+
*/
738+
mapError = (f: (error: DecoderError) => DecoderError): Decoder<A> =>
739+
new Decoder((json, context) => Result.mapError(f, this.decode(json, context)));
740+
733741
/**
734742
* Chain together a sequence of decoders. The first decoder will run, and
735743
* then the function will determine what decoder to run second. If the result

0 commit comments

Comments
 (0)