Skip to content

Commit ccb69d7

Browse files
committed
Add WsException serialization
1 parent 6098786 commit ccb69d7

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

packages/websockets/errors/ws-exception.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,18 @@ export class WsException extends Error {
99
public getError(): string | object {
1010
return this.error;
1111
}
12+
13+
private getErrorString(target) {
14+
if(typeof target === 'string') {
15+
return target;
16+
}
17+
18+
return JSON.stringify(target);
19+
}
20+
21+
public toString(): string {
22+
const message = this.getErrorString(this.message);
23+
24+
return `Error: ${message}`;
25+
}
1226
}

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)