@naturalcycles/js-lib 14.172.0 → 14.173.0

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.
Files changed (40) hide show
  1. package/dist/error/assert.d.ts +0 -4
  2. package/dist/error/assert.js +7 -14
  3. package/dist/error/error.util.d.ts +80 -1
  4. package/dist/error/error.util.js +117 -2
  5. package/dist/error/try.d.ts +0 -9
  6. package/dist/error/try.js +6 -19
  7. package/dist/http/fetcher.d.ts +1 -1
  8. package/dist/http/fetcher.js +3 -4
  9. package/dist/index.d.ts +0 -3
  10. package/dist/index.js +0 -3
  11. package/dist/promise/pTimeout.d.ts +1 -4
  12. package/dist/promise/pTimeout.js +2 -9
  13. package/dist/string/json.util.js +2 -2
  14. package/dist-esm/error/assert.js +1 -7
  15. package/dist-esm/error/error.util.js +110 -1
  16. package/dist-esm/error/try.js +1 -13
  17. package/dist-esm/http/fetcher.js +2 -3
  18. package/dist-esm/index.js +0 -3
  19. package/dist-esm/promise/pTimeout.js +1 -7
  20. package/dist-esm/string/json.util.js +1 -1
  21. package/package.json +1 -1
  22. package/src/error/assert.ts +1 -8
  23. package/src/error/error.util.ts +163 -1
  24. package/src/error/try.ts +1 -18
  25. package/src/http/fetcher.ts +8 -3
  26. package/src/index.ts +0 -3
  27. package/src/promise/pTimeout.ts +1 -8
  28. package/src/string/json.util.ts +1 -1
  29. package/dist/error/app.error.d.ts +0 -31
  30. package/dist/error/app.error.js +0 -57
  31. package/dist/error/httpRequestError.d.ts +0 -28
  32. package/dist/error/httpRequestError.js +0 -32
  33. package/dist/error/jsonParseError.d.ts +0 -11
  34. package/dist/error/jsonParseError.js +0 -14
  35. package/dist-esm/error/app.error.js +0 -53
  36. package/dist-esm/error/httpRequestError.js +0 -28
  37. package/dist-esm/error/jsonParseError.js +0 -10
  38. package/src/error/app.error.ts +0 -81
  39. package/src/error/httpRequestError.ts +0 -38
  40. package/src/error/jsonParseError.ts +0 -20
@@ -1,6 +1,6 @@
1
1
  import { _stringifyAny } from '../string/stringifyAny';
2
- import { AppError } from './app.error';
3
2
  import { _assertErrorClassOrRethrow } from './assert';
