Skip to content

Commit 9be223a

Browse files
committed
feat: allow string enums for errors
1 parent 012e38c commit 9be223a

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

__tests__/typescript-stress.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ import { flattenErrorType, ProcedureErrorSchemaType } from '../router/errors';
2525
import { ReadableImpl } from '../router/streams';
2626
import { createMockTransportNetwork } from '../testUtil/fixtures/mockTransport';
2727

28+
enum TestErrorCodes {
29+
ERROR_ONE = 'ERROR_ONE',
30+
ERROR_TWO = 'ERROR_TWO',
31+
ERROR_THREE = 'ERROR_THREE',
32+
}
33+
2834
const requestData = Type.Union([
2935
Type.Object({ a: Type.Number() }),
3036
Type.Object({ c: Type.String() }),
@@ -479,6 +485,53 @@ describe('Procedure error schema', () => {
479485
);
480486
});
481487

488+
test('object with enum code', () => {
489+
acceptErrorSchema(
490+
Type.Object({
491+
code: Type.Enum(TestErrorCodes),
492+
message: Type.String(),
493+
}),
494+
);
495+
});
496+
497+
test('union containing enum-based error schemas', () => {
498+
acceptErrorSchema(
499+
Type.Union([
500+
Type.Object({
501+
code: Type.Enum(TestErrorCodes),
502+
message: Type.String(),
503+
}),
504+
Type.Object({
505+
code: Type.Literal('OTHER_ERROR'),
506+
message: Type.String(),
507+
}),
508+
]),
509+
);
510+
});
511+
512+
test('flattenErrorType with enum-based error schemas', () => {
513+
acceptErrorSchema(
514+
flattenErrorType(
515+
Type.Union([
516+
Type.Object({
517+
code: Type.Enum(TestErrorCodes),
518+
message: Type.String(),
519+
}),
520+
Type.Union([
521+
Type.Object({
522+
code: Type.Literal('ERROR_4'),
523+
message: Type.String(),
524+
}),
525+
Type.Object({
526+
code: Type.Literal('ERROR_5'),
527+
message: Type.String(),
528+
}),
529+
]),
530+
]),
531+
),
532+
);
533+
});
534+
482535
test('union of union', () => {
483536
acceptErrorSchema(
484537
flattenErrorType(

router/errors.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Kind,
33
Static,
4+
TEnum,
45
TLiteral,
56
TNever,
67
TObject,
@@ -32,13 +33,18 @@ export const CANCEL_CODE = 'CANCEL';
3233

3334
type TLiteralString = TLiteral<string>;
3435

36+
/**
37+
* String literals or string enums for error codes.
38+
*/
39+
type TStringCode = TLiteralString | TEnum<Record<string, string>>;
40+
3541
export type BaseErrorSchemaType =
3642
| TObject<{
37-
code: TLiteralString;
43+
code: TStringCode;
3844
message: TLiteralString | TString;
3945
}>
4046
| TObject<{
41-
code: TLiteralString;
47+
code: TStringCode;
4248
message: TLiteralString | TString;
4349
extras: TSchema;
4450
}>;
@@ -139,7 +145,9 @@ function isUnion(schema: TSchema): schema is TUnion {
139145
type Flatten<T> = T extends BaseErrorSchemaType
140146
? T
141147
: T extends TUnion<Array<infer U extends TSchema>>
142-
? Flatten<U>
148+
? U extends BaseErrorSchemaType
149+
? TUnion<Array<U>>
150+
: Flatten<U>
143151
: unknown;
144152

145153
/**

0 commit comments

Comments
 (0)