Skip to content

Commit 154fb57

Browse files
committed
Remove .fromPayload functionality from main package
1 parent 1e10af4 commit 154fb57

File tree

9 files changed

+24
-2494
lines changed

9 files changed

+24
-2494
lines changed

README.md

Lines changed: 3 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@
44
[![GitHub last commit](https://img.shields.io/github/last-commit/smbcheeky/error-object)](https://github.com/smbcheeky/error-object)
55
[![GitHub stars](https://img.shields.io/github/stars/smbcheeky/error-object)](https://img.shields.io/github/stars/smbcheeky/error-object)
66

7-
## TL;DR
8-
9-
- Install the package `npm install @smbcheeky/error-object`
10-
- Write `ErrorObject.from(<pick an api response with an error>).force.verboseLog('LOG')` and use the info provided to
11-
map your error
12-
- Switch the .force with .error, and now you have an error object
13-
- :tada:
14-
- oh... and check the [playground](https://github.com/SMBCheeky/error-object/blob/main/playground/index.ts) file
15-
167
## Installation
178

189
`npm install @smbcheeky/error-object`
@@ -45,42 +36,10 @@ new features:
4536
- Use `details`, `domain` and `tag` to customize the error object and help easily distinguish between different
4637
errors
4738

48-
## ErrorObject.from
49-
50-
Use `ErrorObject.from(<anything>)` to create errors from any input:
51-
52-
- you can pass an object or a caught error to it, and it will try its best to create an error from it
53-
- `ErrorObject.from(<anything>)` returns an object with two properties: `.error` and `.force`
54-
- `.error` represents the error, if it can be created, otherwise it is `undefined`
55-
- `.force` represents the error, if it can be created, otherwise it is going to return a `ErrorObject.fallback()`
56-
error
39+
## fromPayload()
5740

58-
The processing of the ErrorObject is done in a few steps, based on the `ErrorObjectBuildOptions`:
59-
60-
- first the initial object is checked via the options `checkInputObjectForValues` and `checkInputObjectForTypes` and
61-
`checkInputObjectForKeys`
62-
- then the objects checks for an object array at `pathToErrors`, which could be an array of errors
63-
- if an error array is found, the process will consider all other paths relative to the objects in the error array found
64-
- if an error array is not found, the process will consider all other paths absolute to the initial objectpassed to
65-
`ErrorObject.from()`
66-
- the `pathToCode`, `pathToNumberCode`, `pathToMessage`, `pathToDetails` and `pathToDomain` options are used to map
67-
values to their associated field, if found
68-
- 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
69-
or an object it will be JSON.stringify'ed and saved as a string
70-
- for `numberCode`, if a value is found and it is a number different than `NaN`, it is saved
71-
- the `transform` function is used to transform the found values by the parsing process into the error object
72-
- the transform function has access to all pre-transformation values and also the initial object (object inside the
73-
errors array or initial object)
74-
- everything gets processed into a list of `ErrorSummary | ErrorObjectErrorResult` array
75-
- it contains everything, from error strings custom-made to be as distinct and easy to read as possible, to self
76-
documenting summaries of what values are found, at which path, if an errors object was found, etc.
77-
- the count of the list is meant to be an indication of how many input objects were found and processed, as each of them
78-
should become an error object
79-
- in the last step of the process, the list is filtered down and a single error object is created, with everything baked
80-
in
81-
- think detailed `processingErrors` which includes the summaries and the errors that were triggered during the process,
82-
the `raw` object that was used as in input for the ErrorObject.from() call and the `nextErrors` array which allows for
83-
all errors to be saved on one single error object for later use
41+
To parse errors from any payload, check
42+
out [@smbcheeky/error-object-from-payload](@smbcheeky/error-object-from-payload).
8443

8544
## Usage & Examples
8645

@@ -90,114 +49,10 @@ the [playground](https://github.com/SMBCheeky/error-object/blob/main/playground/
9049
```typescript
9150
new ErrorObject({ code: '', message: 'Something went wrong.', domain: 'auth' }).debugLog('LOG');
9251

93-
ErrorObject.from({ code: '', message: 'Something went wrong', domain: 'auth' })?.force?.debugLog('LOG');
94-
95-
// Example 12 output:
96-
//
97-
// [LOG] Something went wrong. [auth]
98-
// {
99-
// "code": "",
100-
// "message": "Something went wrong.",
101-
// "domain": "auth"
102-
// }
103-
//
10452
// [LOG] Something went wrong [auth]
10553
// {
10654
// "code": "",
10755
// "message": "Something went wrong",
10856
// "domain": "auth"
10957
// }
11058
```
111-
112-
```typescript
113-
const response = {
114-
statusCode: 400,
115-
headers: {
116-
'Content-Type': 'application/json',
117-
},
118-
body: '{"error":"Invalid input data","code":400}',
119-
};
120-
121-
ErrorObject.from(JSON.parse(response?.body), {
122-
pathToNumberCode: ['code'],
123-
pathToMessage: ['error'],
124-
}).force?.debugLog('LOG');
125-
126-
// Example 6 output:
127-
//
128-
// [LOG] Invalid input data [400]
129-
// {
130-
// "code": "400",
131-
// "numberCode": 400,
132-
// "message": "Invalid input data"
133-
// }
134-
```
135-
136-
```typescript
137-
/*
138-
* You could have a file called `errors.ts` in each of your modules/folders and
139-
* define a function like `createAuthError2()` that returns an error object with
140-
* the correct message and domain.
141-
*/
142-
const AuthMessageResolver = (
143-
beforeTransform: ErrorObjectTransformState): ErrorObjectTransformState => {
144-
// Quick tip: Make all messages slightly different, to make it easy
145-
// to find the right one when debugging, even in production
146-
let message: string | undefined;
147-
switch (beforeTransform.code) {
148-
case 'generic':
149-
message = 'Something went wrong';
150-
break;
151-
case 'generic-again':
152-
message = 'Something went wrong. Please try again.';
153-
break;
154-
case 'generic-network':
155-
message = 'Something went wrong. Please check your internet connection and try again.';
156-
break;
157-
default:
158-
message = 'Something went wrong.';
159-
}
160-
return { ...beforeTransform, message };
161-
};
162-
163-
const createAuthError2 = (code: string) => {
164-
return ErrorObject.from({ code, domain: 'auth', }, { transform: AuthMessageResolver, });
165-
};
166-
167-
168-
createAuthError2('generic')?.error?.log('1');
169-
createAuthError2('generic-again')?.error?.log('2');
170-
createAuthError2('generic-network')?.error?.log('3');
171-
createAuthError2('invalid-code')?.error?.log('4');
172-
173-
// Example 2 output:
174-
//
175-
// [1] Something went wrong [auth/generic]
176-
// [2] Something went wrong. Please try again. [auth/generic-again]
177-
// [3] Something went wrong. Please check your internet connection and try again. [auth/generic-network]
178-
// [4] Something went wrong. [auth/invalid-code]
179-
```
180-
181-
## FAQ
182-
183-
### How do I use paths? Are they absolute?
184-
185-
To support inputs containing arrays of errors as well as single errors, all paths are treated initially as absolute (
186-
from the
187-
input root), but if an array of errors is detected, it will consider each element found the new root input object. Devs
188-
have a
189-
choice: set the "pathToErrors" option as empty, and then map only the first error (highly not recommended), or adjust
190-
the paths to be relative to the objects inside the detected errors array.
191-
192-
### How do I use paths? I sometimes get the error code in an `error` object, and sometimes in the root object...
193-
194-
You can use `pathToCode: addPrefixPathVariants('error', ['code']),` or `pathToCode: ['error.code']`
195-
196-
### How do I use paths? Can I get the raw contents of a path and process it later?
197-
198-
Yes, you can. You can use paths like `error.details.0` to get a raw value, and then process it later using the
199-
`transform` option.
200-
If the value is not a string, it will be converted to a string using `JSON.stringify` to ensure everything works as
201-
intended.
202-
Remember, for an ErrorObject to be created, it needs at least a code and a message, and both are required to be string
203-
values.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@smbcheeky/error-object",
33
"description": "Create errors that can be both thrown and returned. Make error handling easier for both JavaScript and TypeScript.",
4-
"version": "1.1.4",
4+
"version": "1.1.5",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",
77
"types": "dist/index.d.ts",

0 commit comments

Comments
 (0)