Skip to content

Commit 251a8a2

Browse files
Merge branch 'sincraianul-fix-class-validator-response'
2 parents cec89d2 + 46d930f commit 251a8a2

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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({
7+
message: 'message',
8+
error: 'error',
9+
statusCode: 200,
10+
});
11+
});
12+
13+
it('should override pre-defined body if message is object', () => {
14+
expect(createHttpExceptionBody({ test: 'object' }, 'error', 200)).to.eql({
15+
test: 'object',
16+
});
17+
});
18+
19+
it('should not override pre-defined body if message is array', () => {
20+
expect(
21+
createHttpExceptionBody(['a', 'random', 'array'], 'error', 200),
22+
).to.eql({
23+
message: ['a', 'random', 'array'],
24+
error: 'error',
25+
statusCode: 200,
26+
});
27+
});
28+
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ 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)
12+
? message
13+
: { statusCode, error, message };
1214
};

0 commit comments

Comments
 (0)