You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39Lines changed: 39 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -439,6 +439,45 @@ module.exports = function (MyModel) {
439
439
}
440
440
```
441
441
442
+
## Custom Errors
443
+
Generic errors respond with a 500, but sometime you want to have a better control over the error that is returned to the client, taking advantages of fields provided by JSONApi.
444
+
445
+
**It is recommanded to extend the base Error constructor, before throwing errors, ex: BadRequestError**
446
+
447
+
`meta` and `source` fields needs to be objects.
448
+
449
+
#### example
450
+
```js
451
+
module.exports=function (MyModel) {
452
+
MyModel.find=function () {
453
+
var err =newError('April 1st, 1998');
454
+
455
+
err.status=418;
456
+
err.name='I\'m a teapot';
457
+
err.source= { model:'Post', method:'find' };
458
+
err.detail='April 1st, 1998';
459
+
err.code='i\'m a teapot';
460
+
err.meta= { rfc:'RFC2324' };
461
+
462
+
throw err
463
+
}
464
+
}
465
+
466
+
// This will be returned as :
467
+
// {
468
+
// errors: [
469
+
// {
470
+
// status: 418,
471
+
// meta: { rfc: 'RFC2324' },
472
+
// code: 'i\'m a teapot',
473
+
// detail: 'April 1st, 1998',
474
+
// title: 'I\'m a teapot',
475
+
// source: { model: 'Post', method: 'find' }
476
+
// }
477
+
// ]
478
+
// }
479
+
```
480
+
442
481
##### function parameters
443
482
444
483
-`options` All config options set for the deserialization process.
0 commit comments