Merge branch 'master' of github.com:keen-studio/payload

This commit is contained in:
James
2020-08-28 11:29:34 -04:00
2 changed files with 31 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
/* eslint-disable max-classes-per-file */
const httpStatus = require('http-status');
/**
@@ -27,7 +28,7 @@ class APIError extends ExtendableError {
* @param {boolean} isPublic - Whether the message should be visible to user or not.
*/
constructor(message, status = httpStatus.INTERNAL_SERVER_ERROR, isPublic = false) {
super(message, status, isPublic);
super(typeof message === 'string' ? message : JSON.stringify(message), status, isPublic);
}
}

29
src/errors/errors.spec.js Normal file
View File

@@ -0,0 +1,29 @@
const { APIError } = require('.');
describe('Errors', () => {
describe('APIError', () => {
it('should handle an error message', () => {
const error = new APIError('my message', 400, false);
expect(error.message).toStrictEqual('my message');
});
it('should handle an array', () => {
const errors = [
{
error: 'some error description',
},
{
error: 'some error description 2',
},
];
const error = new APIError(errors, 400, false);
expect(error.message).toStrictEqual(JSON.stringify(errors));
});
it('should handle an object', () => {
const myFancyErrorObject = { someProp: 'someDetail ' };
const error = new APIError(myFancyErrorObject, 400, false);
expect(error.message).toStrictEqual(JSON.stringify(myFancyErrorObject));
});
});
});