Skip to content

Commit f21e684

Browse files
author
Elias Mulhall
committed
Remove decoderErrorString function
I think I added this helper because I read somewhere that you should make all of your thrown error payloads be strings. This confusion was probably related to using `Error`. I've changed my mind about that, so let's get rid of the extra code.
1 parent 77ad1c3 commit f21e684

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/decoder.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ export type DecoderObject<A> = {[t in keyof A]: Decoder<A[t]>};
4343
export const isDecoderError = (a: any): a is DecoderError =>
4444
a.kind === 'DecoderError' && typeof a.at === 'string' && typeof a.message === 'string';
4545

46-
/**
47-
* `DecoderError` information as a formatted string.
48-
*/
49-
export const decoderErrorString = (error: DecoderError): string =>
50-
`Input: ${JSON.stringify(error.input)}\nFailed at ${error.at}: ${error.message}`;
51-
5246
/*
5347
* Helpers
5448
*/
@@ -597,8 +591,7 @@ export class Decoder<A> {
597591
* Run the decoder and return the value on success, or throw an exception
598592
* with a formatted error string.
599593
*/
600-
runWithException = (json: any): A =>
601-
Result.withException(Result.mapError(decoderErrorString, this.run(json)));
594+
runWithException = (json: any): A => Result.withException(this.run(json));
602595

603596
/**
604597
* Construct a new decoder that applies a transformation to the decoded

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Result from './result';
22
export {Result};
33

4-
export {Decoder, DecoderError, isDecoderError, decoderErrorString, DecoderObject} from './decoder';
4+
export {Decoder, DecoderError, isDecoderError, DecoderObject} from './decoder';
55

66
export {
77
string,

test/json-decode.test.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,20 @@ describe('runWithException', () => {
682682
});
683683

684684
it('throws an exception when the decoder fails', () => {
685-
expect(() => decoder.runWithException(42)).toThrowError(
686-
'Input: 42\nFailed at input: expected a boolean, got a number'
687-
);
685+
let thrownError: any;
686+
687+
try {
688+
decoder.runWithException(42);
689+
} catch (e) {
690+
thrownError = e;
691+
}
692+
693+
expect(thrownError).toEqual({
694+
kind: 'DecoderError',
695+
input: 42,
696+
at: 'input',
697+
message: 'expected a boolean, got a number'
698+
});
688699
});
689700
});
690701

0 commit comments

Comments
 (0)