Skip to content

Commit e888b65

Browse files
Merge pull request nestjs#1970 from rokerkony/bug/fix-bc-on-http-exception
bugfix(common): fix breaking change on http exception
2 parents 37fa9d5 + 0ef55c0 commit e888b65

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

packages/common/exceptions/http.exception.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { isObject } from '../utils/shared.utils';
2-
31
export class HttpException extends Error {
42
public readonly message: any;
53

@@ -22,9 +20,7 @@ export class HttpException extends Error {
2220
private readonly status: number,
2321
) {
2422
super();
25-
this.message =
26-
(isObject(response) && (response as { message?: string }).message) ||
27-
response;
23+
this.message = response;
2824
}
2925

3026
public getResponse(): string | object {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { expect } from 'chai';
2+
import { BadRequestException, HttpException, NotFoundException } from '../../exceptions';
3+
4+
describe('HttpException', () => {
5+
it('should return a message as a string when input is a string', () => {
6+
const message: string = 'My error message';
7+
expect(new HttpException(message, 404).message).to.be.eql('My error message');
8+
});
9+
10+
it('should return a message as an object when input is an object', () => {
11+
const message: object = {
12+
msg: 'My error message',
13+
reason: 'this can be a human readable reason',
14+
anything: 'else',
15+
};
16+
expect(new HttpException(message, 404).message).to.be.eql(message);
17+
});
18+
19+
it('should return a message from a built-in exception as an object', () => {
20+
const message: string = 'My error message';
21+
expect(new BadRequestException(message).message).to.be.eql({
22+
statusCode: 400,
23+
error: 'Bad Request',
24+
message: 'My error message',
25+
});
26+
});
27+
28+
it('should return an object even when the message is undefined', () => {
29+
expect(new BadRequestException().message).to.be.eql({statusCode: 400, error: 'Bad Request'});
30+
});
31+
32+
it('should return a status code', () => {
33+
expect(new BadRequestException().getStatus()).to.be.eql(400);
34+
expect(new NotFoundException().getStatus()).to.be.eql(404);
35+
});
36+
37+
it('should return a response', () => {
38+
expect(new BadRequestException().getResponse()).to.be.eql({
39+
error: 'Bad Request',
40+
statusCode: 400,
41+
});
42+
expect(new NotFoundException().getResponse()).to.be.eql({
43+
error: 'Not Found',
44+
statusCode: 404,
45+
});
46+
});
47+
});

0 commit comments

Comments
 (0)