1515[ plugin] ( https://github.com/ehmicky/modern-errors#-plugins ) to serialize/parse
1616errors.
1717
18- This adds [ ` error .toJSON()` ] ( #errortojson ) and
18+ This adds [ ` BaseError .toJSON()` ] ( #baseerrortojsonerror ) and
1919[ ` BaseError.parse() ` ] ( #baseerrorparseerrorobject ) to serialize/parse errors
2020to/from plain objects.
2121
@@ -46,11 +46,11 @@ export const BaseError = ModernError.subclass('BaseError', {
4646// ...
4747```
4848
49- [ Serializing] ( #errortojson ) errors to plain objects.
49+ [ Serializing] ( #baseerrortojsonerror ) errors to plain objects.
5050
5151``` js
5252const error = new InputError (' Wrong file.' , { props: { filePath } })
53- const errorObject = error .toJSON ()
53+ const errorObject = BaseError .toJSON (error )
5454// { name: 'InputError', message: 'Wrong file', stack: '...', filePath: '...' }
5555const errorString = JSON .stringify (error)
5656// '{"name":"InputError",...}'
@@ -88,16 +88,21 @@ Plugin object to pass to the
8888[ ` plugins ` option] ( https://github.com/ehmicky/modern-errors#adding-plugins ) of
8989` ErrorClass.subclass() ` .
9090
91- ## error .toJSON()
91+ ## BaseError .toJSON(error )
9292
93+ ` error ` : ` Error ` \
9394_ Return value_ : ` ErrorObject `
9495
9596Converts errors to plain objects that are
9697[ serializable] ( https://github.com/ehmicky/error-serializer#json-safety ) to JSON
9798([ or YAML] ( https://github.com/ehmicky/error-serializer#custom-serializationparsing ) ,
98- etc.). This is
99- [ automatically called] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior )
100- by ` JSON.stringify() ` .
99+ etc.).
100+
101+ This is also set as
102+ [ ` error.toJSON() ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior ) ,
103+ so
104+ [ ` JSON.stringify() ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify )
105+ automatically calls it.
101106
102107All
103108[ error properties] ( https://github.com/ehmicky/error-serializer#additional-error-properties )
@@ -125,7 +130,7 @@ const error = new InputError('Wrong file.')
125130error .cycle = error
126131
127132// Cycles make `JSON.stringify()` throw, so they are removed
128- console .log (error .toJSON ().cycle ) // undefined
133+ console .log (BaseError .toJSON (error ).cycle ) // undefined
129134```
130135
131136## Deep serialization/parsing
@@ -153,7 +158,7 @@ serialization/parsing logic to be performed.
153158import { dump , load } from ' js-yaml'
154159
155160const error = new InputError (' Wrong file.' )
156- const errorObject = error .toJSON ()
161+ const errorObject = BaseError .toJSON (error )
157162const errorYamlString = dump (errorObject)
158163// name: InputError
159164// message: Wrong file.
@@ -166,7 +171,7 @@ const newError = BaseError.parse(newErrorObject) // InputError: Wrong file.
166171
167172``` js
168173const error = new InputError (' Wrong file.' , { props: { prop: true } })
169- const errorObject = error .toJSON ()
174+ const errorObject = BaseError .toJSON (error )
170175console .log (errorObject .prop ) // true
171176const newError = BaseError .parse (errorObject)
172177console .log (newError .prop ) // true
@@ -179,7 +184,7 @@ const error = new InputError('Wrong file.', {
179184 errors: [new ExampleError (' one' ), new ExampleError (' two' )],
180185})
181186
182- const errorObject = error .toJSON ()
187+ const errorObject = BaseError .toJSON (error )
183188// {
184189// name: 'InputError',
185190// message: 'Wrong file.',
@@ -212,7 +217,7 @@ const InputError = BaseError.subclass('InputError', {
212217})
213218
214219const error = new InputError (' Wrong file.' , {}, true )
215- const errorObject = error .toJSON ()
220+ const errorObject = BaseError .toJSON (error )
216221// `constructor(message, options, prop)` is not called
217222const newError = BaseError .parse (errorObject)
218223// But properties set by that `constructor(...)` are kept
0 commit comments