@based/errors 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,166 @@
1
+ import { BasedRoute } from '@based/functions';
2
+ export declare enum BasedErrorCode {
3
+ incorrectFieldType = 1000,
4
+ incorrectNodeType = 1001,
5
+ exceedsMaximum = 1002,
6
+ subceedsMinimum = 1003,
7
+ fieldDoesNotExist = 1004,
8
+ incorrectFormat = 1005,
9
+ referenceIsIncorrectType = 1006,
10
+ valueAndDefault = 1007,
11
+ defaultNotSupported = 1008,
12
+ multipleOperationsNotAllowed = 1009,
13
+ requiredFieldNotDefined = 1010,
14
+ languageNotSupported = 1011,
15
+ invalidJSON = 1012,
16
+ noLanguageFound = 1013,
17
+ cannotDeleteNodeFromModify = 1014,
18
+ nestedModifyObjectNotAllowed = 1015,
19
+ infinityNotSupported = 1016,
20
+ invalidSchemaFormat = 1017,
21
+ invalidProperty = 1018,
22
+ FunctionError = 50001,
23
+ AuthorizeFunctionError = 50002,
24
+ NoOservableCacheAvailable = 50003,
25
+ ObservableFunctionError = 50004,
26
+ ObserveCallbackError = 50005,
27
+ FunctionNotFound = 40401,
28
+ FunctionIsNotObservable = 40402,
29
+ FunctionIsObservable = 40403,
30
+ FunctionIsStream = 40404,
31
+ CannotStreamToObservableFunction = 40405,
32
+ FunctionIsWrongType = 40406,
33
+ AuthorizeRejectedError = 40301,
34
+ InvalidPayload = 40001,
35
+ PayloadTooLarge = 40002,
36
+ ChunkTooLarge = 40003,
37
+ UnsupportedContentEncoding = 40004,
38
+ NoBinaryProtocol = 40005,
39
+ LengthRequired = 41101,
40
+ MethodNotAllowed = 40501,
41
+ RateLimit = 40029,
42
+ MissingAuthStateProtocolHeader = 40030,
43
+ IncorrectAccessKey = 40031,
44
+ Block = 90001
45
+ }
46
+ type BasedParseErrorPayload = {
47
+ path: string[];
48
+ };
49
+ type FunctionErrorProps = {
50
+ err: Error | string;
51
+ requestId?: number;
52
+ route: BasedRoute;
53
+ };
54
+ type ObservableFunctionErrorProps = {
55
+ observableId: number;
56
+ err: Error | string;
57
+ route: BasedRoute;
58
+ };
59
+ type ChannelFunctionErrorProps = {
60
+ channelId: number;
61
+ err: Error | string;
62
+ route: BasedRoute;
63
+ };
64
+ export type BasedErrorPayload = {
65
+ observableId: number;
66
+ route: BasedRoute;
67
+ } | {
68
+ requestId: number;
69
+ route: BasedRoute;
70
+ } | {
71
+ channelId: number;
72
+ route: BasedRoute;
73
+ } | {
74
+ route: BasedRoute;
75
+ };
76
+ type BasedFunctionError = FunctionErrorProps | ObservableFunctionErrorProps | ChannelFunctionErrorProps;
77
+ export type ErrorPayload = {
78
+ [BasedErrorCode.incorrectFieldType]: BasedParseErrorPayload;
79
+ [BasedErrorCode.incorrectNodeType]: BasedParseErrorPayload;
80
+ [BasedErrorCode.exceedsMaximum]: BasedParseErrorPayload;
81
+ [BasedErrorCode.subceedsMinimum]: BasedParseErrorPayload;
82
+ [BasedErrorCode.fieldDoesNotExist]: BasedParseErrorPayload;
83
+ [BasedErrorCode.incorrectFormat]: BasedParseErrorPayload;
84
+ [BasedErrorCode.referenceIsIncorrectType]: BasedParseErrorPayload;
85
+ [BasedErrorCode.valueAndDefault]: BasedParseErrorPayload;
86
+ [BasedErrorCode.defaultNotSupported]: BasedParseErrorPayload;
87
+ [BasedErrorCode.multipleOperationsNotAllowed]: BasedParseErrorPayload;
88
+ [BasedErrorCode.requiredFieldNotDefined]: BasedParseErrorPayload;
89
+ [BasedErrorCode.languageNotSupported]: BasedParseErrorPayload;
90
+ [BasedErrorCode.invalidJSON]: BasedParseErrorPayload;
91
+ [BasedErrorCode.noLanguageFound]: BasedParseErrorPayload;
92
+ [BasedErrorCode.cannotDeleteNodeFromModify]: BasedParseErrorPayload;
93
+ [BasedErrorCode.nestedModifyObjectNotAllowed]: BasedParseErrorPayload;
94
+ [BasedErrorCode.infinityNotSupported]: BasedParseErrorPayload;
95
+ [BasedErrorCode.invalidSchemaFormat]: BasedParseErrorPayload;
96
+ [BasedErrorCode.invalidProperty]: BasedParseErrorPayload;
97
+ [BasedErrorCode.FunctionError]: BasedFunctionError;
98
+ [BasedErrorCode.AuthorizeFunctionError]: BasedFunctionError;
99
+ [BasedErrorCode.NoOservableCacheAvailable]: {
100
+ observableId: number;
101
+ route: BasedRoute;
102
+ };
103
+ [BasedErrorCode.ObservableFunctionError]: BasedFunctionError;
104
+ [BasedErrorCode.ObserveCallbackError]: {
105
+ err: Error;
106
+ observableId: number;
107
+ route: BasedRoute;
108
+ };
109
+ [BasedErrorCode.FunctionNotFound]: BasedErrorPayload;
110
+ [BasedErrorCode.FunctionIsNotObservable]: BasedErrorPayload;
111
+ [BasedErrorCode.FunctionIsObservable]: BasedErrorPayload;
112
+ [BasedErrorCode.FunctionIsStream]: BasedErrorPayload;
113
+ [BasedErrorCode.CannotStreamToObservableFunction]: BasedErrorPayload;
114
+ [BasedErrorCode.FunctionIsWrongType]: BasedErrorPayload;
115
+ [BasedErrorCode.AuthorizeRejectedError]: BasedErrorPayload;
116
+ [BasedErrorCode.InvalidPayload]: BasedErrorPayload;
117
+ [BasedErrorCode.PayloadTooLarge]: BasedErrorPayload;
118
+ [BasedErrorCode.ChunkTooLarge]: BasedRoute;
119
+ [BasedErrorCode.UnsupportedContentEncoding]: BasedRoute;
120
+ [BasedErrorCode.NoBinaryProtocol]: {
121
+ buffer: ArrayBuffer;
122
+ };
123
+ [BasedErrorCode.LengthRequired]: BasedRoute;
124
+ [BasedErrorCode.MethodNotAllowed]: BasedRoute;
125
+ [BasedErrorCode.RateLimit]: {};
126
+ [BasedErrorCode.MissingAuthStateProtocolHeader]: {};
127
+ [BasedErrorCode.IncorrectAccessKey]: {};
128
+ [BasedErrorCode.Block]: {};
129
+ };
130
+ export type ErrorHandler<T extends BasedErrorCode> = {
131
+ statusCode?: number;
132
+ statusMessage?: string;
133
+ message: (payload: ErrorPayload[T]) => string;
134
+ };
135
+ export type BasedErrorData<T extends BasedErrorCode = BasedErrorCode> = {
136
+ route?: BasedRoute;
137
+ message: string;
138
+ code: T;
139
+ statusCode?: number;
140
+ statusMessage?: string;
141
+ requestId?: number;
142
+ observableId?: number;
143
+ channelId?: number;
144
+ };
145
+ type ErrorType = {
146
+ [K in BasedErrorCode]: ErrorHandler<K>;
147
+ };
148
+ export declare const errorTypeHandlers: ErrorType;
149
+ export declare const EMPTY_ROUTE: BasedRoute;
150
+ export declare function createErrorData<T extends BasedErrorCode>(code: T, payload: ErrorPayload[T]): {
151
+ statusCode: number;
152
+ statusMessage: string;
153
+ route: {
154
+ name: string;
155
+ path: string;
156
+ type: "function" | "channel" | "query" | "stream" | "app" | "job";
157
+ };
158
+ code: T;
159
+ message: string;
160
+ };
161
+ export declare class BasedError extends Error {
162
+ statusMessage?: string;
163
+ code?: BasedErrorCode;
164
+ }
165
+ export declare const convertDataToBasedError: (payload: BasedErrorData, stack?: string) => BasedError;
166
+ export {};
@@ -0,0 +1,330 @@
1
+ import { isAnyBasedRoute } from '@based/functions';
2
+ export var BasedErrorCode;
3
+ (function (BasedErrorCode) {
4
+ // Parse Errors
5
+ BasedErrorCode[BasedErrorCode["incorrectFieldType"] = 1000] = "incorrectFieldType";
6
+ BasedErrorCode[BasedErrorCode["incorrectNodeType"] = 1001] = "incorrectNodeType";
7
+ BasedErrorCode[BasedErrorCode["exceedsMaximum"] = 1002] = "exceedsMaximum";
8
+ BasedErrorCode[BasedErrorCode["subceedsMinimum"] = 1003] = "subceedsMinimum";
9
+ BasedErrorCode[BasedErrorCode["fieldDoesNotExist"] = 1004] = "fieldDoesNotExist";
10
+ BasedErrorCode[BasedErrorCode["incorrectFormat"] = 1005] = "incorrectFormat";
11
+ BasedErrorCode[BasedErrorCode["referenceIsIncorrectType"] = 1006] = "referenceIsIncorrectType";
12
+ BasedErrorCode[BasedErrorCode["valueAndDefault"] = 1007] = "valueAndDefault";
13
+ BasedErrorCode[BasedErrorCode["defaultNotSupported"] = 1008] = "defaultNotSupported";
14
+ BasedErrorCode[BasedErrorCode["multipleOperationsNotAllowed"] = 1009] = "multipleOperationsNotAllowed";
15
+ BasedErrorCode[BasedErrorCode["requiredFieldNotDefined"] = 1010] = "requiredFieldNotDefined";
16
+ BasedErrorCode[BasedErrorCode["languageNotSupported"] = 1011] = "languageNotSupported";
17
+ BasedErrorCode[BasedErrorCode["invalidJSON"] = 1012] = "invalidJSON";
18
+ BasedErrorCode[BasedErrorCode["noLanguageFound"] = 1013] = "noLanguageFound";
19
+ BasedErrorCode[BasedErrorCode["cannotDeleteNodeFromModify"] = 1014] = "cannotDeleteNodeFromModify";
20
+ BasedErrorCode[BasedErrorCode["nestedModifyObjectNotAllowed"] = 1015] = "nestedModifyObjectNotAllowed";
21
+ BasedErrorCode[BasedErrorCode["infinityNotSupported"] = 1016] = "infinityNotSupported";
22
+ BasedErrorCode[BasedErrorCode["invalidSchemaFormat"] = 1017] = "invalidSchemaFormat";
23
+ BasedErrorCode[BasedErrorCode["invalidProperty"] = 1018] = "invalidProperty";
24
+ // client/server errors
25
+ BasedErrorCode[BasedErrorCode["FunctionError"] = 50001] = "FunctionError";
26
+ BasedErrorCode[BasedErrorCode["AuthorizeFunctionError"] = 50002] = "AuthorizeFunctionError";
27
+ BasedErrorCode[BasedErrorCode["NoOservableCacheAvailable"] = 50003] = "NoOservableCacheAvailable";
28
+ BasedErrorCode[BasedErrorCode["ObservableFunctionError"] = 50004] = "ObservableFunctionError";
29
+ BasedErrorCode[BasedErrorCode["ObserveCallbackError"] = 50005] = "ObserveCallbackError";
30
+ BasedErrorCode[BasedErrorCode["FunctionNotFound"] = 40401] = "FunctionNotFound";
31
+ BasedErrorCode[BasedErrorCode["FunctionIsNotObservable"] = 40402] = "FunctionIsNotObservable";
32
+ BasedErrorCode[BasedErrorCode["FunctionIsObservable"] = 40403] = "FunctionIsObservable";
33
+ BasedErrorCode[BasedErrorCode["FunctionIsStream"] = 40404] = "FunctionIsStream";
34
+ BasedErrorCode[BasedErrorCode["CannotStreamToObservableFunction"] = 40405] = "CannotStreamToObservableFunction";
35
+ BasedErrorCode[BasedErrorCode["FunctionIsWrongType"] = 40406] = "FunctionIsWrongType";
36
+ BasedErrorCode[BasedErrorCode["AuthorizeRejectedError"] = 40301] = "AuthorizeRejectedError";
37
+ BasedErrorCode[BasedErrorCode["InvalidPayload"] = 40001] = "InvalidPayload";
38
+ BasedErrorCode[BasedErrorCode["PayloadTooLarge"] = 40002] = "PayloadTooLarge";
39
+ BasedErrorCode[BasedErrorCode["ChunkTooLarge"] = 40003] = "ChunkTooLarge";
40
+ BasedErrorCode[BasedErrorCode["UnsupportedContentEncoding"] = 40004] = "UnsupportedContentEncoding";
41
+ BasedErrorCode[BasedErrorCode["NoBinaryProtocol"] = 40005] = "NoBinaryProtocol";
42
+ BasedErrorCode[BasedErrorCode["LengthRequired"] = 41101] = "LengthRequired";
43
+ BasedErrorCode[BasedErrorCode["MethodNotAllowed"] = 40501] = "MethodNotAllowed";
44
+ BasedErrorCode[BasedErrorCode["RateLimit"] = 40029] = "RateLimit";
45
+ BasedErrorCode[BasedErrorCode["MissingAuthStateProtocolHeader"] = 40030] = "MissingAuthStateProtocolHeader";
46
+ BasedErrorCode[BasedErrorCode["IncorrectAccessKey"] = 40031] = "IncorrectAccessKey";
47
+ BasedErrorCode[BasedErrorCode["Block"] = 90001] = "Block";
48
+ // Mutation error
49
+ // PrefixAlreadyInUse = 2000,
50
+ })(BasedErrorCode = BasedErrorCode || (BasedErrorCode = {}));
51
+ const addName = (payload) => {
52
+ return payload.name ? `[${payload.name}] ` : '';
53
+ };
54
+ export const errorTypeHandlers = {
55
+ // Parse errors
56
+ [BasedErrorCode.incorrectFieldType]: {
57
+ message: (payload) => `[${payload.path.join('.')}] Incorrect field type.`
58
+ },
59
+ [BasedErrorCode.incorrectNodeType]: {
60
+ message: (payload) => `[${payload.path.join('.')}] Incorrect node type.`
61
+ },
62
+ [BasedErrorCode.exceedsMaximum]: {
63
+ message: (payload) => `[${payload.path.join('.')}] Exceeds maximum property.`
64
+ },
65
+ [BasedErrorCode.subceedsMinimum]: {
66
+ message: (payload) => `[${payload.path.join('.')}] Subceeds minimum property.`
67
+ },
68
+ [BasedErrorCode.fieldDoesNotExist]: {
69
+ message: (payload) => `[${payload.path.join('.')}] Field does not exist.`
70
+ },
71
+ [BasedErrorCode.incorrectFormat]: {
72
+ message: (payload) => `[${payload.path.join('.')}] Incorrect format.`
73
+ },
74
+ [BasedErrorCode.referenceIsIncorrectType]: {
75
+ message: (payload) => `[${payload.path.join('.')}] Reference is from incorrect type.`
76
+ },
77
+ [BasedErrorCode.valueAndDefault]: {
78
+ message: (payload) => `[${payload.path.join('.')}] Value and $default are being used at the same time.`
79
+ },
80
+ [BasedErrorCode.defaultNotSupported]: {
81
+ message: (payload) => `[${payload.path.join('.')}] $default is not suported.`
82
+ },
83
+ [BasedErrorCode.multipleOperationsNotAllowed]: {
84
+ message: (payload) => `[${payload.path.join('.')}] Multiple operations are not allowed here.`
85
+ },
86
+ [BasedErrorCode.requiredFieldNotDefined]: {
87
+ message: (payload) => `[${payload.path.join('.')}] Required field is not defined.`
88
+ },
89
+ [BasedErrorCode.languageNotSupported]: {
90
+ message: (payload) => `[${payload.path.join('.')}] Language not supported.`
91
+ },
92
+ [BasedErrorCode.invalidJSON]: {
93
+ message: (payload) => `[${payload.path.join('.')}] Invalid JSON.`
94
+ },
95
+ [BasedErrorCode.noLanguageFound]: {
96
+ message: (payload) => `[${payload.path.join('.')}] No language found.`
97
+ },
98
+ [BasedErrorCode.cannotDeleteNodeFromModify]: {
99
+ message: (payload) => `[${payload.path.join('.')}] Cannot delete node from modify.`
100
+ },
101
+ [BasedErrorCode.nestedModifyObjectNotAllowed]: {
102
+ message: (payload) => `[${payload.path.join('.')}] Nested modify object not allowed.`
103
+ },
104
+ [BasedErrorCode.infinityNotSupported]: {
105
+ message: (payload) => `[${payload.path.join('.')}] Infinity not supported.`
106
+ },
107
+ [BasedErrorCode.invalidSchemaFormat]: {
108
+ message: (payload) => `[${payload.path.join('.')}] Invalid schema format.`
109
+ },
110
+ [BasedErrorCode.invalidProperty]: {
111
+ message: (payload) => `[${payload.path.join('.')}] Invalid property.`
112
+ },
113
+ [BasedErrorCode.FunctionError]: {
114
+ statusCode: 500,
115
+ statusMessage: 'Internal Server Error',
116
+ message: (payload) => {
117
+ if (typeof payload.err === 'string' || !payload.err.message) {
118
+ return `[${payload.route.name}] ${JSON.stringify(payload.err)}`;
119
+ }
120
+ return (addName(payload.route) +
121
+ `${payload.err.name && payload.err.name !== 'Error'
122
+ ? `[${payload.err.name}] `
123
+ : ''}${payload.err.message || ''}.`);
124
+ },
125
+ },
126
+ [BasedErrorCode.AuthorizeFunctionError]: {
127
+ statusCode: 403,
128
+ statusMessage: 'Forbidden',
129
+ message: (payload) => {
130
+ if (typeof payload.err === 'string' || !payload.err.message) {
131
+ return `[${payload.route.name}] ${JSON.stringify(payload.err)}`;
132
+ }
133
+ return (addName(payload.route) +
134
+ `${payload.err.name && payload.err.name !== 'Error'
135
+ ? `[${payload.err.name}] `
136
+ : ''}${payload.err.message || ''}.`);
137
+ },
138
+ },
139
+ [BasedErrorCode.NoOservableCacheAvailable]: {
140
+ statusCode: 500,
141
+ statusMessage: 'Internal Server Error',
142
+ message: (payload) => addName(payload.route) +
143
+ `No observable cache available${payload.route.name} - ${payload.observableId}.`,
144
+ },
145
+ [BasedErrorCode.ObservableFunctionError]: {
146
+ statusCode: 500,
147
+ statusMessage: 'Internal Server Error',
148
+ message: (payload) => {
149
+ if (typeof payload.err === 'string' || !payload.err.message) {
150
+ return `[${payload.route.name} (observable)] ${JSON.stringify(payload.err)}.`;
151
+ }
152
+ return (addName(payload.route) +
153
+ `${payload.err.name && payload.err.name !== 'Error'
154
+ ? `[${payload.err.name}] `
155
+ : ''}${payload.err.message || ''}.`);
156
+ },
157
+ },
158
+ [BasedErrorCode.ObserveCallbackError]: {
159
+ statusCode: 500,
160
+ statusMessage: 'Internal Server Error',
161
+ message: () => {
162
+ return 'Error in server side observer.';
163
+ },
164
+ },
165
+ [BasedErrorCode.FunctionNotFound]: {
166
+ statusCode: 404,
167
+ statusMessage: 'Not Found',
168
+ message: (payload) => {
169
+ return (addName(payload.route) +
170
+ `Function not found${payload.route.path ? ` path '${payload.route.path}'` : ''}.`);
171
+ },
172
+ },
173
+ [BasedErrorCode.FunctionIsNotObservable]: {
174
+ statusCode: 400,
175
+ statusMessage: 'Incorrect Protocol',
176
+ message: (payload) => {
177
+ return addName(payload.route) + 'Target function is not observable.';
178
+ },
179
+ },
180
+ [BasedErrorCode.FunctionIsObservable]: {
181
+ statusCode: 400,
182
+ statusMessage: 'Incorrect Protocol',
183
+ message: (payload) => {
184
+ return addName(payload.route) + 'Target function is observable.';
185
+ },
186
+ },
187
+ [BasedErrorCode.FunctionIsStream]: {
188
+ statusCode: 400,
189
+ statusMessage: 'Incorrect Protocol',
190
+ message: (payload) => {
191
+ return addName(payload.route) + 'Target function is stream.';
192
+ },
193
+ },
194
+ [BasedErrorCode.CannotStreamToObservableFunction]: {
195
+ statusCode: 400,
196
+ statusMessage: 'Incorrect Protocol',
197
+ message: (payload) => {
198
+ return addName(payload.route) + 'Cannot stream to observable function.';
199
+ },
200
+ },
201
+ [BasedErrorCode.FunctionIsWrongType]: {
202
+ statusCode: 400,
203
+ statusMessage: 'Incorrect Protocol',
204
+ message: (payload) => {
205
+ return addName(payload.route) + 'Target function is of wrong type.';
206
+ },
207
+ },
208
+ [BasedErrorCode.AuthorizeRejectedError]: {
209
+ statusCode: 403,
210
+ statusMessage: 'Forbidden',
211
+ message: (payload) => addName(payload.route) + `Authorize rejected access.`,
212
+ },
213
+ [BasedErrorCode.InvalidPayload]: {
214
+ statusCode: 400,
215
+ statusMessage: 'Bad Request',
216
+ message: (payload) => addName(payload.route) + 'Invalid payload.',
217
+ },
218
+ [BasedErrorCode.PayloadTooLarge]: {
219
+ statusCode: 413,
220
+ statusMessage: 'Payload Too Large',
221
+ message: (payload) => addName(payload.route) + ' PayloadTooLarge.',
222
+ },
223
+ [BasedErrorCode.ChunkTooLarge]: {
224
+ statusCode: 413,
225
+ statusMessage: 'Payload Too Large',
226
+ message: (payload) => addName(payload) + 'ChunkTooLarge ' + payload.name + '.',
227
+ },
228
+ [BasedErrorCode.UnsupportedContentEncoding]: {
229
+ statusCode: 400,
230
+ statusMessage: 'Incorrect content encoding',
231
+ message: (payload) => addName(payload) + 'Incorrect content encoding.',
232
+ },
233
+ [BasedErrorCode.NoBinaryProtocol]: {
234
+ statusCode: 400,
235
+ statusMessage: 'Protocol mismatch',
236
+ message: () => 'Please upgrade to the latest based client.',
237
+ },
238
+ [BasedErrorCode.LengthRequired]: {
239
+ statusCode: 411,
240
+ statusMessage: 'Length Required',
241
+ message: (payload) => addName(payload) + 'Length Required.',
242
+ },
243
+ [BasedErrorCode.MethodNotAllowed]: {
244
+ statusCode: 405,
245
+ statusMessage: 'Method Not Allowed',
246
+ message: (payload) => addName(payload) + 'Method Not Allowed.',
247
+ },
248
+ [BasedErrorCode.RateLimit]: {
249
+ statusCode: 429,
250
+ statusMessage: 'Rate limit',
251
+ message: () => 'Rate limit.',
252
+ },
253
+ [BasedErrorCode.MissingAuthStateProtocolHeader]: {
254
+ statusCode: 500,
255
+ statusMessage: 'Internal Server Error',
256
+ message: () => '',
257
+ },
258
+ [BasedErrorCode.IncorrectAccessKey]: {
259
+ statusCode: 429,
260
+ statusMessage: 'Rate limit',
261
+ message: () => 'Rate limit.',
262
+ },
263
+ [BasedErrorCode.Block]: {
264
+ statusCode: 429,
265
+ statusMessage: 'Blocked ip',
266
+ message: () => 'Blocked ip.',
267
+ },
268
+ };
269
+ export const EMPTY_ROUTE = {
270
+ name: 'no-route',
271
+ path: '',
272
+ type: 'function',
273
+ };
274
+ function isServerError(payload) {
275
+ return payload.route !== undefined;
276
+ }
277
+ export function createErrorData(code, payload) {
278
+ const type = errorTypeHandlers[code];
279
+ const route = !payload
280
+ ? EMPTY_ROUTE
281
+ : isAnyBasedRoute(payload)
282
+ ? payload
283
+ : 'route' in payload
284
+ ? payload.route
285
+ : EMPTY_ROUTE;
286
+ return {
287
+ code,
288
+ message: type.message(payload),
289
+ ...(isServerError(payload) ? {
290
+ statusCode: type.statusCode,
291
+ statusMessage: type.statusMessage,
292
+ route: {
293
+ name: route.name,
294
+ path: route.path,
295
+ type: route.type,
296
+ },
297
+ } : null)
298
+ };
299
+ }
300
+ export class BasedError extends Error {
301
+ statusMessage;
302
+ code;
303
+ }
304
+ export const convertDataToBasedError = (payload, stack) => {
305
+ if (!payload || typeof payload !== 'object') {
306
+ const err = new BasedError(`Payload: ${payload}`);
307
+ // err.code = BasedErrorCode.FunctionError
308
+ err.name = 'Invalid returned payload';
309
+ return err;
310
+ }
311
+ const { message, code } = payload;
312
+ const msg = message
313
+ ? message[0] === '['
314
+ ? message
315
+ : `[${BasedErrorCode[code]}] ` + message
316
+ : !code
317
+ ? JSON.stringify(payload, null, 2)
318
+ : 'Cannot read error msg';
319
+ const error = new BasedError(msg);
320
+ error.stack = stack ? msg + ' ' + stack : msg;
321
+ error.name = BasedErrorCode[code];
322
+ error.code = code;
323
+ return error;
324
+ };
325
+ // export const errorDescriptions: {
326
+ // [BasedErrorCode.PrefixAlreadyInUse]: (payload) =>
327
+ // `Prefix${payload?.prefix ? ` ${payload.prefix}` : ''} is already in use.`,
328
+ // }
329
+ // export const makeException = (error: BasedError) => new BasedException(error)
330
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAE9D,MAAM,CAAN,IAAY,cAiDX;AAjDD,WAAY,cAAc;IACxB,eAAe;IACf,kFAAyB,CAAA;IACzB,gFAAwB,CAAA;IACxB,0EAAqB,CAAA;IACrB,4EAAsB,CAAA;IACtB,gFAAwB,CAAA;IACxB,4EAAsB,CAAA;IACtB,8FAA+B,CAAA;IAC/B,4EAAsB,CAA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AM,CAAC,MAAM,uBAAuB,GAAG,CACrC,OAAuB,EACvB,KAAc,EACF,EAAE;IACd,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QAC3C,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,YAAY,OAAO,EAAE,CAAC,CAAA;QACjD,0CAA0C;QAC1C,GAAG,CAAC,IAAI,GAAG,0BAA0B,CAAA;QACrC,OAAO,GAAG,CAAA;KACX;IACD,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,OAAO,CAAA;IACjC,MAAM,GAAG,GAAG,OAAO;QACjB,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG;YAClB,CAAC,CAAC,OAAO;YACT,CAAC,CAAC,IAAI,cAAc,CAAC,IAAI,CAAC,IAAI,GAAG,OAAO;QAC1C,CAAC,CAAC,CAAC,IAAI;YACL,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;YAClC,CAAC,CAAC,uBAAuB,CAAA;IAC7B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAA;IACjC,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,CAAA;IAC7C,KAAK,CAAC,IAAI,GAAG,cAAc,CAAC,IAAI,CAAC,CAAA;IACjC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAA;IACjB,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED,oCAAoC;AACpC,sDAAsD;AACtD,iFAAiF;AACjF,IAAI;AACJ,gFAAgF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/errors",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",
@@ -18,6 +18,11 @@
18
18
  "./dist/test/**/*.js"