3
+ import { UnexpectedPassError } from './error.util';
4
4
  /**
5
5
  * Calls a function, returns a Tuple of [error, value].
6
6
  * Allows to write shorter code that avoids `try/catch`.
@@ -45,18 +45,6 @@ export async function pTry(promise, errorClass) {
45
45
  return [err, null];
46
46
  }
47
47
  }
48
- /**
49
- * It is thrown when Error was expected, but didn't happen
50
- * ("pass" happened instead).
51
- * "Pass" means "no error".
52
- */
53
- export class UnexpectedPassError extends AppError {
54
- constructor() {
55
- super('expected error was not thrown', {}, {
56
- name: 'UnexpectedPassError',
57
- });
58
- }
59
- }
60
48
  /**
61
49
  * Calls `fn`, expects is to throw, catches the expected error and returns.
62
50
  * If error was NOT thrown - throws UnexpectedPassError instead.
@@ -3,12 +3,11 @@
3
3
  var _a;
4
4
  import { isServerSide } from '../env';
5
5
  import { _assertErrorClassOrRethrow } from '../error/assert';
6
- import { _anyToError, _anyToErrorObject, _errorLikeToErrorObject } from '../error/error.util';
7
- import { HttpRequestError } from '../error/httpRequestError';
6
+ import { _anyToError, _anyToErrorObject, _errorLikeToErrorObject, HttpRequestError, TimeoutError, } from '../error/error.util';
8
7
  import { _clamp } from '../number/number.util';
9
8
  import { _filterNullishValues, _filterUndefinedValues, _mapKeys, _merge, _omit, _pick, } from '../object/object.util';
10
9
  import { pDelay } from '../promise/pDelay';
11
- import { pTimeout, TimeoutError } from '../promise/pTimeout';
10
+ import { pTimeout } from '../promise/pTimeout';
12
11
  import { _jsonParse, _jsonParseIfPossible } from '../string/json.util';
13
12
  import { _stringifyAny } from '../string/stringifyAny';
14
13
  import { _ms, _since } from '../time/time.util';
package/dist-esm/index.js CHANGED
@@ -14,16 +14,13 @@ export * from './decorators/memoFn';
14
14
  export * from './decorators/memoFnAsync';
15
15
  export * from './decorators/retry.decorator';
16
16
  export * from './decorators/timeout.decorator';
17
- export * from './error/app.error';
18
17
  export * from './error/assert';
19
18
  export * from './enum.util';
20
19
  export * from './error/error.model';
21
20
  export * from './error/error.util';
22
21
  export * from './error/errorMode';
23
- export * from './error/httpRequestError';
24
22
  export * from './error/try';
25
23
  export * from './error/tryCatch';
26
- export * from './error/jsonParseError';
27
24
  export * from './json-schema/from-data/generateJsonSchemaFromData';
28
25
  export * from './json-schema/jsonSchema.cnst';
29
26
  export * from './json-schema/jsonSchema.model';
@@ -1,10 +1,4 @@
1
- import { AppError } from '../error/app.error';
2
- import { _errorDataAppend } from '../error/error.util';
3
- export class TimeoutError extends AppError {
4
- constructor(message, data, opt) {
5
- super(message, data, Object.assign(Object.assign({}, opt), { name: 'TimeoutError' }));
6
- }
7
- }
1
+ import { _errorDataAppend, TimeoutError } from '../error/error.util';
8
2
  /**
9
3
  * Decorates a Function with a timeout.
10
4
  * Returns a decorated Function.
@@ -1,4 +1,4 @@
1
- import { JsonParseError } from '../error/jsonParseError';
1
+ import { JsonParseError } from '../error/error.util';
2
2
  // const possibleJsonStartTokens = ['{', '[', '"']
3
3
  const DETECT_JSON = /^\s*[{["\-\d]/;
4
4
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.172.0",
3
+ "version": "14.173.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -1,6 +1,5 @@
1
1
  import type { ErrorData, ErrorObject } from '..'
2
- import { _deepEquals, _isErrorObject, _stringifyAny, Class } from '..'
3
- import { AppError } from './app.error'
2
+ import { _deepEquals, _isErrorObject, _stringifyAny, AssertionError, Class } from '..'
4
3
 
5
4
  /**
6
5
  * Evaluates the `condition` (casts it to Boolean).
@@ -150,9 +149,3 @@ export function _assertTypeOf<T>(v: any, expectedType: string, message?: string)
150
149
  })
151
150
  }
152
151
  }
153
-
154
- export class AssertionError extends AppError {
155
- constructor(message: string, data?: ErrorData) {
156
- super(message, data, { name: 'AssertionError' })
157
- }
158
- }
@@ -6,7 +6,7 @@ import type {
6
6
  HttpRequestErrorData,
7
7
  ErrorLike,
8
8
  } from '..'
9
- import { AppError, _jsonParseIfPossible, _stringifyAny, _truncate } from '..'
9
+ import { _jsonParseIfPossible, _stringifyAny, _truncate, _truncateMiddle } from '..'
10
10
 
11
11
  /**
12
12
  * Useful to ensure that error in `catch (err) { ... }`
@@ -272,3 +272,165 @@ export function _errorDataAppend<ERR>(err: ERR, data?: ErrorData): ERR {
272
272
 
273
273
  return err
274
274
  }
275
+
276
+ /**
277
+ * Base class for all our (not system) errors.
278
+ *
279
+ * message - "technical" message. Frontend decides to show it or not.
280
+ * data - optional "any" payload.
281
+ * data.userFriendly - if present, will be displayed to the User as is.
282
+ *
283
+ * Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
284
+ */
285
+ export class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
286
+ data!: DATA_TYPE
287
+
288
+ /**
289
+ * `cause` here is normalized to be an ErrorObject
290
+ */
291
+ override cause?: ErrorObject
292
+
293
+ constructor(message: string, data = {} as DATA_TYPE, opt: AppErrorOptions = {}) {
294
+ super(message)
295
+ const { name = this.constructor.name, cause } = opt
296
+
297
+ Object.defineProperties(this, {
298
+ name: {
299
+ value: name,
300
+ configurable: true,
301
+ writable: true,
302
+ },
303
+ data: {
304
+ value: data,
305
+ writable: true,
306
+ configurable: true,
307
+ enumerable: false,
308
+ },
309
+ })
310
+
311
+ if (cause) {
312
+ Object.defineProperty(this, 'cause', {
313
+ value: _anyToErrorObject(cause),
314
+ writable: true,
315
+ configurable: true,
316
+ enumerable: true, // unlike standard - setting it to true for "visibility"
317
+ })
318
+ }
319
+
320
+ // this is to allow changing this.constuctor.name to a non-minified version
321
+ Object.defineProperty(this.constructor, 'name', {
322
+ value: name,
323
+ configurable: true,
324
+ writable: true,
325
+ })
326
+
327
+ // todo: check if it's needed at all!
328
+ // if (Error.captureStackTrace) {
329
+ // Error.captureStackTrace(this, this.constructor)
330
+ // } else {
331
+ // Object.defineProperty(this, 'stack', {
332
+ // value: new Error().stack, // eslint-disable-line unicorn/error-message
333
+ // writable: true,
334
+ // configurable: true,
335
+ // })
336
+ // }
337
+ }
338
+ }
339
+
340
+ /**
341
+ * Extra options for AppError constructor.
342
+ */
343
+ export interface AppErrorOptions {
344
+ /**
345
+ * Overrides Error.name and Error.constructor.name
346
+ */
347
+ name?: string
348
+
349
+ /**
350
+ * Sets Error.cause.
351
+ * It is transformed with _anyToErrorObject()
352
+ */
353
+ cause?: any
354
+ }
355
+
356
+ /**
357
+ * Error that is thrown when Http Request was made and returned an error.
358
+ * Thrown by, for example, Fetcher.
359
+ *
360
+ * On the Frontend this Error class represents the error when calling the API,
361
+ * contains all the necessary request and response information.
362
+ *
363
+ * On the Backend, similarly, it represents the error when calling some 3rd-party API
364
+ * (backend-to-backend call).
365
+ * On the Backend it often propagates all the way to the Backend error handler,
366
+ * where it would be wrapped in BackendErrorResponseObject.
367
+ *
368
+ * Please note that `ErrorData.backendResponseStatusCode` is NOT exactly the same as
369
+ * `HttpRequestErrorData.responseStatusCode`.
370
+ * E.g 3rd-party call may return 401, but our Backend will still wrap it into an 500 error
371
+ * (by default).
372
+ */
373
+ export class HttpRequestError extends AppError<HttpRequestErrorData> {
374
+ constructor(message: string, data: HttpRequestErrorData, opt?: AppErrorOptions) {
375
+ if (data.response) {
376
+ Object.defineProperty(data, 'response', {
377
+ enumerable: false,
378
+ })
379
+ }
380
+
381
+ super(message, data, { ...opt, name: 'HttpRequestError' })
382
+ }
383
+
384
+ /**
385
+ * Cause is strictly-defined for HttpRequestError,
386
+ * so it always has a cause.
387
+ * (for dev convenience)
388
+ */
389
+ override cause!: ErrorObject
390
+ }
391
+
392
+ export class AssertionError extends AppError {
393
+ constructor(message: string, data?: ErrorData) {
394
+ super(message, data, { name: 'AssertionError' })
395
+ }
396
+ }
397
+
398
+ export interface JsonParseErrorData extends ErrorData {
399
+ /**
400
+ * Original text that failed to get parsed.
401
+ */
402
+ text?: string
403
+ }
404
+
405
+ export class JsonParseError extends AppError<JsonParseErrorData> {
406
+ constructor(data: JsonParseErrorData) {
407
+ const message = ['Failed to parse', data.text && _truncateMiddle(data.text, 200)]
408
+ .filter(Boolean)
409
+ .join(': ')
410
+
411
+ super(message, data, { name: 'JsonParseError' })
412
+ }
413
+ }
414
+
415
+ export class TimeoutError extends AppError {
416
+ constructor(message: string, data?: ErrorData, opt?: AppErrorOptions) {
417
+ super(message, data, { ...opt, name: 'TimeoutError' })
418
+ }
419
+ }
420
+
421
+ /**
422
+ * It is thrown when Error was expected, but didn't happen
423
+ * ("pass" happened instead).
424
+ * "Pass" means "no error".
425
+ */
426
+ export class UnexpectedPassError extends AppError {
427
+ constructor() {
428
+ super(
429
+ 'expected error was not thrown',
430
+ {},
431
+ {
432
+ name: 'UnexpectedPassError',
433
+ },
434
+ )
435
+ }
436
+ }
package/src/error/try.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { _stringifyAny } from '../string/stringifyAny'
2
2
  import type { Class } from '../typeFest'
