Skip to content

Commit 5310739

Browse files
Merge branch 'csidell-earny-master'
2 parents 9b5ddce + 41e4e85 commit 5310739

7 files changed

Lines changed: 79 additions & 5 deletions

File tree

packages/common/exceptions/http.exception.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isString } from '../utils/shared.utils';
2+
13
export class HttpException extends Error {
24
public readonly message: any;
35

@@ -30,4 +32,13 @@ export class HttpException extends Error {
3032
public getStatus(): number {
3133
return this.status;
3234
}
35+
36+
public toString(): string {
37+
const message = this.getErrorString(this.message);
38+
return `Error: ${message}`;
39+
}
40+
41+
private getErrorString(target: string | object): string {
42+
return isString(target) ? target : JSON.stringify(target);
43+
}
3344
}

packages/common/test/exceptions/http.exception.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,29 @@ describe('HttpException', () => {
4444
statusCode: 404,
4545
});
4646
});
47+
48+
it('should inherit from error', () => {
49+
const error = new HttpException('', 400);
50+
expect(error instanceof Error).to.be.true;
51+
});
52+
53+
it('should be serializable', () => {
54+
const message = 'Some Error';
55+
const error = new HttpException(message, 400);
56+
expect(`${error}`).to.be.eql(`Error: ${message}`);
57+
});
58+
59+
describe('when "message" is an object', () => {
60+
it('should serialize an object', () => {
61+
const obj = { foo: 'bar' };
62+
const error = new HttpException(obj, 400);
63+
expect(`${error}`).to.be.eql(`Error: ${JSON.stringify(obj)}`);
64+
expect(`${error}`.includes('[object Object]')).to.not.be.true;
65+
});
66+
67+
it('should serialize sub errors', () => {
68+
const error = new NotFoundException();
69+
expect(`${error}`.includes('Not Found')).to.be.true;
70+
});
71+
});
4772
});

packages/core/application-config.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ import {
88
import { InstanceWrapper } from './injector/instance-wrapper';
99

1010
export class ApplicationConfig {
11+
private globalPrefix = '';
1112
private globalPipes: PipeTransform[] = [];
12-
private globalRequestPipes: InstanceWrapper<PipeTransform>[] = [];
1313
private globalFilters: ExceptionFilter[] = [];
14-
private globalRequestFilters: InstanceWrapper<ExceptionFilter>[] = [];
1514
private globalInterceptors: NestInterceptor[] = [];
16-
private globalRequestInterceptors: InstanceWrapper<NestInterceptor>[] = [];
1715
private globalGuards: CanActivate[] = [];
18-
private globalRequestGuards: InstanceWrapper<CanActivate>[] = [];
19-
private globalPrefix = '';
16+
private readonly globalRequestPipes: InstanceWrapper<PipeTransform>[] = [];
17+
private readonly globalRequestFilters: InstanceWrapper<
18+
ExceptionFilter
19+
>[] = [];
20+
private readonly globalRequestInterceptors: InstanceWrapper<
21+
NestInterceptor
22+
>[] = [];
23+
private readonly globalRequestGuards: InstanceWrapper<CanActivate>[] = [];
2024

2125
constructor(private ioAdapter: WebSocketAdapter | null = null) {}
2226

packages/microservices/exceptions/rpc-exception.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isString } from '@nestjs/common/utils/shared.utils';
2+
13
export class RpcException extends Error {
24
public readonly message: any;
35
constructor(private readonly error: string | object) {
@@ -8,4 +10,13 @@ export class RpcException extends Error {
810
public getError(): string | object {
911
return this.error;
1012
}
13+
14+
public toString(): string {
15+
const message = this.getErrorString(this.message);
16+
return `Error: ${message}`;
17+
}
18+
19+
private getErrorString(target: string | object): string {
20+
return isString(target) ? target : JSON.stringify(target);
21+
}
1122
}

packages/microservices/test/exceptions/rpc-exception.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ describe('RpcException', () => {
1111
it('should returns error message or object', () => {
1212
expect(instance.getError()).to.be.eql(error);
1313
});
14+
15+
it('should serialize', () => {
16+
expect(`${instance}`.includes(error)).to.be.true;
17+
const obj = {foo: 'bar'};
18+
expect(`${new RpcException(obj)}`.includes(JSON.stringify(obj))).to.be.true;
19+
});
1420
});

packages/websockets/errors/ws-exception.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isString } from '@nestjs/common/utils/shared.utils';
2+
13
export class WsException extends Error {
24
public readonly message: any;
35

@@ -9,4 +11,13 @@ export class WsException extends Error {
911
public getError(): string | object {
1012
return this.error;
1113
}
14+
15+
public toString(): string {
16+
const message = this.getErrorString(this.message);
17+
return `Error: ${message}`;
18+
}
19+
20+
private getErrorString(target: string | object): string {
21+
return isString(target) ? target : JSON.stringify(target);
22+
}
1223
}

packages/websockets/test/exceptions/ws-exception.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ describe('WsException', () => {
1111
it('should returns error message or object', () => {
1212
expect(instance.getError()).to.be.eql(error);
1313
});
14+
15+
it('should serialize', () => {
16+
expect(`${instance}`.includes(error)).to.be.true;
17+
const obj = {foo: 'bar'};
18+
expect(`${new WsException(obj)}`.includes(JSON.stringify(obj))).to.be.true;
19+
});
1420
});

0 commit comments

Comments
 (0)