|
1 | | -import type {RpcValue} from '../../../messages/Value'; |
2 | | -import type {IRpcError} from './RpcErrorType'; |
| 1 | +import {RpcError, RpcErrorCodes, IRpcError} from 'rpc-error'; |
3 | 2 |
|
4 | | -export enum RpcErrorCodes { |
5 | | - /** Any unknown sever error is wrapped into INTERNAL_ERROR, error 500. */ |
6 | | - INTERNAL_ERROR = 0, |
7 | | - |
8 | | - /** When request is not valid, e.g. when request validation fails, error 400. */ |
9 | | - BAD_REQUEST = 1, |
10 | | - |
11 | | - /** |
12 | | - * Error thrown when there was no activity on a |
13 | | - * stream for a long time, and timeout was reached. |
14 | | - */ |
15 | | - TIMEOUT = 2, |
16 | | - |
17 | | - /** Resource not found, error 404. */ |
18 | | - NOT_FOUND = 3, |
19 | | - |
20 | | - /** When operation cannot be performed due to a conflict, error 409. */ |
21 | | - CONFLICT = 4, |
22 | | - |
23 | | - ID_TAKEN = 5, |
24 | | - INVALID_METHOD = 6, |
25 | | - INVALID_METHOD_NAME = 7, |
26 | | - NO_METHOD_SPECIFIED = 8, |
27 | | - METHOD_NOT_FOUND = 9, |
28 | | - |
29 | | - STOP = 10, |
30 | | - DISCONNECT = 11, |
31 | | - BUFFER_OVERFLOW = 12, |
32 | | -} |
33 | | - |
34 | | -export type RpcErrorValue = RpcValue<RpcError>; |
35 | | - |
36 | | -export class RpcError extends Error implements IRpcError { |
37 | | - public static from(error: unknown): RpcError { |
38 | | - if (error instanceof RpcError) return error; |
39 | | - return RpcError.internal(error); |
40 | | - } |
41 | | - |
42 | | - public static fromCode( |
43 | | - errno: RpcErrorCodes, |
44 | | - message = '', |
45 | | - meta: unknown = undefined, |
46 | | - originalError: unknown = undefined, |
47 | | - ): RpcError { |
48 | | - const code = RpcErrorCodes[errno]; |
49 | | - return new RpcError(message || code, code, errno, undefined, meta || undefined, originalError); |
50 | | - } |
51 | | - |
52 | | - public static internal(originalError: unknown, message = 'Internal Server Error'): RpcError { |
53 | | - return RpcError.fromCode(RpcErrorCodes.INTERNAL_ERROR, message, undefined, originalError); |
54 | | - } |
55 | | - |
56 | | - public static badRequest(message = 'Bad Request'): RpcError { |
57 | | - return RpcError.fromCode(RpcErrorCodes.BAD_REQUEST, message); |
58 | | - } |
59 | | - |
60 | | - public static notFound(message = 'Not Found'): RpcError { |
61 | | - return RpcError.fromCode(RpcErrorCodes.NOT_FOUND, message); |
62 | | - } |
63 | | - |
64 | | - public static validation(message: string, meta?: unknown): RpcError { |
65 | | - return RpcError.fromCode(RpcErrorCodes.BAD_REQUEST, message, meta); |
66 | | - } |
67 | | - |
68 | | - public static isRpcError(error: unknown): error is RpcError { |
69 | | - return error instanceof RpcError; |
70 | | - } |
71 | | - |
72 | | - constructor( |
73 | | - public readonly message: string, |
74 | | - public readonly code: string | undefined, |
75 | | - public readonly errno: number, |
76 | | - public readonly errorId: string | undefined, |
77 | | - public readonly meta: unknown | undefined, |
78 | | - public readonly originalError: unknown | undefined, |
79 | | - ) { |
80 | | - super(message); |
81 | | - if (message === code) this.code = undefined; |
82 | | - Object.setPrototypeOf(this, RpcError.prototype); |
83 | | - } |
84 | | - |
85 | | - public toJson(): IRpcError { |
86 | | - const err: IRpcError = {message: this.message}; |
87 | | - if (this.code) err.code = this.code; |
88 | | - if (this.errno) err.errno = this.errno; |
89 | | - if (this.errorId) err.errorId = this.errorId; |
90 | | - if (this.meta) err.meta = this.meta; |
91 | | - return err; |
92 | | - } |
93 | | -} |
| 3 | +export {RpcError, RpcErrorCodes, IRpcError}; |
0 commit comments