Skip to content

Commit d6b8e1f

Browse files
author
Andrei Sîncrăian
committed
bugfix(common): prevent exception body override if message is an array
1 parent d8c7ba3 commit d6b8e1f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect } from 'chai';
2+
import { createHttpExceptionBody } from '../../utils/http-exception-body.util';
3+
4+
describe('createHttpExceptionBody', () => {
5+
it('should return pre-defined body if message is string', () => {
6+
expect(createHttpExceptionBody('message', 'error', 200)).to.eql({ message: 'message', error: 'error', statusCode: 200 });
7+
});
8+
9+
it('should override pre-defined body if message is object', () => {
10+
expect(createHttpExceptionBody({ test: 'object' }, 'error', 200)).to.eql({ test: 'object' });
11+
});
12+
13+
it('should not override pre-defined body if message is array', () => {
14+
expect(createHttpExceptionBody(['a', 'random', 'array'], 'error', 200)).to.eql({ message: ['a', 'random', 'array'], error: 'error', statusCode: 200 });
15+
});
16+
});

packages/common/utils/http-exception-body.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export const createHttpExceptionBody = (
88
if (!message) {
99
return { statusCode, error };
1010
}
11-
return isObject(message) ? message : { statusCode, error, message };
11+
return isObject(message) && !Array.isArray(message) ? message : { statusCode, error, message };
1212
};

0 commit comments

Comments
 (0)