Merge branch 'master' of https://github.com/csidell-earny/nest into csidell-earny-master

This commit is contained in:
Kamil Myśliwiec
2019-07-09 15:05:39 +02:00
6 changed files with 79 additions and 0 deletions

View File

@@ -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}`;
}
}

View File

@@ -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;
});
});
});

View File

@@ -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}`;
}
}

View File

@@ -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;
});
});

View File

@@ -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}`;
}
}

View File

@@ -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;
});
});