@naturalcycles/js-lib 14.124.0 → 14.124.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.
@@ -11,12 +11,23 @@ const __1 = require("..");
11
11
  * Alternatively, if you're sure it's Error - you can use `_assertIsError(err)`.
12
12
  */
13
13
  function _anyToError(o, errorClass = Error, errorData, opt) {
14
- if (o instanceof errorClass)
15
- return o;
16
- // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
17
- const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt);
18
- Object.assign(errorObject.data, errorData);
19
- return _errorObjectToError(errorObject, errorClass);
14
+ let e;
15
+ if (o instanceof errorClass) {
16
+ e = o;
17
+ }
18
+ else {
19
+ // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
20
+ const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt);
21
+ e = _errorObjectToError(errorObject, errorClass);
22
+ }
23
+ if (errorData) {
24
+ ;
25
+ e.data = {
26
+ ...e.data,
27
+ ...errorData,
28
+ };
29
+ }
30
+ return e;
20
31
  }
21
32
  exports._anyToError = _anyToError;
22
33
  /**
@@ -8,12 +8,20 @@ import { AppError, _jsonParseIfPossible, _stringifyAny } from '..';
8
8
  * Alternatively, if you're sure it's Error - you can use `_assertIsError(err)`.
9
9
  */
10
10
  export function _anyToError(o, errorClass = Error, errorData, opt) {
11
- if (o instanceof errorClass)
12
- return o;
13
- // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
14
- const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt);
15
- Object.assign(errorObject.data, errorData);
16
- return _errorObjectToError(errorObject, errorClass);
11
+ let e;
12
+ if (o instanceof errorClass) {
13
+ e = o;
14
+ }
15
+ else {
16
+ // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
17
+ const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt);
18
+ e = _errorObjectToError(errorObject, errorClass);
19
+ }
20
+ if (errorData) {
21
+ ;
22
+ e.data = Object.assign(Object.assign({}, e.data), errorData);
23
+ }
24
+ return e;
17
25
  }
18
26
  /**
19
27
  * Converts "anything" to ErrorObject.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.124.0",
3
+ "version": "14.124.1",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -22,13 +22,25 @@ export function _anyToError<ERROR_TYPE extends Error = Error>(
22
22
  errorData?: ErrorData,
23
23
  opt?: StringifyAnyOptions,
24
24
  ): ERROR_TYPE {
25
- if (o instanceof errorClass) return o
25
+ let e: ERROR_TYPE
26
+
27
+ if (o instanceof errorClass) {
28
+ e = o
29
+ } else {
30
+ // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
26
31
 
27
- // If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
32
+ const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt)
33
+ e = _errorObjectToError(errorObject, errorClass) as any
34
+ }
35
+
36
+ if (errorData) {
37
+ ;(e as any).data = {
38
+ ...(e as any).data,
39
+ ...errorData,
40
+ }
41
+ }
28
42
 
29
- const errorObject = _isErrorObject(o) ? o : _anyToErrorObject(o, {}, opt)
30
- Object.assign(errorObject.data, errorData)
31
- return _errorObjectToError(errorObject, errorClass)
43
+ return e
32
44
  }
33
45
 
34
46
  /**