mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
Merge branch 'master' of https://github.com/csidell-earny/nest into csidell-earny-master
This commit is contained in:
@@ -30,4 +30,18 @@ export class HttpException extends Error {
|
||||
public getStatus(): number {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
private getErrorString(target: string | object): string{
|
||||
if (typeof target === 'string') {
|
||||
return target;
|
||||
}
|
||||
|
||||
return JSON.stringify(target);
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
const message = this.getErrorString(this.message);
|
||||
|
||||
return `Error: ${message}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,4 +44,29 @@ describe('HttpException', () => {
|
||||
statusCode: 404,
|
||||
});
|
||||
});
|
||||
|
||||
it('should inherit from error', () => {
|
||||
const error = new HttpException('', 400);
|
||||
expect(error instanceof Error).to.be.true;
|
||||
});
|
||||
|
||||
it('should be serializable', () => {
|
||||
const message = 'Some Error';
|
||||
const error = new HttpException(message, 400);
|
||||
expect(`${error}`).to.be.eql(`Error: ${message}`);
|
||||
});
|
||||
|
||||
describe('when "message" is an object', () => {
|
||||
it('should serialize an object', () => {
|
||||
const obj = { foo: 'bar' };
|
||||
const error = new HttpException(obj, 400);
|
||||
expect(`${error}`).to.be.eql(`Error: ${JSON.stringify(obj)}`);
|
||||
expect(`${error}`.includes('[object Object]')).to.not.be.true;
|
||||
});
|
||||
|
||||
it('should serialize sub errors', () => {
|
||||
const error = new NotFoundException();
|
||||
expect(`${error}`.includes('Not Found')).to.be.true;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,4 +8,18 @@ export class RpcException extends Error {
|
||||
public getError(): string | object {
|
||||
return this.error;
|
||||
}
|
||||
|
||||
private getErrorString(target: string | object): string {
|
||||
if (typeof target === 'string') {
|
||||
return target;
|
||||
}
|
||||
|
||||
return JSON.stringify(target);
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
const message = this.getErrorString(this.message);
|
||||
|
||||
return `Error: ${message}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,4 +11,10 @@ describe('RpcException', () => {
|
||||
it('should returns error message or object', () => {
|
||||
expect(instance.getError()).to.be.eql(error);
|
||||
});
|
||||
|
||||
it('should serialize', () => {
|
||||
expect(`${instance}`.includes(error)).to.be.true;
|
||||
const obj = {foo: 'bar'};
|
||||
expect(`${new RpcException(obj)}`.includes(JSON.stringify(obj))).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,4 +9,18 @@ export class WsException extends Error {
|
||||
public getError(): string | object {
|
||||
return this.error;
|
||||
}
|
||||
|
||||
private getErrorString(target: string | object): string {
|
||||
if (typeof target === 'string') {
|
||||
return target;
|
||||
}
|
||||
|
||||
return JSON.stringify(target);
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
const message = this.getErrorString(this.message);
|
||||
|
||||
return `Error: ${message}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,4 +11,10 @@ describe('WsException', () => {
|
||||
it('should returns error message or object', () => {
|
||||
expect(instance.getError()).to.be.eql(error);
|
||||
});
|
||||
|
||||
it('should serialize', () => {
|
||||
expect(`${instance}`.includes(error)).to.be.true;
|
||||
const obj = {foo: 'bar'};
|
||||
expect(`${new WsException(obj)}`.includes(JSON.stringify(obj))).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user