@smbcheeky/error-object-from-payload 1.1.6 → 1.2.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.
- package/README.md +29 -38
- package/dist/index.d.mts +143 -0
- package/dist/index.d.ts +17 -8
- package/dist/index.js +160 -123
- package/dist/index.mjs +155 -118
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -7,9 +7,8 @@
|
|
|
7
7
|
## TL;DR
|
|
8
8
|
|
|
9
9
|
- Install the package `npm install @smbcheeky/error-object @smbcheeky/error-object-from-payload`
|
|
10
|
-
- Write `
|
|
10
|
+
- Write `new ErrorObjectFromPayload(<pick an api response with an error>).verboseLog('LOG')` and use the info provided to
|
|
11
11
|
map your error
|
|
12
|
-
- Switch the .force with .error, and now you have an error object
|
|
13
12
|
- :tada:
|
|
14
13
|
- oh... and check the [playground](https://github.com/SMBCheeky/error-object-from-payload/blob/main/playground/index.ts)
|
|
15
14
|
file
|
|
@@ -22,8 +21,9 @@
|
|
|
22
21
|
|
|
23
22
|
## Description
|
|
24
23
|
|
|
25
|
-
The ErrorObject class is made to extend `Error` enabling checks like `errorObject instanceof Error
|
|
26
|
-
`errorObject instanceof ErrorObject
|
|
24
|
+
The ErrorObject class is made to extend `Error` enabling type guard checks like `errorObject instanceof Error`,
|
|
25
|
+
`errorObject instanceof ErrorObject`, ErrorObject.is() and isErrorObject(). . The `ErrorObject` class is backwards
|
|
26
|
+
compatible with `Error`.
|
|
27
27
|
|
|
28
28
|
This functionality was extracted from the [error-object](https://github.com/SMBCheeky/error-object) package (at v1.1.5)
|
|
29
29
|
and renamed to `error-object-from-payload` to simplify the main package and its README. This package is not a
|
|
@@ -32,13 +32,7 @@ payload by providing step-by-step debugging logs for the entire process.
|
|
|
32
32
|
|
|
33
33
|
## Usage
|
|
34
34
|
|
|
35
|
-
Use `
|
|
36
|
-
|
|
37
|
-
- you can pass an object or a caught error to it, and it will try its best to create an error from it
|
|
38
|
-
- `fromPayload(<anything>)` returns an object with two properties: `.error` and `.force`
|
|
39
|
-
- `.error` represents the error, if it can be created, otherwise it is `undefined`
|
|
40
|
-
- `.force` represents the error, if it can be created, otherwise it is going to return a `ErrorObject.fallback()`
|
|
41
|
-
error
|
|
35
|
+
Use `new ErrorObjectFromPayload(<anything>, ErrorObjectBuildOptions)` to create errors from any input.
|
|
42
36
|
|
|
43
37
|
The processing of the ErrorObject is done in a few steps, based on the `ErrorObjectBuildOptions`:
|
|
44
38
|
|
|
@@ -46,26 +40,27 @@ The processing of the ErrorObject is done in a few steps, based on the `ErrorObj
|
|
|
46
40
|
`checkInputObjectForKeys`
|
|
47
41
|
- then the objects checks for an object array at `pathToErrors`, which could be an array of errors
|
|
48
42
|
- if an error array is found, the process will consider all other paths relative to the objects in the error array found
|
|
49
|
-
- if an error array is not found, the process will consider all other paths absolute to the initial
|
|
50
|
-
`
|
|
43
|
+
- if an error array is not found, the process will consider all other paths absolute to the initial object passed to
|
|
44
|
+
`new ErrorObjectFromPayload()`
|
|
51
45
|
- the `pathToCode`, `pathToNumberCode`, `pathToMessage`, `pathToDetails` and `pathToDomain` options are used to map
|
|
52
46
|
values to their associated field, if found
|
|
53
47
|
- for all fields other than `numberCode`, if a value is found and is a string, it is saved as is, but if it is an array
|
|
54
|
-
or an object it will be JSON.stringify
|
|
55
|
-
- for `numberCode`, if a value is found and it is a number different
|
|
48
|
+
or an object, it will be JSON.stringify and saved as a string
|
|
49
|
+
- for `numberCode`, if a value is found, and it is a number different from `NaN`, it is saved
|
|
56
50
|
- the `transform` function is used to transform the found values by the parsing process into the error object
|
|
57
51
|
- the transform function has access to all pre-transformation values and also the initial object (object inside the
|
|
58
|
-
errors array or initial object)
|
|
52
|
+
errors' array or initial object)
|
|
59
53
|
- everything gets processed into a list of `ErrorSummary | ErrorObjectErrorResult` array
|
|
60
|
-
- it contains everything, from error strings custom-made to be as distinct and easy to read as possible, to
|
|
61
|
-
documenting summaries of what values are found, at which path, if an
|
|
54
|
+
- it contains everything, from error strings custom-made to be as distinct and easy to read as possible, to
|
|
55
|
+
self-documenting summaries of what values are found, at which path, if an error object was found, etc.
|
|
62
56
|
- the count of the list is meant to be an indication of how many input objects were found and processed, as each of them
|
|
63
57
|
should become an error object
|
|
64
58
|
- in the last step of the process, the list is filtered down and a single error object is created, with everything baked
|
|
65
59
|
in
|
|
66
|
-
-
|
|
67
|
-
|
|
68
|
-
|
|
60
|
+
- the `raw` will contain details about the process and also the initial/input object
|
|
61
|
+
- check `processingErrors` and `summary` fields in `raw` object for the errors and the summaries that were
|
|
62
|
+
computed during the process
|
|
63
|
+
- the `nextErrors` array allows for all errors found to be saved on one single error object for later use
|
|
69
64
|
|
|
70
65
|
## Usage & Examples
|
|
71
66
|
|
|
@@ -75,17 +70,17 @@ the [playground](https://github.com/SMBCheeky/error-object/blob/main/playground/
|
|
|
75
70
|
```typescript
|
|
76
71
|
new ErrorObject({ code: '', message: 'Something went wrong.', domain: 'auth' }).debugLog('LOG');
|
|
77
72
|
|
|
78
|
-
|
|
73
|
+
new ErrorObjectFromPayload({ code: '', message: 'Something went wrong', domain: 'auth' })?.force?.debugLog('LOG');
|
|
79
74
|
|
|
80
75
|
// Example 12 output:
|
|
81
|
-
//
|
|
76
|
+
//
|
|
82
77
|
// [LOG] Something went wrong. [auth]
|
|
83
78
|
// {
|
|
84
79
|
// "code": "",
|
|
85
80
|
// "message": "Something went wrong.",
|
|
86
81
|
// "domain": "auth"
|
|
87
82
|
// }
|
|
88
|
-
//
|
|
83
|
+
//
|
|
89
84
|
// [LOG] Something went wrong [auth]
|
|
90
85
|
// {
|
|
91
86
|
// "code": "",
|
|
@@ -103,7 +98,7 @@ const response = {
|
|
|
103
98
|
body: '{"error":"Invalid input data","code":400}',
|
|
104
99
|
};
|
|
105
100
|
|
|
106
|
-
|
|
101
|
+
new ErrorObjectFromPayload(JSON.parse(response?.body), {
|
|
107
102
|
pathToNumberCode: ['code'],
|
|
108
103
|
pathToMessage: ['error'],
|
|
109
104
|
}).force?.debugLog('LOG');
|
|
@@ -119,13 +114,12 @@ fromPayload(JSON.parse(response?.body), {
|
|
|
119
114
|
```
|
|
120
115
|
|
|
121
116
|
```typescript
|
|
122
|
-
/*
|
|
123
|
-
* You could have a file called `errors.ts` in each of your modules/folders and
|
|
124
|
-
* define a function like `createAuthError2()` that returns an error object with
|
|
117
|
+
/*
|
|
118
|
+
* You could have a file called `errors.ts` in each of your modules/folders and
|
|
119
|
+
* define a function like `createAuthError2()` that returns an error object with
|
|
125
120
|
* the correct message and domain.
|
|
126
121
|
*/
|
|
127
|
-
const AuthMessageResolver = (
|
|
128
|
-
beforeTransform: ErrorObjectTransformState): ErrorObjectTransformState => {
|
|
122
|
+
const AuthMessageResolver = (beforeTransform: ErrorObjectTransformState): ErrorObjectTransformState => {
|
|
129
123
|
// Quick tip: Make all messages slightly different, to make it easy
|
|
130
124
|
// to find the right one when debugging, even in production
|
|
131
125
|
let message: string | undefined;
|
|
@@ -146,10 +140,9 @@ const AuthMessageResolver = (
|
|
|
146
140
|
};
|
|
147
141
|
|
|
148
142
|
const createAuthError2 = (code: string) => {
|
|
149
|
-
return
|
|
143
|
+
return new ErrorObjectFromPayload({ code, domain: 'auth' }, { transform: AuthMessageResolver });
|
|
150
144
|
};
|
|
151
145
|
|
|
152
|
-
|
|
153
146
|
createAuthError2('generic')?.error?.log('1');
|
|
154
147
|
createAuthError2('generic-again')?.error?.log('2');
|
|
155
148
|
createAuthError2('generic-network')?.error?.log('3');
|
|
@@ -167,12 +160,10 @@ createAuthError2('invalid-code')?.error?.log('4');
|
|
|
167
160
|
|
|
168
161
|
### How do I use paths? Are they absolute?
|
|
169
162
|
|
|
170
|
-
To support inputs containing arrays of errors as well as single errors, all paths are treated initially as absolute
|
|
171
|
-
from the
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
choice: set the "pathToErrors" option as empty, and then map only the first error (highly not recommended), or adjust
|
|
175
|
-
the paths to be relative to the objects inside the detected errors array.
|
|
163
|
+
To support inputs containing arrays of errors as well as single errors, all paths are treated initially as absolute
|
|
164
|
+
(from the input root), but if an array of errors is detected, it will consider each element found the new root input
|
|
165
|
+
object. Devs have a choice: set the "pathToErrors" option as empty, and then map only the first
|
|
166
|
+
error (not recommended), or adjust the paths to be relative to the objects inside the detected errors array.
|
|
176
167
|
|
|
177
168
|
### How do I use paths? I sometimes get the error code in an `error` object, and sometimes in the root object...
|
|
178
169
|
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { ErrorObject } from '@smbcheeky/error-object';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* For customizing the paths, please make sure to use the {@link addPrefixPathVariants} function to also add the `error.` prefixed paths to the pathTo... arrays used in the {@link ErrorObjectBuildOptions} type.
|
|
5
|
+
*/
|
|
6
|
+
declare const addPrefixPathVariants: (prefix: string | string[], array: string[]) => string[];
|
|
7
|
+
/**
|
|
8
|
+
* The {@link DEFAULT_BUILD_OPTIONS} is a set of default options that can be used to customize the behavior of the {@link fromPayload()} method.
|
|
9
|
+
* It contains common paths and a transform function that automatically converts the error number code, if it exists, to a string and sets it as the error code.
|
|
10
|
+
*/
|
|
11
|
+
declare const DEFAULT_BUILD_OPTIONS: ErrorObjectBuildOptions;
|
|
12
|
+
/**
|
|
13
|
+
* The {@link ErrorObjectTransformState} type contains the state of the error object before and after the transformation.
|
|
14
|
+
*/
|
|
15
|
+
type ErrorObjectTransformState = {
|
|
16
|
+
code?: string | undefined;
|
|
17
|
+
numberCode?: number | undefined;
|
|
18
|
+
message?: string | undefined;
|
|
19
|
+
details?: string | undefined;
|
|
20
|
+
domain?: string | undefined;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The {@link ErrorObjectBuildOptions} type contains all the options that can be used to customize the behavior of the {@link fromPayload()} method.
|
|
24
|
+
* Options are self-explanatory and the code behind them is kept similar and very straightforward, by design.
|
|
25
|
+
*/
|
|
26
|
+
type ErrorObjectBuildOptions = {
|
|
27
|
+
/**
|
|
28
|
+
* The {@link checkInputObjectForValues} option allows you to check if the input object contains specific values.
|
|
29
|
+
*/
|
|
30
|
+
checkInputObjectForValues?: {
|
|
31
|
+
[key: string]: {
|
|
32
|
+
value: string | number | boolean | null;
|
|
33
|
+
exists: boolean;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* The {@link checkInputObjectForTypes} option allows you to check if the input object contains specific types.
|
|
38
|
+
*/
|
|
39
|
+
checkInputObjectForTypes?: {
|
|
40
|
+
[key: string]: {
|
|
41
|
+
type: 'string' | 'number' | 'boolean' | 'object';
|
|
42
|
+
valueIsArray?: boolean;
|
|
43
|
+
exists: boolean;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* The {@link checkInputObjectForKeys} option allows you to check if the input object contains specific keys.
|
|
48
|
+
*/
|
|
49
|
+
checkInputObjectForKeys?: {
|
|
50
|
+
[key: string]: {
|
|
51
|
+
exists: boolean;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* All paths should be absolute, from the root of the input object, unless an array of errors is found.
|
|
56
|
+
* When an array of errors is found, the paths are considered relative to the objects found in the errors array.
|
|
57
|
+
* "[" and "]" chars are not allowed. Use paths like "errors.0.code" instead of "errors[0].code".
|
|
58
|
+
*/
|
|
59
|
+
pathToErrors: string[];
|
|
60
|
+
/**
|
|
61
|
+
* Path to the error code. This is the main error code that is used to identify the error.
|
|
62
|
+
*/
|
|
63
|
+
pathToCode: string[];
|
|
64
|
+
/**
|
|
65
|
+
* Path to the error number code. This is an optional property that can be used to provide a numeric error code.
|
|
66
|
+
* The default options will automatically convert the number code to a string and set it as the error code + keep the original number code.
|
|
67
|
+
*/
|
|
68
|
+
pathToNumberCode: string[];
|
|
69
|
+
/**
|
|
70
|
+
* Path to the error message. This is the main error message that is used to describe the error.
|
|
71
|
+
*/
|
|
72
|
+
pathToMessage: string[];
|
|
73
|
+
/**
|
|
74
|
+
* Path to the error details. This is an optional property that can be used to provide additional details about the error.
|
|
75
|
+
* A common pattern is to use this to keep extra information about the error.
|
|
76
|
+
*/
|
|
77
|
+
pathToDetails: string[];
|
|
78
|
+
/**
|
|
79
|
+
* Path to the error domain. This is an optional property that can be used to provide a domain for the error.
|
|
80
|
+
*/
|
|
81
|
+
pathToDomain: string[];
|
|
82
|
+
/**
|
|
83
|
+
* The transform function is used to transform the properties found during the process of building the error object.
|
|
84
|
+
* This is useful for transforming a the message based on the error code, the domain based on the error code, etc. allowing
|
|
85
|
+
* the developer to customize the final error object created.
|
|
86
|
+
*/
|
|
87
|
+
transform?: (beforeTransform: ErrorObjectTransformState, inputObject: any) => ErrorObjectTransformState;
|
|
88
|
+
};
|
|
89
|
+
type PathValueAndTransform<V> = {
|
|
90
|
+
path: string | undefined;
|
|
91
|
+
beforeTransform: V | undefined;
|
|
92
|
+
value: V | undefined;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* The {@link ErrorSummary} helps with debugging and troubleshooting the error object building process.
|
|
96
|
+
* It contains the input object and the properties found during the process of building the error object.
|
|
97
|
+
*/
|
|
98
|
+
type ErrorSummary = {
|
|
99
|
+
didDetectErrorsArray?: boolean;
|
|
100
|
+
input: NonNullable<Record<string, any>>;
|
|
101
|
+
path?: string;
|
|
102
|
+
value: {
|
|
103
|
+
code?: PathValueAndTransform<string>;
|
|
104
|
+
numberCode?: PathValueAndTransform<number>;
|
|
105
|
+
message?: PathValueAndTransform<string>;
|
|
106
|
+
details?: PathValueAndTransform<string>;
|
|
107
|
+
domain?: PathValueAndTransform<string>;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* The {@link ErrorObjectErrorResult} type contains all the possible error results that can be returned by the {@link fromPayload()} method.
|
|
112
|
+
* The error results are used to identify the type of error that occurred during the process of building the error object.
|
|
113
|
+
*/
|
|
114
|
+
type ErrorObjectErrorResult = 'isNullish' | 'isNotAnObject' | 'checkIsNullish' | 'checkIsNotAnObject' | 'isNotAnArray' | 'checkInputObjectForValuesIsNotAnObject' | 'checkInputObjectForValuesFailed' | 'checkInputObjectForTypesIsNotAnObject' | 'checkInputObjectForTypesFailed' | 'checkInputObjectForTypesValueIsArrayFailed' | 'checkInputObjectForKeysIsNotAnObject' | 'checkInputObjectForKeysFailed' | 'pathToErrorsIsNotAnArray' | 'pathToErrorsValuesAreNotStrings' | 'pathToCodeIsInvalid' | 'pathToCodeIsNotAnArray' | 'pathToCodeValuesAreNotStrings' | 'pathToNumberCodeIsInvalid' | 'pathToNumberCodeIsNotAnArray' | 'pathToNumberCodeValuesAreNotStrings' | 'pathToMessageIsInvalid' | 'pathToMessageIsNotAnArray' | 'pathToMessageValuesAreNotStrings' | 'pathToDetailsIsInvalid' | 'pathToDetailsIsNotAnArray' | 'pathToDetailsValuesAreNotStrings' | 'pathToDomainIsInvalid' | 'pathToDomainIsNotAnArray' | 'pathToDomainValuesAreNotStrings' | 'transformIsNotAFunction' | 'transformResultIsNotAValidObject' | 'transformCodeResultIsNotString' | 'transformNumberCodeResultIsNotNumber' | 'transformNumberCodeResultIsNaN' | 'transformMessageResultIsNotString' | 'transformDetailsResultIsNotString' | 'transformDomainResultIsNotString' | 'buildSummaryIsNullish' | 'buildSummaryIsNotAnObject' | 'generalBuildSummariesFromObjectError' | 'generalBuildSummaryFromObjectError' | 'generalCheckInputObjectForValuesError' | 'unknownCodeOrMessage' | 'invalidSummary';
|
|
115
|
+
/**
|
|
116
|
+
* The {@link ErrorObjectProcessingError} type contains the error result and the error summary.
|
|
117
|
+
* It provides all the information needed to identify and troubleshoot the error that occurred during the process of building the error object.
|
|
118
|
+
*/
|
|
119
|
+
type ErrorObjectProcessingError = {
|
|
120
|
+
errorCode: ErrorObjectErrorResult;
|
|
121
|
+
summary?: ErrorSummary;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The {@link ErrorObjectFromPayload} class is an alternative way to create an {@link ErrorObject} from anything
|
|
126
|
+
* resembling an error, even request payloads. It also chains errors using the `.setNextErrors()` method.
|
|
127
|
+
*/
|
|
128
|
+
declare class ErrorObjectFromPayload extends ErrorObject {
|
|
129
|
+
static DEFAULT_FALLBACK_TAG: string;
|
|
130
|
+
nextErrors?: ErrorObjectFromPayload[];
|
|
131
|
+
constructor(props: any, withOptions?: Partial<ErrorObjectBuildOptions>);
|
|
132
|
+
static generic: () => ErrorObjectFromPayload;
|
|
133
|
+
protected _log(logTag: string, logLevel: 'log' | 'debug' | 'verbose'): this;
|
|
134
|
+
toVerboseString(): string;
|
|
135
|
+
/**
|
|
136
|
+
* This method allows users to check if an error is a fallback error, returned when an error could not be created from payload.
|
|
137
|
+
*/
|
|
138
|
+
isFallback(): boolean;
|
|
139
|
+
private static checkInputForInitialObject;
|
|
140
|
+
private static processErrorObjectResult;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export { DEFAULT_BUILD_OPTIONS, type ErrorObjectBuildOptions, type ErrorObjectErrorResult, ErrorObjectFromPayload, type ErrorObjectProcessingError, type ErrorObjectTransformState, type ErrorSummary, addPrefixPathVariants };
|
package/dist/index.d.ts
CHANGED
|
@@ -122,13 +122,22 @@ type ErrorObjectProcessingError = {
|
|
|
122
122
|
};
|
|
123
123
|
|
|
124
124
|
/**
|
|
125
|
-
* The {@link
|
|
126
|
-
*
|
|
127
|
-
* Check out the {@link ErrorObjectBuildOptions} type for more details.
|
|
125
|
+
* The {@link ErrorObjectFromPayload} class is an alternative way to create an {@link ErrorObject} from anything
|
|
126
|
+
* resembling an error, even request payloads. It also chains errors using the `.setNextErrors()` method.
|
|
128
127
|
*/
|
|
129
|
-
declare
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
128
|
+
declare class ErrorObjectFromPayload extends ErrorObject {
|
|
129
|
+
static DEFAULT_FALLBACK_TAG: string;
|
|
130
|
+
nextErrors?: ErrorObjectFromPayload[];
|
|
131
|
+
constructor(props: any, withOptions?: Partial<ErrorObjectBuildOptions>);
|
|
132
|
+
static generic: () => ErrorObjectFromPayload;
|
|
133
|
+
protected _log(logTag: string, logLevel: 'log' | 'debug' | 'verbose'): this;
|
|
134
|
+
toVerboseString(): string;
|
|
135
|
+
/**
|
|
136
|
+
* This method allows users to check if an error is a fallback error, returned when an error could not be created from payload.
|
|
137
|
+
*/
|
|
138
|
+
isFallback(): boolean;
|
|
139
|
+
private static checkInputForInitialObject;
|
|
140
|
+
private static processErrorObjectResult;
|
|
141
|
+
}
|
|
133
142
|
|
|
134
|
-
export { DEFAULT_BUILD_OPTIONS, ErrorObjectBuildOptions, ErrorObjectErrorResult, ErrorObjectProcessingError, ErrorObjectTransformState, ErrorSummary, addPrefixPathVariants
|
|
143
|
+
export { DEFAULT_BUILD_OPTIONS, type ErrorObjectBuildOptions, type ErrorObjectErrorResult, ErrorObjectFromPayload, type ErrorObjectProcessingError, type ErrorObjectTransformState, type ErrorSummary, addPrefixPathVariants };
|
package/dist/index.js
CHANGED
|
@@ -35,13 +35,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
35
35
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
36
|
|
|
37
37
|
// src/index.ts
|
|
38
|
-
var
|
|
39
|
-
__export(
|
|
38
|
+
var index_exports = {};
|
|
39
|
+
__export(index_exports, {
|
|
40
40
|
DEFAULT_BUILD_OPTIONS: () => DEFAULT_BUILD_OPTIONS,
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
ErrorObjectFromPayload: () => ErrorObjectFromPayload,
|
|
42
|
+
addPrefixPathVariants: () => addPrefixPathVariants
|
|
43
43
|
});
|
|
44
|
-
module.exports = __toCommonJS(
|
|
44
|
+
module.exports = __toCommonJS(index_exports);
|
|
45
45
|
var import_error_object2 = require("@smbcheeky/error-object");
|
|
46
46
|
|
|
47
47
|
// src/builder/index.ts
|
|
@@ -50,9 +50,7 @@ var import_error_object = require("@smbcheeky/error-object");
|
|
|
50
50
|
// src/utils.ts
|
|
51
51
|
var addPrefixPathVariants = (prefix, array) => {
|
|
52
52
|
if (typeof prefix === "string") {
|
|
53
|
-
return array.concat(
|
|
54
|
-
array.map((s) => typeof s === "string" ? `${prefix}.${s}` : s)
|
|
55
|
-
);
|
|
53
|
+
return array.concat(array.map((s) => typeof s === "string" ? `${prefix}.${s}` : s));
|
|
56
54
|
}
|
|
57
55
|
if (Array.isArray(prefix) && prefix.length > 0 && typeof (prefix == null ? void 0 : prefix[0]) === "string") {
|
|
58
56
|
const result = [...array];
|
|
@@ -66,37 +64,16 @@ var addPrefixPathVariants = (prefix, array) => {
|
|
|
66
64
|
};
|
|
67
65
|
var DEFAULT_BUILD_OPTIONS = {
|
|
68
66
|
pathToErrors: ["errors", "errs"],
|
|
69
|
-
pathToCode: addPrefixPathVariants("error", [
|
|
70
|
-
"code",
|
|
71
|
-
"err_code",
|
|
72
|
-
"errorCode",
|
|
73
|
-
"error_code"
|
|
74
|
-
]),
|
|
67
|
+
pathToCode: addPrefixPathVariants("error", ["code", "err_code", "errorCode", "error_code"]),
|
|
75
68
|
pathToNumberCode: addPrefixPathVariants("error", [
|
|
76
69
|
"numberCode",
|
|
77
70
|
"err_number_code",
|
|
78
71
|
"errorNumberCode",
|
|
79
72
|
"error_number_code"
|
|
80
73
|
]),
|
|
81
|
-
pathToMessage: addPrefixPathVariants("error", [
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
"errorMessage",
|
|
85
|
-
"error_message"
|
|
86
|
-
]),
|
|
87
|
-
pathToDetails: addPrefixPathVariants("error", [
|
|
88
|
-
"details",
|
|
89
|
-
"err_details",
|
|
90
|
-
"errorDetails",
|
|
91
|
-
"error_details"
|
|
92
|
-
]),
|
|
93
|
-
pathToDomain: addPrefixPathVariants("error", [
|
|
94
|
-
"domain",
|
|
95
|
-
"errorDomain",
|
|
96
|
-
"error_domain",
|
|
97
|
-
"err_domain",
|
|
98
|
-
"type"
|
|
99
|
-
]),
|
|
74
|
+
pathToMessage: addPrefixPathVariants("error", ["message", "err_message", "errorMessage", "error_message"]),
|
|
75
|
+
pathToDetails: addPrefixPathVariants("error", ["details", "err_details", "errorDetails", "error_details"]),
|
|
76
|
+
pathToDomain: addPrefixPathVariants("error", ["domain", "errorDomain", "error_domain", "err_domain", "type"]),
|
|
100
77
|
transform: (beforeTransform) => {
|
|
101
78
|
let newCode = beforeTransform.code;
|
|
102
79
|
if (beforeTransform.code === void 0 || beforeTransform.code === null) {
|
|
@@ -285,21 +262,12 @@ var buildSummariesFromObject = (input, withOptions) => {
|
|
|
285
262
|
}
|
|
286
263
|
let summaries = [];
|
|
287
264
|
for (const errorMaybeObject of errors) {
|
|
288
|
-
const summary = buildSummaryFromObject(
|
|
289
|
-
errorMaybeObject,
|
|
290
|
-
errorsPath,
|
|
291
|
-
didDetectErrorsArray,
|
|
292
|
-
options
|
|
293
|
-
);
|
|
265
|
+
const summary = buildSummaryFromObject(errorMaybeObject, errorsPath, didDetectErrorsArray, options);
|
|
294
266
|
summaries.push(summary);
|
|
295
267
|
}
|
|
296
268
|
return summaries;
|
|
297
269
|
} catch (generalError) {
|
|
298
|
-
import_error_object.ErrorObject.SHOW_ERROR_LOGS && console.log(
|
|
299
|
-
"[ErrorObject]",
|
|
300
|
-
"Error during buildSummariesFromObject():",
|
|
301
|
-
generalError
|
|
302
|
-
);
|
|
270
|
+
import_error_object.ErrorObject.SHOW_ERROR_LOGS && console.log("[ErrorObject]", "Error during buildSummariesFromObject():", generalError);
|
|
303
271
|
return ["generalBuildSummariesFromObjectError"];
|
|
304
272
|
}
|
|
305
273
|
};
|
|
@@ -409,11 +377,7 @@ var buildSummaryFromObject = (maybeObject, errorsPath, didDetectErrorsArray, wit
|
|
|
409
377
|
}
|
|
410
378
|
};
|
|
411
379
|
} catch (generalError) {
|
|
412
|
-
import_error_object.ErrorObject.SHOW_ERROR_LOGS && console.log(
|
|
413
|
-
"[ErrorObject]",
|
|
414
|
-
"Error during buildSummaryFromObject():",
|
|
415
|
-
generalError
|
|
416
|
-
);
|
|
380
|
+
import_error_object.ErrorObject.SHOW_ERROR_LOGS && console.log("[ErrorObject]", "Error during buildSummaryFromObject():", generalError);
|
|
417
381
|
return "generalBuildSummaryFromObjectError";
|
|
418
382
|
}
|
|
419
383
|
};
|
|
@@ -434,8 +398,116 @@ var findNestedValueForPath = (value, path) => {
|
|
|
434
398
|
};
|
|
435
399
|
|
|
436
400
|
// src/index.ts
|
|
437
|
-
var
|
|
438
|
-
|
|
401
|
+
var _ErrorObjectFromPayload = class _ErrorObjectFromPayload extends import_error_object2.ErrorObject {
|
|
402
|
+
constructor(props, withOptions) {
|
|
403
|
+
var _a, _b, _c, _d;
|
|
404
|
+
if ("code" in props && props.code !== void 0 && props.code !== null && typeof props.code === "string" && "message" in props && props.message !== void 0 && props.message !== null && typeof props.message === "string") {
|
|
405
|
+
super(props);
|
|
406
|
+
} else {
|
|
407
|
+
const options = __spreadValues(__spreadValues({}, DEFAULT_BUILD_OPTIONS), withOptions != null ? withOptions : {});
|
|
408
|
+
let checksFailed;
|
|
409
|
+
try {
|
|
410
|
+
checksFailed = _ErrorObjectFromPayload.checkInputForInitialObject(props, options);
|
|
411
|
+
} catch (error) {
|
|
412
|
+
super({
|
|
413
|
+
code: "INT-1",
|
|
414
|
+
message: `Error during checkInputForInitialObject(). Details: ${(_b = (_a = error == null ? void 0 : error.toString) == null ? void 0 : _a.call(error)) != null ? _b : import_error_object2.ErrorObject.GENERIC_MESSAGE}}`,
|
|
415
|
+
tag: _ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG,
|
|
416
|
+
raw: {
|
|
417
|
+
error
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (checksFailed !== void 0) {
|
|
423
|
+
super({
|
|
424
|
+
code: import_error_object2.ErrorObject.GENERIC_CODE,
|
|
425
|
+
message: import_error_object2.ErrorObject.GENERIC_MESSAGE,
|
|
426
|
+
tag: _ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG,
|
|
427
|
+
raw: {
|
|
428
|
+
processingErrors: [{ errorCode: checksFailed, summary: void 0 }]
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
try {
|
|
434
|
+
const { validErrors, summaries, processingErrors } = _ErrorObjectFromPayload.processErrorObjectResult(
|
|
435
|
+
props,
|
|
436
|
+
options
|
|
437
|
+
);
|
|
438
|
+
if (validErrors.length > 0) {
|
|
439
|
+
const [firstError, ...rawNextErrors] = validErrors;
|
|
440
|
+
if (firstError) {
|
|
441
|
+
super(__spreadProps(__spreadValues({}, firstError), {
|
|
442
|
+
raw: {
|
|
443
|
+
processingErrors,
|
|
444
|
+
summary: summaries,
|
|
445
|
+
value: props
|
|
446
|
+
}
|
|
447
|
+
}));
|
|
448
|
+
const nextErrors = rawNextErrors && (rawNextErrors == null ? void 0 : rawNextErrors.length) > 0 ? rawNextErrors.map((p) => new _ErrorObjectFromPayload(p)) : void 0;
|
|
449
|
+
if (nextErrors !== void 0) {
|
|
450
|
+
this.nextErrors = nextErrors;
|
|
451
|
+
}
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
super({
|
|
456
|
+
code: import_error_object2.ErrorObject.GENERIC_CODE,
|
|
457
|
+
message: import_error_object2.ErrorObject.GENERIC_MESSAGE,
|
|
458
|
+
tag: _ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG,
|
|
459
|
+
raw: {
|
|
460
|
+
processingErrors: processingErrors.length > 0 ? processingErrors : void 0,
|
|
461
|
+
summary: summaries
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
} catch (error) {
|
|
465
|
+
super({
|
|
466
|
+
code: "INT-2",
|
|
467
|
+
message: `Error during processErrorObjectResult(). Details: ${(_d = (_c = error == null ? void 0 : error.toString) == null ? void 0 : _c.call(error)) != null ? _d : import_error_object2.ErrorObject.GENERIC_MESSAGE}}`,
|
|
468
|
+
tag: _ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG,
|
|
469
|
+
raw: {
|
|
470
|
+
error
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
_log(logTag, logLevel) {
|
|
477
|
+
const logForThis = logLevel === "verbose" ? this.toVerboseString() : logLevel === "debug" ? this.toDebugString() : this.toString();
|
|
478
|
+
if (Array.isArray(this.nextErrors) && this.nextErrors.length > 0) {
|
|
479
|
+
let row = 1;
|
|
480
|
+
import_error_object2.ErrorObject.LOG_METHOD && typeof import_error_object2.ErrorObject.LOG_METHOD === "function" && import_error_object2.ErrorObject.LOG_METHOD(`[${logTag}][${row}]`, logForThis);
|
|
481
|
+
for (const error of this.nextErrors) {
|
|
482
|
+
row++;
|
|
483
|
+
import_error_object2.ErrorObject.LOG_METHOD && typeof import_error_object2.ErrorObject.LOG_METHOD === "function" && import_error_object2.ErrorObject.LOG_METHOD(
|
|
484
|
+
`[${logTag}][${row}]`,
|
|
485
|
+
logLevel === "verbose" ? error.toVerboseString() : logLevel === "debug" ? error.toDebugString() : error.toString()
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
} else {
|
|
489
|
+
import_error_object2.ErrorObject.LOG_METHOD && typeof import_error_object2.ErrorObject.LOG_METHOD === "function" && import_error_object2.ErrorObject.LOG_METHOD(`[${logTag}]`, logForThis);
|
|
490
|
+
}
|
|
491
|
+
return this;
|
|
492
|
+
}
|
|
493
|
+
toVerboseString() {
|
|
494
|
+
return this.toDebugString() + `
|
|
495
|
+
${JSON.stringify(
|
|
496
|
+
{
|
|
497
|
+
raw: this.raw,
|
|
498
|
+
nextErrors: this.nextErrors
|
|
499
|
+
},
|
|
500
|
+
null,
|
|
501
|
+
2
|
|
502
|
+
)}`;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* This method allows users to check if an error is a fallback error, returned when an error could not be created from payload.
|
|
506
|
+
*/
|
|
507
|
+
isFallback() {
|
|
508
|
+
return this.tag === _ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG;
|
|
509
|
+
}
|
|
510
|
+
static checkInputForInitialObject(input, withOptions) {
|
|
439
511
|
const options = withOptions != null ? withOptions : __spreadValues({}, DEFAULT_BUILD_OPTIONS);
|
|
440
512
|
if (input === void 0 || input === null) {
|
|
441
513
|
return "checkIsNullish";
|
|
@@ -447,9 +519,7 @@ var checkInputForInitialObject = (input, withOptions) => {
|
|
|
447
519
|
if (typeof options.checkInputObjectForValues !== "object") {
|
|
448
520
|
return "checkInputObjectForValuesIsNotAnObject";
|
|
449
521
|
}
|
|
450
|
-
const checkInputObjectForValues = Object.entries(
|
|
451
|
-
options.checkInputObjectForValues
|
|
452
|
-
);
|
|
522
|
+
const checkInputObjectForValues = Object.entries(options.checkInputObjectForValues);
|
|
453
523
|
for (const [key, rule] of checkInputObjectForValues) {
|
|
454
524
|
const foundValue = findNestedValueForPath(input, key);
|
|
455
525
|
if (rule.exists ? foundValue !== rule.value : foundValue === rule.value) {
|
|
@@ -461,9 +531,7 @@ var checkInputForInitialObject = (input, withOptions) => {
|
|
|
461
531
|
if (typeof options.checkInputObjectForTypes !== "object") {
|
|
462
532
|
return "checkInputObjectForTypesIsNotAnObject";
|
|
463
533
|
}
|
|
464
|
-
const checkInputObjectForTypes = Object.entries(
|
|
465
|
-
options.checkInputObjectForTypes
|
|
466
|
-
);
|
|
534
|
+
const checkInputObjectForTypes = Object.entries(options.checkInputObjectForTypes);
|
|
467
535
|
for (const [key, rule] of checkInputObjectForTypes) {
|
|
468
536
|
const foundValue = findNestedValueForPath(input, key);
|
|
469
537
|
if (rule.valueIsArray) {
|
|
@@ -480,9 +548,7 @@ var checkInputForInitialObject = (input, withOptions) => {
|
|
|
480
548
|
if (typeof options.checkInputObjectForKeys !== "object") {
|
|
481
549
|
return "checkInputObjectForKeysIsNotAnObject";
|
|
482
550
|
}
|
|
483
|
-
const checkInputObjectForKeys = Object.entries(
|
|
484
|
-
options.checkInputObjectForKeys
|
|
485
|
-
);
|
|
551
|
+
const checkInputObjectForKeys = Object.entries(options.checkInputObjectForKeys);
|
|
486
552
|
for (const [key, rule] of checkInputObjectForKeys) {
|
|
487
553
|
const foundValue = findNestedValueForPath(input, key);
|
|
488
554
|
if (rule.exists ? !foundValue : foundValue) {
|
|
@@ -490,18 +556,16 @@ var checkInputForInitialObject = (input, withOptions) => {
|
|
|
490
556
|
}
|
|
491
557
|
}
|
|
492
558
|
}
|
|
493
|
-
|
|
494
|
-
import_error_object2.ErrorObject.SHOW_ERROR_LOGS && console.log(
|
|
495
|
-
"[ErrorObject]",
|
|
496
|
-
"Error during checkInputForInitialObject():",
|
|
497
|
-
error
|
|
498
|
-
);
|
|
559
|
+
return void 0;
|
|
499
560
|
}
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
561
|
+
static processErrorObjectResult(props, options) {
|
|
562
|
+
let summaries;
|
|
563
|
+
if ("pathToErrors" in options) {
|
|
564
|
+
summaries = buildSummariesFromObject(props, options);
|
|
565
|
+
} else {
|
|
566
|
+
summaries = [buildSummaryFromObject(props, void 0, false, options)];
|
|
567
|
+
}
|
|
568
|
+
const processingErrors = [];
|
|
505
569
|
const validErrors = summaries.filter((summary) => {
|
|
506
570
|
var _a, _b;
|
|
507
571
|
if (typeof summary === "string") {
|
|
@@ -528,70 +592,43 @@ var processErrorObjectResult = (summaries, raw, fallbackError) => {
|
|
|
528
592
|
var _a, _b, _c, _d, _e;
|
|
529
593
|
const summary = s;
|
|
530
594
|
const code = (_a = summary.value.code) == null ? void 0 : _a.value;
|
|
531
|
-
const
|
|
532
|
-
const message = (_c = summary.value.message) == null ? void 0 : _c.value;
|
|
533
|
-
const details = (_d = summary.value.details) == null ? void 0 : _d.value;
|
|
534
|
-
const domain = (_e = summary.value.domain) == null ? void 0 : _e.value;
|
|
595
|
+
const message = (_b = summary.value.message) == null ? void 0 : _b.value;
|
|
535
596
|
if (code !== void 0 && code !== null && typeof code === "string") {
|
|
536
597
|
if (message !== void 0 && message !== null && typeof message === "string") {
|
|
537
|
-
return
|
|
598
|
+
return {
|
|
538
599
|
code,
|
|
539
600
|
message,
|
|
540
|
-
numberCode,
|
|
541
|
-
details,
|
|
542
|
-
domain,
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
601
|
+
numberCode: (_c = summary.value.numberCode) == null ? void 0 : _c.value,
|
|
602
|
+
details: (_d = summary.value.details) == null ? void 0 : _d.value,
|
|
603
|
+
domain: (_e = summary.value.domain) == null ? void 0 : _e.value,
|
|
604
|
+
raw: {
|
|
605
|
+
value: props,
|
|
606
|
+
processingErrors,
|
|
607
|
+
summary
|
|
608
|
+
}
|
|
609
|
+
};
|
|
546
610
|
}
|
|
547
611
|
}
|
|
548
612
|
return null;
|
|
549
|
-
}).filter(
|
|
550
|
-
(s) => s !== null && s !== void 0 && s instanceof import_error_object2.ErrorObject
|
|
551
|
-
);
|
|
552
|
-
if (validErrors.length > 0) {
|
|
553
|
-
const [firstError, ...nextErrors] = validErrors;
|
|
554
|
-
const error = firstError.setNextErrors((nextErrors == null ? void 0 : nextErrors.length) > 0 ? nextErrors : void 0).setRaw(raw);
|
|
555
|
-
return {
|
|
556
|
-
error,
|
|
557
|
-
force: error != null ? error : fallbackError.setProcessingErrors(processingErrors)
|
|
558
|
-
};
|
|
559
|
-
}
|
|
560
|
-
} catch (error) {
|
|
561
|
-
import_error_object2.ErrorObject.SHOW_ERROR_LOGS && console.log(
|
|
562
|
-
"[ErrorObject]",
|
|
563
|
-
"Error during processErrorObjectResult():",
|
|
564
|
-
error
|
|
565
|
-
);
|
|
566
|
-
}
|
|
567
|
-
return {
|
|
568
|
-
error: void 0,
|
|
569
|
-
force: processingErrors.length > 0 ? fallbackError.setProcessingErrors(processingErrors) : fallbackError
|
|
570
|
-
};
|
|
571
|
-
};
|
|
572
|
-
var fromPayload = (value, withOptions) => {
|
|
573
|
-
const options = __spreadValues(__spreadValues({}, DEFAULT_BUILD_OPTIONS), withOptions != null ? withOptions : {});
|
|
574
|
-
const fallbackError = import_error_object2.ErrorObject.fallback().setRaw(value);
|
|
575
|
-
const checksFailed = checkInputForInitialObject(value, options);
|
|
576
|
-
if (checksFailed !== void 0) {
|
|
613
|
+
}).filter((s) => s !== null && s !== void 0);
|
|
577
614
|
return {
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
])
|
|
615
|
+
validErrors,
|
|
616
|
+
summaries,
|
|
617
|
+
processingErrors
|
|
582
618
|
};
|
|
583
619
|
}
|
|
584
|
-
let summaries;
|
|
585
|
-
if ("pathToErrors" in options) {
|
|
586
|
-
summaries = buildSummariesFromObject(value, options);
|
|
587
|
-
} else {
|
|
588
|
-
summaries = [buildSummaryFromObject(value, void 0, false, options)];
|
|
589
|
-
}
|
|
590
|
-
return processErrorObjectResult(summaries, value, fallbackError);
|
|
591
620
|
};
|
|
621
|
+
_ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG = "fallback-error-object";
|
|
622
|
+
// Overrides
|
|
623
|
+
_ErrorObjectFromPayload.generic = () => new _ErrorObjectFromPayload({
|
|
624
|
+
code: import_error_object2.ErrorObject.GENERIC_CODE,
|
|
625
|
+
message: import_error_object2.ErrorObject.GENERIC_MESSAGE,
|
|
626
|
+
tag: import_error_object2.ErrorObject.GENERIC_TAG
|
|
627
|
+
});
|
|
628
|
+
var ErrorObjectFromPayload = _ErrorObjectFromPayload;
|
|
592
629
|
// Annotate the CommonJS export names for ESM import in node:
|
|
593
630
|
0 && (module.exports = {
|
|
594
631
|
DEFAULT_BUILD_OPTIONS,
|
|
595
|
-
|
|
596
|
-
|
|
632
|
+
ErrorObjectFromPayload,
|
|
633
|
+
addPrefixPathVariants
|
|
597
634
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -27,9 +27,7 @@ import { ErrorObject } from "@smbcheeky/error-object";
|
|
|
27
27
|
// src/utils.ts
|
|
28
28
|
var addPrefixPathVariants = (prefix, array) => {
|
|
29
29
|
if (typeof prefix === "string") {
|
|
30
|
-
return array.concat(
|
|
31
|
-
array.map((s) => typeof s === "string" ? `${prefix}.${s}` : s)
|
|
32
|
-
);
|
|
30
|
+
return array.concat(array.map((s) => typeof s === "string" ? `${prefix}.${s}` : s));
|
|
33
31
|
}
|
|
34
32
|
if (Array.isArray(prefix) && prefix.length > 0 && typeof (prefix == null ? void 0 : prefix[0]) === "string") {
|
|
35
33
|
const result = [...array];
|
|
@@ -43,37 +41,16 @@ var addPrefixPathVariants = (prefix, array) => {
|
|
|
43
41
|
};
|
|
44
42
|
var DEFAULT_BUILD_OPTIONS = {
|
|
45
43
|
pathToErrors: ["errors", "errs"],
|
|
46
|
-
pathToCode: addPrefixPathVariants("error", [
|
|
47
|
-
"code",
|
|
48
|
-
"err_code",
|
|
49
|
-
"errorCode",
|
|
50
|
-
"error_code"
|
|
51
|
-
]),
|
|
44
|
+
pathToCode: addPrefixPathVariants("error", ["code", "err_code", "errorCode", "error_code"]),
|
|
52
45
|
pathToNumberCode: addPrefixPathVariants("error", [
|
|
53
46
|
"numberCode",
|
|
54
47
|
"err_number_code",
|
|
55
48
|
"errorNumberCode",
|
|
56
49
|
"error_number_code"
|
|
57
50
|
]),
|
|
58
|
-
pathToMessage: addPrefixPathVariants("error", [
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
"errorMessage",
|
|
62
|
-
"error_message"
|
|
63
|
-
]),
|
|
64
|
-
pathToDetails: addPrefixPathVariants("error", [
|
|
65
|
-
"details",
|
|
66
|
-
"err_details",
|
|
67
|
-
"errorDetails",
|
|
68
|
-
"error_details"
|
|
69
|
-
]),
|
|
70
|
-
pathToDomain: addPrefixPathVariants("error", [
|
|
71
|
-
"domain",
|
|
72
|
-
"errorDomain",
|
|
73
|
-
"error_domain",
|
|
74
|
-
"err_domain",
|
|
75
|
-
"type"
|
|
76
|
-
]),
|
|
51
|
+
pathToMessage: addPrefixPathVariants("error", ["message", "err_message", "errorMessage", "error_message"]),
|
|
52
|
+
pathToDetails: addPrefixPathVariants("error", ["details", "err_details", "errorDetails", "error_details"]),
|
|
53
|
+
pathToDomain: addPrefixPathVariants("error", ["domain", "errorDomain", "error_domain", "err_domain", "type"]),
|
|
77
54
|
transform: (beforeTransform) => {
|
|
78
55
|
let newCode = beforeTransform.code;
|
|
79
56
|
if (beforeTransform.code === void 0 || beforeTransform.code === null) {
|
|
@@ -262,21 +239,12 @@ var buildSummariesFromObject = (input, withOptions) => {
|
|
|
262
239
|
}
|
|
263
240
|
let summaries = [];
|
|
264
241
|
for (const errorMaybeObject of errors) {
|
|
265
|
-
const summary = buildSummaryFromObject(
|
|
266
|
-
errorMaybeObject,
|
|
267
|
-
errorsPath,
|
|
268
|
-
didDetectErrorsArray,
|
|
269
|
-
options
|
|
270
|
-
);
|
|
242
|
+
const summary = buildSummaryFromObject(errorMaybeObject, errorsPath, didDetectErrorsArray, options);
|
|
271
243
|
summaries.push(summary);
|
|
272
244
|
}
|
|
273
245
|
return summaries;
|
|
274
246
|
} catch (generalError) {
|
|
275
|
-
ErrorObject.SHOW_ERROR_LOGS && console.log(
|
|
276
|
-
"[ErrorObject]",
|
|
277
|
-
"Error during buildSummariesFromObject():",
|
|
278
|
-
generalError
|
|
279
|
-
);
|
|
247
|
+
ErrorObject.SHOW_ERROR_LOGS && console.log("[ErrorObject]", "Error during buildSummariesFromObject():", generalError);
|
|
280
248
|
return ["generalBuildSummariesFromObjectError"];
|
|
281
249
|
}
|
|
282
250
|
};
|
|
@@ -386,11 +354,7 @@ var buildSummaryFromObject = (maybeObject, errorsPath, didDetectErrorsArray, wit
|
|
|
386
354
|
}
|
|
387
355
|
};
|
|
388
356
|
} catch (generalError) {
|
|
389
|
-
ErrorObject.SHOW_ERROR_LOGS && console.log(
|
|
390
|
-
"[ErrorObject]",
|
|
391
|
-
"Error during buildSummaryFromObject():",
|
|
392
|
-
generalError
|
|
393
|
-
);
|
|
357
|
+
ErrorObject.SHOW_ERROR_LOGS && console.log("[ErrorObject]", "Error during buildSummaryFromObject():", generalError);
|
|
394
358
|
return "generalBuildSummaryFromObjectError";
|
|
395
359
|
}
|
|
396
360
|
};
|
|
@@ -411,8 +375,116 @@ var findNestedValueForPath = (value, path) => {
|
|
|
411
375
|
};
|
|
412
376
|
|
|
413
377
|
// src/index.ts
|
|
414
|
-
var
|
|
415
|
-
|
|
378
|
+
var _ErrorObjectFromPayload = class _ErrorObjectFromPayload extends ErrorObject2 {
|
|
379
|
+
constructor(props, withOptions) {
|
|
380
|
+
var _a, _b, _c, _d;
|
|
381
|
+
if ("code" in props && props.code !== void 0 && props.code !== null && typeof props.code === "string" && "message" in props && props.message !== void 0 && props.message !== null && typeof props.message === "string") {
|
|
382
|
+
super(props);
|
|
383
|
+
} else {
|
|
384
|
+
const options = __spreadValues(__spreadValues({}, DEFAULT_BUILD_OPTIONS), withOptions != null ? withOptions : {});
|
|
385
|
+
let checksFailed;
|
|
386
|
+
try {
|
|
387
|
+
checksFailed = _ErrorObjectFromPayload.checkInputForInitialObject(props, options);
|
|
388
|
+
} catch (error) {
|
|
389
|
+
super({
|
|
390
|
+
code: "INT-1",
|
|
391
|
+
message: `Error during checkInputForInitialObject(). Details: ${(_b = (_a = error == null ? void 0 : error.toString) == null ? void 0 : _a.call(error)) != null ? _b : ErrorObject2.GENERIC_MESSAGE}}`,
|
|
392
|
+
tag: _ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG,
|
|
393
|
+
raw: {
|
|
394
|
+
error
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (checksFailed !== void 0) {
|
|
400
|
+
super({
|
|
401
|
+
code: ErrorObject2.GENERIC_CODE,
|
|
402
|
+
message: ErrorObject2.GENERIC_MESSAGE,
|
|
403
|
+
tag: _ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG,
|
|
404
|
+
raw: {
|
|
405
|
+
processingErrors: [{ errorCode: checksFailed, summary: void 0 }]
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
const { validErrors, summaries, processingErrors } = _ErrorObjectFromPayload.processErrorObjectResult(
|
|
412
|
+
props,
|
|
413
|
+
options
|
|
414
|
+
);
|
|
415
|
+
if (validErrors.length > 0) {
|
|
416
|
+
const [firstError, ...rawNextErrors] = validErrors;
|
|
417
|
+
if (firstError) {
|
|
418
|
+
super(__spreadProps(__spreadValues({}, firstError), {
|
|
419
|
+
raw: {
|
|
420
|
+
processingErrors,
|
|
421
|
+
summary: summaries,
|
|
422
|
+
value: props
|
|
423
|
+
}
|
|
424
|
+
}));
|
|
425
|
+
const nextErrors = rawNextErrors && (rawNextErrors == null ? void 0 : rawNextErrors.length) > 0 ? rawNextErrors.map((p) => new _ErrorObjectFromPayload(p)) : void 0;
|
|
426
|
+
if (nextErrors !== void 0) {
|
|
427
|
+
this.nextErrors = nextErrors;
|
|
428
|
+
}
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
super({
|
|
433
|
+
code: ErrorObject2.GENERIC_CODE,
|
|
434
|
+
message: ErrorObject2.GENERIC_MESSAGE,
|
|
435
|
+
tag: _ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG,
|
|
436
|
+
raw: {
|
|
437
|
+
processingErrors: processingErrors.length > 0 ? processingErrors : void 0,
|
|
438
|
+
summary: summaries
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
} catch (error) {
|
|
442
|
+
super({
|
|
443
|
+
code: "INT-2",
|
|
444
|
+
message: `Error during processErrorObjectResult(). Details: ${(_d = (_c = error == null ? void 0 : error.toString) == null ? void 0 : _c.call(error)) != null ? _d : ErrorObject2.GENERIC_MESSAGE}}`,
|
|
445
|
+
tag: _ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG,
|
|
446
|
+
raw: {
|
|
447
|
+
error
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
_log(logTag, logLevel) {
|
|
454
|
+
const logForThis = logLevel === "verbose" ? this.toVerboseString() : logLevel === "debug" ? this.toDebugString() : this.toString();
|
|
455
|
+
if (Array.isArray(this.nextErrors) && this.nextErrors.length > 0) {
|
|
456
|
+
let row = 1;
|
|
457
|
+
ErrorObject2.LOG_METHOD && typeof ErrorObject2.LOG_METHOD === "function" && ErrorObject2.LOG_METHOD(`[${logTag}][${row}]`, logForThis);
|
|
458
|
+
for (const error of this.nextErrors) {
|
|
459
|
+
row++;
|
|
460
|
+
ErrorObject2.LOG_METHOD && typeof ErrorObject2.LOG_METHOD === "function" && ErrorObject2.LOG_METHOD(
|
|
461
|
+
`[${logTag}][${row}]`,
|
|
462
|
+
logLevel === "verbose" ? error.toVerboseString() : logLevel === "debug" ? error.toDebugString() : error.toString()
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
} else {
|
|
466
|
+
ErrorObject2.LOG_METHOD && typeof ErrorObject2.LOG_METHOD === "function" && ErrorObject2.LOG_METHOD(`[${logTag}]`, logForThis);
|
|
467
|
+
}
|
|
468
|
+
return this;
|
|
469
|
+
}
|
|
470
|
+
toVerboseString() {
|
|
471
|
+
return this.toDebugString() + `
|
|
472
|
+
${JSON.stringify(
|
|
473
|
+
{
|
|
474
|
+
raw: this.raw,
|
|
475
|
+
nextErrors: this.nextErrors
|
|
476
|
+
},
|
|
477
|
+
null,
|
|
478
|
+
2
|
|
479
|
+
)}`;
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* This method allows users to check if an error is a fallback error, returned when an error could not be created from payload.
|
|
483
|
+
*/
|
|
484
|
+
isFallback() {
|
|
485
|
+
return this.tag === _ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG;
|
|
486
|
+
}
|
|
487
|
+
static checkInputForInitialObject(input, withOptions) {
|
|
416
488
|
const options = withOptions != null ? withOptions : __spreadValues({}, DEFAULT_BUILD_OPTIONS);
|
|
417
489
|
if (input === void 0 || input === null) {
|
|
418
490
|
return "checkIsNullish";
|
|
@@ -424,9 +496,7 @@ var checkInputForInitialObject = (input, withOptions) => {
|
|
|
424
496
|
if (typeof options.checkInputObjectForValues !== "object") {
|
|
425
497
|
return "checkInputObjectForValuesIsNotAnObject";
|
|
426
498
|
}
|
|
427
|
-
const checkInputObjectForValues = Object.entries(
|
|
428
|
-
options.checkInputObjectForValues
|
|
429
|
-
);
|
|
499
|
+
const checkInputObjectForValues = Object.entries(options.checkInputObjectForValues);
|
|
430
500
|
for (const [key, rule] of checkInputObjectForValues) {
|
|
431
501
|
const foundValue = findNestedValueForPath(input, key);
|
|
432
502
|
if (rule.exists ? foundValue !== rule.value : foundValue === rule.value) {
|
|
@@ -438,9 +508,7 @@ var checkInputForInitialObject = (input, withOptions) => {
|
|
|
438
508
|
if (typeof options.checkInputObjectForTypes !== "object") {
|
|
439
509
|
return "checkInputObjectForTypesIsNotAnObject";
|
|
440
510
|
}
|
|
441
|
-
const checkInputObjectForTypes = Object.entries(
|
|
442
|
-
options.checkInputObjectForTypes
|
|
443
|
-
);
|
|
511
|
+
const checkInputObjectForTypes = Object.entries(options.checkInputObjectForTypes);
|
|
444
512
|
for (const [key, rule] of checkInputObjectForTypes) {
|
|
445
513
|
const foundValue = findNestedValueForPath(input, key);
|
|
446
514
|
if (rule.valueIsArray) {
|
|
@@ -457,9 +525,7 @@ var checkInputForInitialObject = (input, withOptions) => {
|
|
|
457
525
|
if (typeof options.checkInputObjectForKeys !== "object") {
|
|
458
526
|
return "checkInputObjectForKeysIsNotAnObject";
|
|
459
527
|
}
|
|
460
|
-
const checkInputObjectForKeys = Object.entries(
|
|
461
|
-
options.checkInputObjectForKeys
|
|
462
|
-
);
|
|
528
|
+
const checkInputObjectForKeys = Object.entries(options.checkInputObjectForKeys);
|
|
463
529
|
for (const [key, rule] of checkInputObjectForKeys) {
|
|
464
530
|
const foundValue = findNestedValueForPath(input, key);
|
|
465
531
|
if (rule.exists ? !foundValue : foundValue) {
|
|
@@ -467,18 +533,16 @@ var checkInputForInitialObject = (input, withOptions) => {
|
|
|
467
533
|
}
|
|
468
534
|
}
|
|
469
535
|
}
|
|
470
|
-
|
|
471
|
-
ErrorObject2.SHOW_ERROR_LOGS && console.log(
|
|
472
|
-
"[ErrorObject]",
|
|
473
|
-
"Error during checkInputForInitialObject():",
|
|
474
|
-
error
|
|
475
|
-
);
|
|
536
|
+
return void 0;
|
|
476
537
|
}
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
538
|
+
static processErrorObjectResult(props, options) {
|
|
539
|
+
let summaries;
|
|
540
|
+
if ("pathToErrors" in options) {
|
|
541
|
+
summaries = buildSummariesFromObject(props, options);
|
|
542
|
+
} else {
|
|
543
|
+
summaries = [buildSummaryFromObject(props, void 0, false, options)];
|
|
544
|
+
}
|
|
545
|
+
const processingErrors = [];
|
|
482
546
|
const validErrors = summaries.filter((summary) => {
|
|
483
547
|
var _a, _b;
|
|
484
548
|
if (typeof summary === "string") {
|
|
@@ -505,69 +569,42 @@ var processErrorObjectResult = (summaries, raw, fallbackError) => {
|
|
|
505
569
|
var _a, _b, _c, _d, _e;
|
|
506
570
|
const summary = s;
|
|
507
571
|
const code = (_a = summary.value.code) == null ? void 0 : _a.value;
|
|
508
|
-
const
|
|
509
|
-
const message = (_c = summary.value.message) == null ? void 0 : _c.value;
|
|
510
|
-
const details = (_d = summary.value.details) == null ? void 0 : _d.value;
|
|
511
|
-
const domain = (_e = summary.value.domain) == null ? void 0 : _e.value;
|
|
572
|
+
const message = (_b = summary.value.message) == null ? void 0 : _b.value;
|
|
512
573
|
if (code !== void 0 && code !== null && typeof code === "string") {
|
|
513
574
|
if (message !== void 0 && message !== null && typeof message === "string") {
|
|
514
|
-
return
|
|
575
|
+
return {
|
|
515
576
|
code,
|
|
516
577
|
message,
|
|
517
|
-
numberCode,
|
|
518
|
-
details,
|
|
519
|
-
domain,
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
578
|
+
numberCode: (_c = summary.value.numberCode) == null ? void 0 : _c.value,
|
|
579
|
+
details: (_d = summary.value.details) == null ? void 0 : _d.value,
|
|
580
|
+
domain: (_e = summary.value.domain) == null ? void 0 : _e.value,
|
|
581
|
+
raw: {
|
|
582
|
+
value: props,
|
|
583
|
+
processingErrors,
|
|
584
|
+
summary
|
|
585
|
+
}
|
|
586
|
+
};
|
|
523
587
|
}
|
|
524
588
|
}
|
|
525
589
|
return null;
|
|
526
|
-
}).filter(
|
|
527
|
-
(s) => s !== null && s !== void 0 && s instanceof ErrorObject2
|
|
528
|
-
);
|
|
529
|
-
if (validErrors.length > 0) {
|
|
530
|
-
const [firstError, ...nextErrors] = validErrors;
|
|
531
|
-
const error = firstError.setNextErrors((nextErrors == null ? void 0 : nextErrors.length) > 0 ? nextErrors : void 0).setRaw(raw);
|
|
532
|
-
return {
|
|
533
|
-
error,
|
|
534
|
-
force: error != null ? error : fallbackError.setProcessingErrors(processingErrors)
|
|
535
|
-
};
|
|
536
|
-
}
|
|
537
|
-
} catch (error) {
|
|
538
|
-
ErrorObject2.SHOW_ERROR_LOGS && console.log(
|
|
539
|
-
"[ErrorObject]",
|
|
540
|
-
"Error during processErrorObjectResult():",
|
|
541
|
-
error
|
|
542
|
-
);
|
|
543
|
-
}
|
|
544
|
-
return {
|
|
545
|
-
error: void 0,
|
|
546
|
-
force: processingErrors.length > 0 ? fallbackError.setProcessingErrors(processingErrors) : fallbackError
|
|
547
|
-
};
|
|
548
|
-
};
|
|
549
|
-
var fromPayload = (value, withOptions) => {
|
|
550
|
-
const options = __spreadValues(__spreadValues({}, DEFAULT_BUILD_OPTIONS), withOptions != null ? withOptions : {});
|
|
551
|
-
const fallbackError = ErrorObject2.fallback().setRaw(value);
|
|
552
|
-
const checksFailed = checkInputForInitialObject(value, options);
|
|
553
|
-
if (checksFailed !== void 0) {
|
|
590
|
+
}).filter((s) => s !== null && s !== void 0);
|
|
554
591
|
return {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
])
|
|
592
|
+
validErrors,
|
|
593
|
+
summaries,
|
|
594
|
+
processingErrors
|
|
559
595
|
};
|
|
560
596
|
}
|
|
561
|
-
let summaries;
|
|
562
|
-
if ("pathToErrors" in options) {
|
|
563
|
-
summaries = buildSummariesFromObject(value, options);
|
|
564
|
-
} else {
|
|
565
|
-
summaries = [buildSummaryFromObject(value, void 0, false, options)];
|
|
566
|
-
}
|
|
567
|
-
return processErrorObjectResult(summaries, value, fallbackError);
|
|
568
597
|
};
|
|
598
|
+
_ErrorObjectFromPayload.DEFAULT_FALLBACK_TAG = "fallback-error-object";
|
|
599
|
+
// Overrides
|
|
600
|
+
_ErrorObjectFromPayload.generic = () => new _ErrorObjectFromPayload({
|
|
601
|
+
code: ErrorObject2.GENERIC_CODE,
|
|
602
|
+
message: ErrorObject2.GENERIC_MESSAGE,
|
|
603
|
+
tag: ErrorObject2.GENERIC_TAG
|
|
604
|
+
});
|
|
605
|
+
var ErrorObjectFromPayload = _ErrorObjectFromPayload;
|
|
569
606
|
export {
|
|
570
607
|
DEFAULT_BUILD_OPTIONS,
|
|
571
|
-
|
|
572
|
-
|
|
608
|
+
ErrorObjectFromPayload,
|
|
609
|
+
addPrefixPathVariants
|
|
573
610
|
};
|
package/package.json
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smbcheeky/error-object-from-payload",
|
|
3
3
|
"description": "Create ErrorObjects from any payload with simple rules and benefit from step-to-step debugging logs for the entire process.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
10
10
|
"pack": "yarn build && npm pack",
|
|
11
|
-
"release": "yarn build && npm publish --access public",
|
|
11
|
+
"release": "yarn build && npm login && npm publish --access public",
|
|
12
12
|
"lint": "tsc --noEmit"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@smbcheeky/error-object": "
|
|
15
|
+
"@smbcheeky/error-object": "1.2.0"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"tsup": "
|
|
19
|
-
"typescript": "^4.9.4"
|
|
18
|
+
"tsup": "8.3.5",
|
|
19
|
+
"typescript": "^4.9.4",
|
|
20
|
+
"prettier": "^3.7.4"
|
|
20
21
|
},
|
|
21
22
|
"license": "MIT",
|
|
22
23
|
"private": false,
|