11import { isErrorLike } from "serialize-error" ;
2- import type { ErrorDetail , LocalisedMessage } from "./types.js " ;
3- import { withNullProto } from "./utils.js " ;
2+ import type { ErrorDetail , LocalisedMessage } from "./types.ts " ;
3+ import { withNullProto } from "./utils.ts " ;
44
55export type DebugData = Record < string , unknown > ;
66
7- enum StatusCode {
8- OK = 0 ,
9- CANCELLED = 1 ,
10- UNKNOWN = 2 ,
11- INVALID_ARGUMENT = 3 ,
12- DEADLINE_EXCEEDED = 4 ,
13- NOT_FOUND = 5 ,
14- ALREADY_EXISTS = 6 ,
15- PERMISSION_DENIED = 7 ,
16- RESOURCE_EXHAUSTED = 8 ,
17- FAILED_PRECONDITION = 9 ,
18- ABORTED = 10 ,
19- OUT_OF_RANGE = 11 ,
20- UNIMPLEMENTED = 12 ,
21- INTERNAL = 13 ,
22- UNAVAILABLE = 14 ,
23- DATA_LOSS = 15 ,
24- UNAUTHENTICATED = 16 ,
7+ const StatusCode = {
8+ OK : 0 as const ,
9+ CANCELLED : 1 as const ,
10+ UNKNOWN : 2 as const ,
11+ INVALID_ARGUMENT : 3 as const ,
12+ DEADLINE_EXCEEDED : 4 as const ,
13+ NOT_FOUND : 5 as const ,
14+ ALREADY_EXISTS : 6 as const ,
15+ PERMISSION_DENIED : 7 as const ,
16+ RESOURCE_EXHAUSTED : 8 as const ,
17+ FAILED_PRECONDITION : 9 as const ,
18+ ABORTED : 10 as const ,
19+ OUT_OF_RANGE : 11 as const ,
20+ UNIMPLEMENTED : 12 as const ,
21+ INTERNAL : 13 as const ,
22+ UNAVAILABLE : 14 as const ,
23+ DATA_LOSS : 15 as const ,
24+ UNAUTHENTICATED : 16 as const ,
2525}
2626
2727// just export the type, we CustomError.XX should be used for the actual code
28- export type { StatusCode } ;
28+ export type StatusCode = typeof StatusCode [ keyof typeof StatusCode ] ;
2929
30- type SerializedError < T extends StatusCode | number > = {
30+ const statusCodes : ReadonlySet < number > = new Set ( Object . values ( StatusCode ) ) ;
31+
32+ export function isStatusCode ( value : unknown ) : value is StatusCode {
33+ return typeof value === 'number' && statusCodes . has ( value ) ;
34+ }
35+
36+ export type SerializedError < T extends StatusCode > = {
3137 readonly debug ?: DebugData ;
3238 readonly stack ?: string ;
3339 readonly cause ?: SerializedError < StatusCode > [ ] ;
@@ -60,17 +66,17 @@ export class CustomError extends Error {
6066
6167 static http = Object . freeze ( {
6268 [ CustomError . OK ] : 200 ,
63- [ CustomError . CANCELLED ] : 299 ,
69+ [ CustomError . CANCELLED ] : 499 ,
6470 [ CustomError . UNKNOWN ] : 500 ,
6571 [ CustomError . INVALID_ARGUMENT ] : 400 ,
6672 [ CustomError . DEADLINE_EXCEEDED ] : 504 ,
6773 [ CustomError . NOT_FOUND ] : 404 ,
6874 [ CustomError . ALREADY_EXISTS ] : 409 ,
6975 [ CustomError . PERMISSION_DENIED ] : 403 ,
70- [ CustomError . RESOURCE_EXHAUSTED ] : 403 ,
76+ [ CustomError . RESOURCE_EXHAUSTED ] : 429 ,
7177 [ CustomError . FAILED_PRECONDITION ] : 400 ,
72- [ CustomError . ABORTED ] : 299 ,
73- [ CustomError . OUT_OF_RANGE ] : 400 ,
78+ [ CustomError . ABORTED ] : 409 ,
79+ [ CustomError . OUT_OF_RANGE ] : 422 ,
7480 [ CustomError . UNIMPLEMENTED ] : 501 ,
7581 [ CustomError . INTERNAL ] : 500 ,
7682 [ CustomError . UNAVAILABLE ] : 503 ,
@@ -114,7 +120,7 @@ export class CustomError extends Error {
114120 this . cause = cause ;
115121
116122 // FF doesnt have captureStackTrace
117- if ( Error . captureStackTrace ) {
123+ if ( ' captureStackTrace' in Error ) {
118124 Error . captureStackTrace ( this , this . constructor ) ;
119125 }
120126
@@ -189,13 +195,13 @@ export class CustomError extends Error {
189195 /**
190196 * "Hydrates" a previously serialised error object
191197 */
192- public static fromJSON < const T extends StatusCode | number > (
198+ public static fromJSON < const T extends StatusCode > (
193199 params : SerializedError < T > ,
194200 ) {
195201 const { message, details, code } = params ;
196202
197203 class ImportedError extends CustomError {
198- override readonly code = code ;
204+ override readonly code = code as StatusCode ;
199205 }
200206
201207 const err = new ImportedError ( message ) . debug ( {
@@ -215,9 +221,11 @@ export class CustomError extends Error {
215221 public static suggestHttpResponseCode ( err : Error | CustomError | unknown ) {
216222 return (
217223 ( CustomError . isCustomError ( err ) && CustomError . http [ err . code ] ) ||
218- CustomError . http [ CustomError . UNKNOWN ]
224+ CustomError . http [ CustomError . UNKNOWN ] ||
225+ 500
219226 ) ;
220227 }
228+
221229}
222230
223231// Mark all instances of 'CustomError'
0 commit comments