Skip to content

Commit 453683e

Browse files
committed
oops
1 parent 77327c5 commit 453683e

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

src/utils.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { AnyError, UserFacingError, create } from ".";
1+
import type { AnyError, UserFacingError } from '.';
22

33
type ErrorHandlers<T extends AnyError, O> = {
4-
[K in T["name"]]: (error: Extract<T, { name: K }>) => O;
4+
[K in T['name']]: (error: Extract<T, { name: K }>) => O;
55
};
66

77
/**
@@ -11,12 +11,15 @@ type ErrorHandlers<T extends AnyError, O> = {
1111
* @param handlers - The handlers for each error type
1212
* @returns
1313
*/
14-
export function match<T extends AnyError, O>(error: T, handlers: ErrorHandlers<T, O>): O {
14+
export function match<T extends AnyError, O>(
15+
error: T,
16+
handlers: ErrorHandlers<T, O>,
17+
): O {
1518
return (handlers as any)[error.name](error);
1619
}
1720

1821
type InternalErrorHandlers<T extends AnyError, O> = {
19-
[K in T["name"]]: Extract<T, { name: K }> extends { type: "internal" }
22+
[K in T['name']]: Extract<T, { name: K }> extends { type: 'internal' }
2023
? (error: Extract<T, { name: K }>) => O
2124
: never;
2225
};
@@ -34,17 +37,15 @@ type NeverNever<T> = {
3437
*/
3538
export function mapToUserFacingError<E extends AnyError>(
3639
error: E,
37-
handlers: NeverNever<InternalErrorHandlers<E, string>>
40+
handlers: NeverNever<InternalErrorHandlers<E, string>>,
3841
): UserFacingError {
39-
if (error.type === "user") return error;
42+
if (error.type === 'user') return error;
4043

4144
const message = (handlers as any)[error.name](error);
4245

4346
return {
4447
...error,
45-
type: "user",
48+
type: 'user',
4649
userMessage: message,
4750
};
4851
}
49-
50-
export type ErrorTypes<Factory extends ReturnType<typeof create>, Key extends keyof Factory> = ReturnType<Factory[Key]>;

tests/index.test.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
1-
import { describe, expect, it } from "vitest";
2-
import * as justerror from "../src/index.js";
1+
import { describe, expect, it } from 'vitest';
2+
import * as justerror from '../src/index.js';
33

44
const error = justerror.create({
55
InvalidArgumentError: {
6-
code: "USER_001",
7-
message: (args: { field: string; value?: unknown }) => `Invalid argument: ${args.field} = ${args.value}`,
8-
userMessage: (args: { field: string; value?: unknown }) => `Invalid argument: ${args.field} = ${args.value}`,
6+
code: 'USER_001',
7+
message: (args: { field: string; value?: unknown }) =>
8+
`Invalid argument: ${args.field} = ${args.value}`,
9+
userMessage: (args: { field: string; value?: unknown }) =>
10+
`Invalid argument: ${args.field} = ${args.value}`,
911
},
1012
InternalError: {
11-
code: "INTERNAL_001",
13+
code: 'INTERNAL_001',
1214
message: (args: { message: string }) => `Internal error: ${args.message}`,
1315
},
1416
});
1517

16-
describe("create", () => {
17-
it("Should create the correct error", () => {
18-
const e = error.InternalError({ message: "test" });
18+
describe('create', () => {
19+
it('Should create the correct error', () => {
20+
const e = error.InternalError({ message: 'test' });
1921

20-
expect(e.message).toBe("Internal error: test");
21-
expect(e.code).toBe("INTERNAL_001");
22-
expect(e.type).toBe("internal");
23-
expect(e.name).toBe("InternalError");
22+
expect(e.message).toBe('Internal error: test');
23+
expect(e.code).toBe('INTERNAL_001');
24+
expect(e.type).toBe('internal');
25+
expect(e.name).toBe('InternalError');
2426
expect(e.recoverable).toBe(false);
25-
expect(e.config).toEqual({ message: "test" });
27+
expect(e.config).toEqual({ message: 'test' });
2628
});
2729
});
2830

29-
describe("match", () => {
30-
it("Should match the correct error", () => {
31+
describe('match', () => {
32+
it('Should match the correct error', () => {
3133
const fake = () => true;
3234

3335
// now can be either error
3436
const e = fake()
35-
? error.InternalError({ message: "test" })
36-
: error.InvalidArgumentError({ field: "test", value: "test" });
37+
? error.InternalError({ message: 'test' })
38+
: error.InvalidArgumentError({ field: 'test', value: 'test' });
3739

3840
const result = justerror.match(e, {
3941
InternalError: (e) => e.message,
4042
InvalidArgumentError: (e) => e.message,
4143
});
4244

43-
expect(result).toBe("Internal error: test");
45+
expect(result).toBe('Internal error: test');
4446

4547
// error matching should be exhaustive if it isn't tsc will fail the next line
4648

@@ -51,33 +53,33 @@ describe("match", () => {
5153
});
5254
});
5355

54-
describe("mapToUserFacingError", () => {
55-
it("Should map the error to a user facing error", () => {
56+
describe('mapToUserFacingError', () => {
57+
it('Should map the error to a user facing error', () => {
5658
const fake = () => true;
5759

5860
// now can be either error
5961
const e = fake()
60-
? error.InternalError({ message: "test" })
61-
: error.InvalidArgumentError({ field: "test", value: "test" });
62+
? error.InternalError({ message: 'test' })
63+
: error.InvalidArgumentError({ field: 'test', value: 'test' });
6264

63-
// only should have to handle internal errors
65+
// only should have to handle internal errors
6466
const result = justerror.mapToUserFacingError(e, {
65-
InternalError: () => "There was an error serving your request",
67+
InternalError: () => 'There was an error serving your request',
6668
});
6769

68-
expect(result.userMessage).toBe("There was an error serving your request");
69-
expect(result.message).toBe("Internal error: test");
70-
expect(result.code).toBe("INTERNAL_001");
71-
expect(result.type).toBe("user");
72-
expect(result.name).toBe("InternalError");
70+
expect(result.userMessage).toBe('There was an error serving your request');
71+
expect(result.message).toBe('Internal error: test');
72+
expect(result.code).toBe('INTERNAL_001');
73+
expect(result.type).toBe('user');
74+
expect(result.name).toBe('InternalError');
7375
expect(result.recoverable).toBe(false);
74-
expect(result.config).toEqual({ message: "test" });
76+
expect(result.config).toEqual({ message: 'test' });
7577

76-
// users should only have to handle internal errors otherwise tsc will fail the next line
78+
// users should only have to handle internal errors otherwise tsc will fail the next line
7779

7880
justerror.mapToUserFacingError(e, {
7981
InternalError: (e) => e.message,
80-
// @ts-expect-error Shouldn't need to handle user facing errors
82+
// @ts-expect-error Shouldn't need to handle user facing errors
8183
InvalidArgumentError: (e) => e.message,
8284
});
8385
});

0 commit comments

Comments
 (0)