@@ -85,9 +85,9 @@ Let's break it down:
8585 - for all fields other than ` numberCode ` , if a value is found and is a string, it is saved as is, but if it is
8686 an array or an object it will be JSON.stringify'ed and saved as a string
8787 - for ` numberCode ` , if a value is found and it is a number different than ` NaN ` , it is saved
88- - the ` transformCode ` , ` transformNumberCode ` , ` transformMessage ` , ` transformDetails ` and ` transformDomain `
89- functions are used to transform the found values to the error object
90- - the transform functions have access to each respective value, all other values, and the initial object (object
88+ - the ` transform `
89+ function is used to transform the found values by the parsing process into the error object
90+ - the transform function has access to all pre-transformation values and also the initial object (object
9191 inside the errors array or initial object)
9292 - everything gets processed into a list of ` ErrorSummary | ErrorObjectErrorResult ` array
9393 - it contains everything, from error strings custom-made to be as distinct and easy to read as possible, to self
@@ -177,20 +177,24 @@ ErrorObject.from(JSON.parse(response?.body), {
177177 * the correct message and domain.
178178 */
179179const AuthMessageResolver = (
180- message : string | undefined ,
181- beforeTransform : ErrorObjectBeforeTransformState ): string => {
180+ beforeTransform : ErrorObjectTransformState ): ErrorObjectTransformState => {
182181 // Quick tip: Make all messages slightly different, to make it easy
183182 // to find the right one when debugging, even in production
183+ let message: string | undefined ;
184184 switch (beforeTransform .code ) {
185185 case ' generic' :
186- return ' Something went wrong' ;
186+ message = ' Something went wrong' ;
187+ break ;
187188 case ' generic-again' :
188- return ' Something went wrong. Please try again.' ;
189+ message = ' Something went wrong. Please try again.' ;
190+ break ;
189191 case ' generic-network' :
190- return ' Something went wrong. Please check your internet connection and try again.' ;
192+ message = ' Something went wrong. Please check your internet connection and try again.' ;
193+ break ;
191194 default :
192- return ' Something went wrong.' ;
195+ message = ' Something went wrong.' ;
193196 }
197+ return { ... beforeTransform , message };
194198};
195199
196200const createAuthError2 = (code : string ) => {
@@ -200,7 +204,7 @@ const createAuthError2 = (code: string) => {
200204 domain: ' auth' ,
201205 },
202206 {
203- transformMessage : AuthMessageResolver ,
207+ transform : AuthMessageResolver ,
204208 },
205209 );
206210};
@@ -218,3 +222,27 @@ createAuthError2('invalid-code')?.error?.log('4');
218222// [3] Something went wrong. Please check your internet connection and try again. [auth/generic-network]
219223// [4] Something went wrong. [auth/invalid-code]
220224```
225+
226+ ## FAQ
227+
228+ ### How do I use paths? Are they absolute?
229+
230+ To support inputs containing arrays of errors as well as single errors, all paths are treated initially as absolute (
231+ from the
232+ input root), but if an array of errors is detected, it will consider each element found the new root input object. Devs
233+ have a
234+ choice: set the "pathToErrors" option as empty, and then map only the first error (highly not recommended), or adjust
235+ the paths to be relative to the objects inside the detected errors array.
236+
237+ ### How do I use paths? I sometimes get the error code in an ` error ` object, and sometimes in the root object...
238+
239+ You can use ` pathToCode: addPrefixPathVariants('error', ['code']), ` or ` pathToCode: ['error.code'] `
240+
241+ ### How do I use paths? Can I get the raw contents of a path and process it later?
242+
243+ Yes, you can. You can use paths like ` error.details.0 ` to get a raw value, and then process it later using the
244+ ` transform ` option.
245+ If the value is not a string, it will be converted to a string using ` JSON.stringify ` to ensure everything works as
246+ intended.
247+ Remember, for an ErrorObject to be created, it needs at least a code and a message, and both are required to be string
248+ values.
0 commit comments