refactor(core) make message property (http errors) serializable

This commit is contained in:
Kamil Myśliwiec
2019-09-27 11:15:58 +02:00
parent 08960e83d0
commit 73de9cb0a8
2 changed files with 32 additions and 26 deletions

View File

@@ -9,7 +9,6 @@ import { isObject, isString } from '../utils/shared.utils';
* @publicApi
*/
export class HttpException extends Error {
public readonly message: any;
/**
* Instantiate a plain HTTP Exception.
*
@@ -40,7 +39,14 @@ export class HttpException extends Error {
private readonly status: number,
) {
super();
this.message = response;
this.initMessage();
}
public initMessage() {
const defaultText = `HTTP Exception (response status code: ${this.status})`;
this.message = isString(this.response)
? `${defaultText} - ${this.response}`
: defaultText;
}
public getResponse(): string | object {
@@ -51,15 +57,6 @@ export class HttpException extends Error {
return this.status;
}
public toString(): string {
const message = this.getErrorString(this.message);
return `Error: ${message}`;
}
private getErrorString(target: string | object): string {
return isString(target) ? target : JSON.stringify(target);
}
public static createBody(
message: object | string,
error?: string,