From 918228c9dce7c1e9df759fed0cda70b620470a25 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Wed, 26 Aug 2020 23:14:28 -0400 Subject: [PATCH] Handle values other than string in APIError --- src/errors/APIError.js | 3 ++- src/errors/errors.spec.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/errors/errors.spec.js diff --git a/src/errors/APIError.js b/src/errors/APIError.js index a743174186..d4c872bc8c 100644 --- a/src/errors/APIError.js +++ b/src/errors/APIError.js @@ -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); } } diff --git a/src/errors/errors.spec.js b/src/errors/errors.spec.js new file mode 100644 index 0000000000..0ba924f515 --- /dev/null +++ b/src/errors/errors.spec.js @@ -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)); + }); + }); +});