19
19
  ]
20
20
  },
21
+ "files": [
22
+ "dist/src",
23
+ "README.md",
24
+ "package.json"
25
+ ],
21
26
  "dependencies": {},
22
27
  "devDependencies": {
23
28
  "ts-node": "10.9.1",
package/src/index.ts DELETED
@@ -1,482 +0,0 @@
1
- import { BasedRoute, isAnyBasedRoute } from '@based/functions'
2
-
3
- export enum BasedErrorCode {
4
- // Parse Errors
5
- incorrectFieldType = 1000,
6
- incorrectNodeType = 1001,
7
- exceedsMaximum = 1002,
8
- subceedsMinimum = 1003,
9
- fieldDoesNotExist = 1004,
10
- incorrectFormat = 1005,
11
- referenceIsIncorrectType = 1006,
12
- valueAndDefault = 1007,
13
- defaultNotSupported = 1008,
14
- multipleOperationsNotAllowed = 1009,
15
- requiredFieldNotDefined = 1010,
16
- languageNotSupported = 1011,
17
- invalidJSON = 1012,
18
- noLanguageFound = 1013,
19
- cannotDeleteNodeFromModify = 1014,
20
- nestedModifyObjectNotAllowed = 1015,
21
- infinityNotSupported = 1016,
22
- invalidSchemaFormat = 1017,
23
- invalidProperty = 1018,
24
-
25
- // client/server errors
26
- FunctionError = 50001,
27
- AuthorizeFunctionError = 50002,
28
- NoOservableCacheAvailable = 50003,
29
- ObservableFunctionError = 50004,
30
- ObserveCallbackError = 50005,
31
- FunctionNotFound = 40401,
32
- FunctionIsNotObservable = 40402,
33
- FunctionIsObservable = 40403,
34
- FunctionIsStream = 40404,
35
- CannotStreamToObservableFunction = 40405,
36
- FunctionIsWrongType = 40406, // was 40402 in server
37
- AuthorizeRejectedError = 40301,
38
- InvalidPayload = 40001,
39
- PayloadTooLarge = 40002,
40
- ChunkTooLarge = 40003,
41
- UnsupportedContentEncoding = 40004,
42
- NoBinaryProtocol = 40005,
43
- LengthRequired = 41101,
44
- MethodNotAllowed = 40501,
45
- RateLimit = 40029,
46
- MissingAuthStateProtocolHeader = 40030,
47
- IncorrectAccessKey = 40031,
48
- Block = 90001,
49
-
50
- // Mutation error
51
- // PrefixAlreadyInUse = 2000,
52
- }
53
-
54
- type BasedParseErrorPayload = {
55
- path: string[]
56
- }
57
-
58
- type FunctionErrorProps = {
59
- err: Error | string
60
- requestId?: number
61
- route: BasedRoute
62
- }
63
-
64
- type ObservableFunctionErrorProps = {
65
- observableId: number
66
- err: Error | string
67
- route: BasedRoute
68
- }
69
-
70
- type ChannelFunctionErrorProps = {
71
- channelId: number
72
- err: Error | string
73
- route: BasedRoute
74
- }
75
-
76
- export type BasedErrorPayload =
77
- | {
78
- observableId: number
79
- route: BasedRoute
80
- }
81
- | {
82
- requestId: number
83
- route: BasedRoute
84
- }
85
- | {
86
- channelId: number
87
- route: BasedRoute
88
- }
89
- | { route: BasedRoute }
90
-
91
- type BasedFunctionError =
92
- | FunctionErrorProps
93
- | ObservableFunctionErrorProps
94
- | ChannelFunctionErrorProps
95
-
96
- export type ErrorPayload = {
97
- [BasedErrorCode.incorrectFieldType]: BasedParseErrorPayload
98
- [BasedErrorCode.incorrectNodeType]: BasedParseErrorPayload
99
- [BasedErrorCode.exceedsMaximum]: BasedParseErrorPayload
100
- [BasedErrorCode.subceedsMinimum]: BasedParseErrorPayload
101
- [BasedErrorCode.fieldDoesNotExist]: BasedParseErrorPayload
102
- [BasedErrorCode.incorrectFormat]: BasedParseErrorPayload,
103
- [BasedErrorCode.referenceIsIncorrectType]: BasedParseErrorPayload,
104
- [BasedErrorCode.valueAndDefault]: BasedParseErrorPayload,
105
- [BasedErrorCode.defaultNotSupported]: BasedParseErrorPayload,
106
- [BasedErrorCode.multipleOperationsNotAllowed]: BasedParseErrorPayload,
107
- [BasedErrorCode.requiredFieldNotDefined]: BasedParseErrorPayload,
108
- [BasedErrorCode.languageNotSupported]: BasedParseErrorPayload,
109
- [BasedErrorCode.invalidJSON]: BasedParseErrorPayload,
110
- [BasedErrorCode.noLanguageFound]: BasedParseErrorPayload,
111
- [BasedErrorCode.cannotDeleteNodeFromModify]: BasedParseErrorPayload,
112
- [BasedErrorCode.nestedModifyObjectNotAllowed]: BasedParseErrorPayload,
113
- [BasedErrorCode.infinityNotSupported]: BasedParseErrorPayload,
114
- [BasedErrorCode.invalidSchemaFormat]: BasedParseErrorPayload,
115
- [BasedErrorCode.invalidProperty]: BasedParseErrorPayload,
116
-
117
- [BasedErrorCode.FunctionError]: BasedFunctionError
118
- [BasedErrorCode.AuthorizeFunctionError]: BasedFunctionError
119
- [BasedErrorCode.NoOservableCacheAvailable]: {
120
- observableId: number
121
- route: BasedRoute
122
- }
123
- [BasedErrorCode.ObservableFunctionError]: BasedFunctionError
124
- [BasedErrorCode.ObserveCallbackError]: {
125
- err: Error
126
- observableId: number
127
- route: BasedRoute
128
- }
129
- [BasedErrorCode.FunctionNotFound]: BasedErrorPayload
130
- [BasedErrorCode.FunctionIsNotObservable]: BasedErrorPayload // FunctionIsWrongType?
131
- [BasedErrorCode.FunctionIsObservable]: BasedErrorPayload // FunctionIsWrongType?
132
- [BasedErrorCode.FunctionIsStream]: BasedErrorPayload // FunctionIsWrongType?
133
- [BasedErrorCode.CannotStreamToObservableFunction]: BasedErrorPayload
134
- [BasedErrorCode.FunctionIsWrongType]: BasedErrorPayload
135
- [BasedErrorCode.AuthorizeRejectedError]: BasedErrorPayload
136
- [BasedErrorCode.InvalidPayload]: BasedErrorPayload
137
- [BasedErrorCode.PayloadTooLarge]: BasedErrorPayload
138
- [BasedErrorCode.ChunkTooLarge]: BasedRoute
139
- [BasedErrorCode.UnsupportedContentEncoding]: BasedRoute
140
- [BasedErrorCode.NoBinaryProtocol]: { buffer: ArrayBuffer }
141
- [BasedErrorCode.LengthRequired]: BasedRoute
142
- [BasedErrorCode.MethodNotAllowed]: BasedRoute
143
- [BasedErrorCode.RateLimit]: {}
144
- [BasedErrorCode.MissingAuthStateProtocolHeader]: {}
145
- [BasedErrorCode.IncorrectAccessKey]: {}
146
- [BasedErrorCode.Block]: {}
147
- }
148
-
149
- export type ErrorHandler<T extends BasedErrorCode> = {
150
- statusCode?: number
151
- statusMessage?: string
152
- message: (payload: ErrorPayload[T]) => string
153
- }
154
-
155
- export type BasedErrorData<T extends BasedErrorCode = BasedErrorCode> = {
156
- route?: BasedRoute
157
- message: string
158
- code: T
159
- statusCode?: number
160
- statusMessage?: string
161
- requestId?: number
162
- observableId?: number
163
- channelId?: number
164
- }
165
-
166
- const addName = (
167
- payload: { name: string } & { [key: string]: unknown }
168
- ): string => {
169
- return payload.name ? `[${payload.name}] ` : ''
170
- }
171
-
172
- type ErrorType = {
173
- [K in BasedErrorCode]: ErrorHandler<K>
174
- }
175
-
176
- export const errorTypeHandlers: ErrorType = {
177
- // Parse errors
178
- [BasedErrorCode.incorrectFieldType]: {
179
- message: (payload) => `[${payload.path.join('.')}] Incorrect field type.`
180
- },
181
- [BasedErrorCode.incorrectNodeType]: {
182
- message: (payload) => `[${payload.path.join('.')}] Incorrect node type.`
183
- },
184
- [BasedErrorCode.exceedsMaximum]: {
185
- message: (payload) => `[${payload.path.join('.')}] Exceeds maximum property.`
186
- },
187
- [BasedErrorCode.subceedsMinimum]: {
188
- message: (payload) => `[${payload.path.join('.')}] Subceeds minimum property.`
189
- },
190
- [BasedErrorCode.fieldDoesNotExist]: {
191
- message: (payload) => `[${payload.path.join('.')}] Field does not exist.`
192
- },
193
- [BasedErrorCode.incorrectFormat]: {
194
- message: (payload) => `[${payload.path.join('.')}] Incorrect format.`
195
- },
196
- [BasedErrorCode.referenceIsIncorrectType]: {
197
- message: (payload) => `[${payload.path.join('.')}] Reference is from incorrect type.`
198
- },
199
- [BasedErrorCode.valueAndDefault]: {
200
- message: (payload) => `[${payload.path.join('.')}] Value and $default are being used at the same time.`
201
- },
202
- [BasedErrorCode.defaultNotSupported]: {
203
- message: (payload) => `[${payload.path.join('.')}] $default is not suported.`
204
- },
205
- [BasedErrorCode.multipleOperationsNotAllowed]: {
206
- message: (payload) => `[${payload.path.join('.')}] Multiple operations are not allowed here.`
207
- },
208
- [BasedErrorCode.requiredFieldNotDefined]: {
209
- message: (payload) => `[${payload.path.join('.')}] Required field is not defined.`
210
- },
211
- [BasedErrorCode.languageNotSupported]: {
212
- message: (payload) => `[${payload.path.join('.')}] Language not supported.`
213
- },
214
- [BasedErrorCode.invalidJSON]: {
215
- message: (payload) => `[${payload.path.join('.')}] Invalid JSON.`
216
- },
217
- [BasedErrorCode.noLanguageFound]: {
218
- message: (payload) => `[${payload.path.join('.')}] No language found.`
219
- },
220
- [BasedErrorCode.cannotDeleteNodeFromModify]: {
221
- message: (payload) => `[${payload.path.join('.')}] Cannot delete node from modify.`
222
- },
223
- [BasedErrorCode.nestedModifyObjectNotAllowed]: {
224
- message: (payload) => `[${payload.path.join('.')}] Nested modify object not allowed.`
225
- },
226
- [BasedErrorCode.infinityNotSupported]: {
227
- message: (payload) => `[${payload.path.join('.')}] Infinity not supported.`
228
- },
229
- [BasedErrorCode.invalidSchemaFormat]: {
230
- message: (payload) => `[${payload.path.join('.')}] Invalid schema format.`
231
- },
232
- [BasedErrorCode.invalidProperty]: {
233
- message: (payload) => `[${payload.path.join('.')}] Invalid property.`
234
- },
235
-
236
- [BasedErrorCode.FunctionError]: {
237
- statusCode: 500,
238
- statusMessage: 'Internal Server Error',
239
- message: (payload) => {
240
- if (typeof payload.err === 'string' || !payload.err.message) {
241
- return `[${payload.route.name}] ${JSON.stringify(payload.err)}`
242
- }
243
- return (
244
- addName(payload.route) +
245
- `${payload.err.name && payload.err.name !== 'Error'
246
- ? `[${payload.err.name}] `
247
- : ''
248
- }${payload.err.message || ''}.`
249
- )
250
- },
251
- },
252
- [BasedErrorCode.AuthorizeFunctionError]: {
253
- statusCode: 403,
254
- statusMessage: 'Forbidden',
255
- message: (payload) => {
256
- if (typeof payload.err === 'string' || !payload.err.message) {
257
- return `[${payload.route.name}] ${JSON.stringify(payload.err)}`
258
- }
259
- return (
260
- addName(payload.route) +
261
- `${payload.err.name && payload.err.name !== 'Error'
262
- ? `[${payload.err.name}] `
263
- : ''
264
- }${payload.err.message || ''}.`
265
- )
266
- },
267
- },
268
- [BasedErrorCode.NoOservableCacheAvailable]: {
269
- statusCode: 500,
270
- statusMessage: 'Internal Server Error',
271
- message: (
272
- payload: ErrorPayload[BasedErrorCode.NoOservableCacheAvailable]
273
- ) =>
274
- addName(payload.route) +
275
- `No observable cache available${payload.route.name} - ${payload.observableId}.`,
276
- },
277
- [BasedErrorCode.ObservableFunctionError]: {
278
- statusCode: 500,
279
- statusMessage: 'Internal Server Error',
280
- message: (payload) => {
281
- if (typeof payload.err === 'string' || !payload.err.message) {
282
- return `[${payload.route.name} (observable)] ${JSON.stringify(payload.err)}.`
283
- }
284
- return (
285
- addName(payload.route) +
286
- `${payload.err.name && payload.err.name !== 'Error'
287
- ? `[${payload.err.name}] `
288
- : ''
289
- }${payload.err.message || ''}.`
290
- )
291
- },
292
- },
293
- [BasedErrorCode.ObserveCallbackError]: {
294
- statusCode: 500,
295
- statusMessage: 'Internal Server Error',
296
- message: () => {
297
- return 'Error in server side observer.'
298
- },
299
- },
300
- [BasedErrorCode.FunctionNotFound]: {
301
- statusCode: 404,
302
- statusMessage: 'Not Found',
303
- message: (payload) => {
304
- return (
305
- addName(payload.route) +
306
- `Function not found${payload.route.path ? ` path '${payload.route.path}'` : ''
307
- }.`
308
- )
309
- },
310
- },
311
- [BasedErrorCode.FunctionIsNotObservable]: {
312
- statusCode: 400,
313
- statusMessage: 'Incorrect Protocol',
314
- message: (payload) => {
315
- return addName(payload.route) + 'Target function is not observable.'
316
- },
317
- },
318
- [BasedErrorCode.FunctionIsObservable]: {
319
- statusCode: 400,
320
- statusMessage: 'Incorrect Protocol',
321
- message: (payload) => {
322
- return addName(payload.route) + 'Target function is observable.'
323
- },
324
- },
325
- [BasedErrorCode.FunctionIsStream]: {
326
- statusCode: 400,
327
- statusMessage: 'Incorrect Protocol',
328
- message: (payload) => {
329
- return addName(payload.route) + 'Target function is stream.'
330
- },
331
- },
332
- [BasedErrorCode.CannotStreamToObservableFunction]: {
333
- statusCode: 400,
334
- statusMessage: 'Incorrect Protocol',
335
- message: (payload) => {
336
- return addName(payload.route) + 'Cannot stream to observable function.'
337
- },
338
- },
339
- [BasedErrorCode.FunctionIsWrongType]: {
340
- statusCode: 400,
341
- statusMessage: 'Incorrect Protocol',
342
- message: (payload) => {
343
- return addName(payload.route) + 'Target function is of wrong type.'
344
- },
345
- },
346
- [BasedErrorCode.AuthorizeRejectedError]: {
347
- statusCode: 403,
348
- statusMessage: 'Forbidden',
349
- message: (payload) => addName(payload.route) + `Authorize rejected access.`,
350
- },
351
- [BasedErrorCode.InvalidPayload]: {
352
- statusCode: 400,
353
- statusMessage: 'Bad Request',
354
- message: (payload) => addName(payload.route) + 'Invalid payload.',
355
- },
356
- [BasedErrorCode.PayloadTooLarge]: {
357
- statusCode: 413,
358
- statusMessage: 'Payload Too Large',
359
- message: (payload) => addName(payload.route) + ' PayloadTooLarge.',
360
- },
361
- [BasedErrorCode.ChunkTooLarge]: {
362
- statusCode: 413,
363
- statusMessage: 'Payload Too Large',
364
- message: (payload) => addName(payload) + 'ChunkTooLarge ' + payload.name + '.',
365
- },
366
- [BasedErrorCode.UnsupportedContentEncoding]: {
367
- statusCode: 400,
368
- statusMessage: 'Incorrect content encoding',
369
- message: (payload) => addName(payload) + 'Incorrect content encoding.',
370
- },
371
- [BasedErrorCode.NoBinaryProtocol]: {
372
- statusCode: 400,
373
- statusMessage: 'Protocol mismatch',
374
- message: () => 'Please upgrade to the latest based client.',
375
- },
376
- [BasedErrorCode.LengthRequired]: {
377
- statusCode: 411,
378
- statusMessage: 'Length Required',
379
- message: (payload) => addName(payload) + 'Length Required.',
380
- },
381
- [BasedErrorCode.MethodNotAllowed]: {
382
- statusCode: 405,
383
- statusMessage: 'Method Not Allowed',
384
- message: (payload) => addName(payload) + 'Method Not Allowed.',
385
- },
386
- [BasedErrorCode.RateLimit]: {
387
- statusCode: 429,
388
- statusMessage: 'Rate limit',
389
- message: () => 'Rate limit.',
390
- },
391
- [BasedErrorCode.MissingAuthStateProtocolHeader]: {
392
- statusCode: 500,
393
- statusMessage: 'Internal Server Error',
394
- message: () => '',
395
- },
396
- [BasedErrorCode.IncorrectAccessKey]: {
397
- statusCode: 429,
398
- statusMessage: 'Rate limit',
399
- message: () => 'Rate limit.',
400
- },
401
- [BasedErrorCode.Block]: {
402
- statusCode: 429,
403
- statusMessage: 'Blocked ip',
404
- message: () => 'Blocked ip.',
405
- },
406
- }
407
-
408
- export const EMPTY_ROUTE: BasedRoute = {
409
- name: 'no-route',
410
- path: '',
411
- type: 'function',
412
- }
413
-
414
- function isServerError(
415
- payload: {} | BasedErrorPayload | BasedParseErrorPayload
416
- ): payload is BasedErrorPayload {
417
- return (payload as BasedErrorPayload).route !== undefined
418
- }
419
-
420
- export function createErrorData<T extends BasedErrorCode>(
421
- code: T,
422
- payload: ErrorPayload[T]
423
- ) {
424
- const type = errorTypeHandlers[code]
425
- const route = !payload
426
- ? EMPTY_ROUTE
427
- : isAnyBasedRoute(payload)
428
- ? payload
429
- : 'route' in payload
430
- ? payload.route
431
- : EMPTY_ROUTE
432
-
433
- return {
434
- code,
435
- message: type.message(payload),
436
- ...(isServerError(payload) ? {
437
- statusCode: type.statusCode,
438
- statusMessage: type.statusMessage,
439
- route: {
440
- name: route.name,
441
- path: route.path,
442
- type: route.type,
443
- },
444
- } : null)
445
- }
446
- }
447
-
448
- export class BasedError extends Error {
449
- public statusMessage?: string
450
- public code?: BasedErrorCode
451
- }
452
-
453
- export const convertDataToBasedError = (
454
- payload: BasedErrorData,
455
- stack?: string
456
- ): BasedError => {
457
- if (!payload || typeof payload !== 'object') {
458
- const err = new BasedError(`Payload: ${payload}`)
459
- // err.code = BasedErrorCode.FunctionError
460
- err.name = 'Invalid returned payload'
461
- return err
462
- }
463
- const { message, code } = payload
464
- const msg = message
465
- ? message[0] === '['
466
- ? message
467
- : `[${BasedErrorCode[code]}] ` + message
468
- : !code
469
- ? JSON.stringify(payload, null, 2)
470
- : 'Cannot read error msg'
471
- const error = new BasedError(msg)
472
- error.stack = stack ? msg + ' ' + stack : msg
473
- error.name = BasedErrorCode[code]
474
- error.code = code
475
- return error
476
- }
477
-
478
- // export const errorDescriptions: {
479
- // [BasedErrorCode.PrefixAlreadyInUse]: (payload) =>
480
- // `Prefix${payload?.prefix ? ` ${payload.prefix}` : ''} is already in use.`,
481
- // }
482
- // export const makeException = (error: BasedError) => new BasedException(error)
package/test/index.ts DELETED
@@ -1,26 +0,0 @@
1
- import test from 'ava'
2
- import {
3
- BasedErrorCode,
4
- BasedErrorPayload,
5
- createErrorData,
6
- errorTypeHandlers
7
- } from '../src/index.js'
8
-
9
- test('createErrorData server error', (t) => {
10
- const payload: BasedErrorPayload = {
11
- observableId: 1234,
12
- route: { type: 'function', name: 'test' }
13
- }
14
- const result = createErrorData(BasedErrorCode.InvalidPayload, payload)
15
- t.is(result.code, BasedErrorCode.InvalidPayload)
16
- t.is(result.statusCode, errorTypeHandlers[BasedErrorCode.InvalidPayload].statusCode)
17
- t.is(result.statusMessage, errorTypeHandlers[BasedErrorCode.InvalidPayload].statusMessage)
18
- t.is(result.message, errorTypeHandlers[BasedErrorCode.InvalidPayload].message(payload))
19
- })
20
-
21
- test('createErrorData parse error', (t) => {
22
- const payload = { path: ['a', 'field'] }
23
- const result = createErrorData(BasedErrorCode.incorrectFieldType, payload)
24
- t.is(result.code, BasedErrorCode.incorrectFieldType)
25
- t.is(result.message, errorTypeHandlers[BasedErrorCode.incorrectFieldType].message(payload))
26
- })
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "extends": "@saulx/tsconfig/default.json",
3
- "compilerOptions": {
4
- "outDir": "dist",
5
- "esModuleInterop": true,
6
- "allowJs": true,
7
- "noPropertyAccessFromIndexSignature": false
8
- },
9
- "include": [
10
- "./src",
11
- "./test"
12
- ],
13
- "exclude": [
14
- "./dist/src",
15
- "./dist/test"
16
- ]
17
- }