Skip to content

Commit 5750170

Browse files
committed
add default error class name into 'title' field of JSON API errors
closes #102
1 parent da9f73e commit 5750170

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/JSONAPISerializer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ module.exports = class JSONAPISerializer {
412412
serializedError = {
413413
status: status && status.toString(),
414414
code: err.code,
415+
title: err.title || err.constructor.name,
415416
detail: err.message,
416417
};
417418
} else {

test/unit/JSONAPISerializer.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,14 +2276,41 @@ describe('JSONAPISerializer', function() {
22762276

22772277
it('should return serialized error with an instance of Error', function(done) {
22782278
const error = new Error('An error occured');
2279+
2280+
const serializedError = Serializer.serializeError(error);
2281+
2282+
expect(serializedError).to.have.property('errors').to.be.instanceof(Array).to.have.lengthOf(1);
2283+
expect(serializedError.errors[0]).to.have.property('title').to.eql('Error');
2284+
expect(serializedError.errors[0]).to.have.property('detail').to.eql('An error occured');
2285+
2286+
done();
2287+
});
2288+
2289+
it('should return serialized error with a custom Error class', function(done) {
2290+
class CustomError extends Error { };
2291+
const error = new CustomError('An error occured');
2292+
2293+
const serializedError = Serializer.serializeError(error);
2294+
2295+
expect(serializedError).to.have.property('errors').to.be.instanceof(Array).to.have.lengthOf(1);
2296+
expect(serializedError.errors[0]).to.have.property('title').to.eql('CustomError');
2297+
expect(serializedError.errors[0]).to.have.property('detail').to.eql('An error occured');
2298+
2299+
done();
2300+
});
2301+
2302+
it('should return serialized error with an instance of Error including additional properties status, code and title', function(done) {
2303+
const error = new Error('An error occured');
22792304
error.status = 500;
22802305
error.code = 'ERROR';
2306+
error.title = 'Error title';
22812307

22822308
const serializedError = Serializer.serializeError(error);
22832309

22842310
expect(serializedError).to.have.property('errors').to.be.instanceof(Array).to.have.lengthOf(1);
22852311
expect(serializedError.errors[0]).to.have.property('status').to.eql('500');
22862312
expect(serializedError.errors[0]).to.have.property('code').to.eql('ERROR');
2313+
expect(serializedError.errors[0]).to.have.property('title').to.eql('Error title');
22872314
expect(serializedError.errors[0]).to.have.property('detail').to.eql('An error occured');
22882315

22892316
done();
@@ -2295,7 +2322,10 @@ describe('JSONAPISerializer', function() {
22952322
const serializedErrors = Serializer.serializeError(errors);
22962323

22972324
expect(serializedErrors).to.have.property('errors').to.be.instanceof(Array).to.have.lengthOf(2);
2325+
2326+
expect(serializedErrors.errors[0]).to.have.property('title').to.eql('Error');
22982327
expect(serializedErrors.errors[0]).to.have.property('detail').to.eql('First Error');
2328+
expect(serializedErrors.errors[1]).to.have.property('title').to.eql('Error');
22992329
expect(serializedErrors.errors[1]).to.have.property('detail').to.eql('Second Error');
23002330

23012331
done();

0 commit comments

Comments
 (0)