3
3
  import type { AnyFunction, ErrorDataTuple } from '../types'
4
- import { AppError } from './app.error'
5
4
  import { _assertErrorClassOrRethrow } from './assert'
5
+ import { UnexpectedPassError } from './error.util'
6
6
 
7
7
  /**
8
8
  * Calls a function, returns a Tuple of [error, value].
@@ -55,23 +55,6 @@ export async function pTry<T, ERR extends Error = Error>(
55
55
  }
56
56
  }
57
57
 
58
- /**
59
- * It is thrown when Error was expected, but didn't happen
60
- * ("pass" happened instead).
61
- * "Pass" means "no error".
62
- */
63
- export class UnexpectedPassError extends AppError {
64
- constructor() {
65
- super(
66
- 'expected error was not thrown',
67
- {},
68
- {
69
- name: 'UnexpectedPassError',
70
- },
71
- )
72
- }
73
- }
74
-
75
58
  /**
76
59
  * Calls `fn`, expects is to throw, catches the expected error and returns.
77
60
  * If error was NOT thrown - throws UnexpectedPassError instead.
@@ -4,8 +4,13 @@
4
4
  import { isServerSide } from '../env'
5
5
  import { _assertErrorClassOrRethrow } from '../error/assert'
6
6
  import { ErrorLike, ErrorObject } from '../error/error.model'
7
- import { _anyToError, _anyToErrorObject, _errorLikeToErrorObject } from '../error/error.util'
8
- import { HttpRequestError } from '../error/httpRequestError'
7
+ import {
8
+ _anyToError,
9
+ _anyToErrorObject,
10
+ _errorLikeToErrorObject,
11
+ HttpRequestError,
12
+ TimeoutError,
13
+ } from '../error/error.util'
9
14
  import { _clamp } from '../number/number.util'
10
15
  import {
11
16
  _filterNullishValues,
@@ -16,7 +21,7 @@ import {
16
21
  _pick,
17
22
  } from '../object/object.util'
18
23
  import { pDelay } from '../promise/pDelay'
19
- import { pTimeout, TimeoutError } from '../promise/pTimeout'
24
+ import { pTimeout } from '../promise/pTimeout'
20
25
  import { _jsonParse, _jsonParseIfPossible } from '../string/json.util'
21
26
  import { _stringifyAny } from '../string/stringifyAny'
22
27
  import { _ms, _since } from '../time/time.util'
package/src/index.ts CHANGED
@@ -14,16 +14,13 @@ export * from './decorators/memoFn'
14
14
  export * from './decorators/memoFnAsync'
15
15
  export * from './decorators/retry.decorator'
16
16
  export * from './decorators/timeout.decorator'
17
- export * from './error/app.error'
18
17
  export * from './error/assert'
19
18
  export * from './enum.util'
20
19
  export * from './error/error.model'
21
20
  export * from './error/error.util'
22
21
  export * from './error/errorMode'
23
- export * from './error/httpRequestError'
24
22
  export * from './error/try'
25
23
  export * from './error/tryCatch'
26
- export * from './error/jsonParseError'
27
24
  export * from './json-schema/from-data/generateJsonSchemaFromData'
28
25
  export * from './json-schema/jsonSchema.cnst'
29
26
  export * from './json-schema/jsonSchema.model'
@@ -1,14 +1,7 @@
1
- import { AppError, AppErrorOptions } from '../error/app.error'
2
1
  import type { ErrorData } from '../error/error.model'
3
- import { _errorDataAppend } from '../error/error.util'
2
+ import { _errorDataAppend, TimeoutError } from '../error/error.util'
4
3
  import type { AnyAsyncFunction, NumberOfMilliseconds } from '../types'
5
4
 
6
- export class TimeoutError extends AppError {
7
- constructor(message: string, data?: ErrorData, opt?: AppErrorOptions) {
8
- super(message, data, { ...opt, name: 'TimeoutError' })
9
- }
10
- }
11
-
12
5
  export interface PTimeoutOptions {
13
6
  /**
14
7
  * Timeout in milliseconds.
@@ -1,4 +1,4 @@
1
- import { JsonParseError } from '../error/jsonParseError'
1
+ import { JsonParseError } from '../error/error.util'
2
2
  import { Reviver } from '../types'
3
3
 
4
4
  // const possibleJsonStartTokens = ['{', '[', '"']
@@ -1,31 +0,0 @@
1
- import type { ErrorData, ErrorObject } from './error.model';
2
- /**
3
- * Base class for all our (not system) errors.
4
- *
5
- * message - "technical" message. Frontend decides to show it or not.
6
- * data - optional "any" payload.
7
- * data.userFriendly - if present, will be displayed to the User as is.
8
- *
9
- * Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
10
- */
11
- export declare class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
12
- data: DATA_TYPE;
13
- /**
14
- * `cause` here is normalized to be an ErrorObject
15
- */
16
- cause?: ErrorObject;
17
- constructor(message: string, data?: DATA_TYPE, opt?: AppErrorOptions);
18
- }
19
- /**
20
- * Extra options for AppError constructor.
21
- */
22
- export interface AppErrorOptions {
23
- /**
24
- * Overrides Error.name and Error.constructor.name
25
- */
26
- name?: string;
27
- /**
28
- * Sets Error.cause
29
- */
30
- cause?: ErrorObject;
31
- }
@@ -1,57 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AppError = void 0;
4
- /**
5
- * Base class for all our (not system) errors.
6
- *
7
- * message - "technical" message. Frontend decides to show it or not.
8
- * data - optional "any" payload.
9
- * data.userFriendly - if present, will be displayed to the User as is.
10
- *
11
- * Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
12
- */
13
- class AppError extends Error {
14
- constructor(message, data = {}, opt = {}) {
15
- super(message);
16
- const { name = this.constructor.name, cause } = opt;
17
- Object.defineProperties(this, {
18
- name: {
19
- value: name,
20
- configurable: true,
21
- writable: true,
22
- },
23
- data: {
24
- value: data,
25
- writable: true,
26
- configurable: true,
27
- enumerable: false,
28
- },
29
- });
30
- if (cause) {
31
- Object.defineProperty(this, 'cause', {
32
- // I'd love to do _anyToError(opt.cause) here, but it causes circular dep ;(
33
- value: cause,
34
- writable: true,
35
- configurable: true,
36
- enumerable: true, // unlike standard - setting it to true for "visibility"
37
- });
38
- }
39
- // this is to allow changing this.constuctor.name to a non-minified version
40
- Object.defineProperty(this.constructor, 'name', {
41
- value: name,
42
- configurable: true,
43
- writable: true,
44
- });
45
- // todo: check if it's needed at all!
46
- // if (Error.captureStackTrace) {
47
- // Error.captureStackTrace(this, this.constructor)
48
- // } else {
49
- // Object.defineProperty(this, 'stack', {
50
- // value: new Error().stack, // eslint-disable-line unicorn/error-message
51
- // writable: true,
52
- // configurable: true,
53
- // })
54
- // }
55
- }
56
- }
57
- exports.AppError = AppError;
@@ -1,28 +0,0 @@
1
- import { AppError, AppErrorOptions } from './app.error';
2
- import type { ErrorObject, HttpRequestErrorData } from './error.model';
3
- /**
4
- * Error that is thrown when Http Request was made and returned an error.
5
- * Thrown by, for example, Fetcher.
6
- *
7
- * On the Frontend this Error class represents the error when calling the API,
8
- * contains all the necessary request and response information.
9
- *
10
- * On the Backend, similarly, it represents the error when calling some 3rd-party API
11
- * (backend-to-backend call).
12
- * On the Backend it often propagates all the way to the Backend error handler,
13
- * where it would be wrapped in BackendErrorResponseObject.
14
- *
15
- * Please note that `ErrorData.backendResponseStatusCode` is NOT exactly the same as
16
- * `HttpRequestErrorData.responseStatusCode`.
17
- * E.g 3rd-party call may return 401, but our Backend will still wrap it into an 500 error
18
- * (by default).
19
- */
20
- export declare class HttpRequestError extends AppError<HttpRequestErrorData> {
21
- constructor(message: string, data: HttpRequestErrorData, opt?: AppErrorOptions);
22
- /**
23
- * Cause is strictly-defined for HttpRequestError,
24
- * so it always has a cause.
25
- * (for dev convenience)
26
- */
27
- cause: ErrorObject;
28
- }
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HttpRequestError = void 0;
4
- const app_error_1 = require("./app.error");
5
- /**
6
- * Error that is thrown when Http Request was made and returned an error.
7
- * Thrown by, for example, Fetcher.
8
- *
9
- * On the Frontend this Error class represents the error when calling the API,
10
- * contains all the necessary request and response information.
11
- *
12
- * On the Backend, similarly, it represents the error when calling some 3rd-party API
13
- * (backend-to-backend call).
14
- * On the Backend it often propagates all the way to the Backend error handler,
15
- * where it would be wrapped in BackendErrorResponseObject.
16
- *
17
- * Please note that `ErrorData.backendResponseStatusCode` is NOT exactly the same as
18
- * `HttpRequestErrorData.responseStatusCode`.
19
- * E.g 3rd-party call may return 401, but our Backend will still wrap it into an 500 error
20
- * (by default).
21
- */
22
- class HttpRequestError extends app_error_1.AppError {
23
- constructor(message, data, opt) {
24
- if (data.response) {
25
- Object.defineProperty(data, 'response', {
26
- enumerable: false,
27
- });
28
- }
29
- super(message, data, { ...opt, name: 'HttpRequestError' });
30
- }
31
- }
32
- exports.HttpRequestError = HttpRequestError;
@@ -1,11 +0,0 @@
1
- import { AppError } from './app.error';
2
- import { ErrorData } from './error.model';
3
- export interface JsonParseErrorData extends ErrorData {
4
- /**
5
- * Original text that failed to get parsed.
6
- */
7
- text?: string;
8
- }
9
- export declare class JsonParseError extends AppError<JsonParseErrorData> {
10
- constructor(data: JsonParseErrorData);
11
- }
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JsonParseError = void 0;
4
- const string_util_1 = require("../string/string.util");
5
- const app_error_1 = require("./app.error");
6
- class JsonParseError extends app_error_1.AppError {
7
- constructor(data) {
8
- const message = ['Failed to parse', data.text && (0, string_util_1._truncateMiddle)(data.text, 200)]
9
- .filter(Boolean)
10
- .join(': ');
11
- super(message, data, { name: 'JsonParseError' });
12
- }
13
- }
14
- exports.JsonParseError = JsonParseError;
@@ -1,53 +0,0 @@
1
- /**
2
- * Base class for all our (not system) errors.
3
- *
4
- * message - "technical" message. Frontend decides to show it or not.
5
- * data - optional "any" payload.
6
- * data.userFriendly - if present, will be displayed to the User as is.
7
- *
8
- * Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
9
- */
10
- export class AppError extends Error {
11
- constructor(message, data = {}, opt = {}) {
12
- super(message);
13
- const { name = this.constructor.name, cause } = opt;
14
- Object.defineProperties(this, {
15
- name: {
16
- value: name,
17
- configurable: true,
18
- writable: true,
19
- },
20
- data: {
21
- value: data,
22
- writable: true,
23
- configurable: true,
24
- enumerable: false,
25
- },
26
- });
27
- if (cause) {
28
- Object.defineProperty(this, 'cause', {
29
- // I'd love to do _anyToError(opt.cause) here, but it causes circular dep ;(
30
- value: cause,
31
- writable: true,
32
- configurable: true,
33
- enumerable: true, // unlike standard - setting it to true for "visibility"
34
- });
35
- }
36
- // this is to allow changing this.constuctor.name to a non-minified version
37
- Object.defineProperty(this.constructor, 'name', {
38
- value: name,
39
- configurable: true,
40
- writable: true,
41
- });
42
- // todo: check if it's needed at all!
43
- // if (Error.captureStackTrace) {
44
- // Error.captureStackTrace(this, this.constructor)
45
- // } else {
46
- // Object.defineProperty(this, 'stack', {
47
- // value: new Error().stack, // eslint-disable-line unicorn/error-message
48
- // writable: true,
49
- // configurable: true,
50
- // })
51
- // }
52
- }
53
- }
@@ -1,28 +0,0 @@
1
- import { AppError } from './app.error';
2
- /**
3
- * Error that is thrown when Http Request was made and returned an error.
4
- * Thrown by, for example, Fetcher.
5
- *
6
- * On the Frontend this Error class represents the error when calling the API,
7
- * contains all the necessary request and response information.
8
- *
9
- * On the Backend, similarly, it represents the error when calling some 3rd-party API
10
- * (backend-to-backend call).
11
- * On the Backend it often propagates all the way to the Backend error handler,
12
- * where it would be wrapped in BackendErrorResponseObject.
13
- *
14
- * Please note that `ErrorData.backendResponseStatusCode` is NOT exactly the same as
15
- * `HttpRequestErrorData.responseStatusCode`.
16
- * E.g 3rd-party call may return 401, but our Backend will still wrap it into an 500 error
17
- * (by default).
18
- */
19
- export class HttpRequestError extends AppError {
20
- constructor(message, data, opt) {
21
- if (data.response) {
22
- Object.defineProperty(data, 'response', {
23
- enumerable: false,
24
- });
25
- }
26
- super(message, data, Object.assign(Object.assign({}, opt), { name: 'HttpRequestError' }));
27
- }
28
- }
@@ -1,10 +0,0 @@
1
- import { _truncateMiddle } from '../string/string.util';
2
- import { AppError } from './app.error';
3
- export class JsonParseError extends AppError {
4
- constructor(data) {
5
- const message = ['Failed to parse', data.text && _truncateMiddle(data.text, 200)]
6
- .filter(Boolean)
7
- .join(': ');
8
- super(message, data, { name: 'JsonParseError' });
9
- }
10
- }