diff --git a/packages/fetcher/package.json b/packages/fetcher/package.json index 182a526a2..9c422e726 100644 --- a/packages/fetcher/package.json +++ b/packages/fetcher/package.json @@ -1,6 +1,6 @@ { "name": "@nestia/fetcher", - "version": "1.0.1", + "version": "1.1.0-dev.20230413", "description": "Fetcher library of Nestia SDK", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/packages/fetcher/src/HttpError.ts b/packages/fetcher/src/HttpError.ts index 0c205dd85..6a3309793 100644 --- a/packages/fetcher/src/HttpError.ts +++ b/packages/fetcher/src/HttpError.ts @@ -6,6 +6,11 @@ * @author Jeongho Nam - https://github.com/samchon */ export class HttpError extends Error { + /** + * @internal + */ + private body_: any = NOT_YET; + /** * Initializer Constructor. * @@ -27,4 +32,40 @@ export class HttpError extends Error { if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto); else (this as any).__proto__ = proto; } + + /** + * `HttpError` to JSON. + * + * When you call `JSON.stringify()` function on current `HttpError` instance, + * this `HttpError.toJSON()` method would be automatically called. + * + * Also, if response body from the remote HTTP server forms a JSON object, + * this `HttpError.toJSON()` method would be useful because it returns the + * parsed JSON object about the {@link message} property. + * + * @template T Expected type of the response body. + * @returns JSON object of the `HttpError`. + */ + public toJSON(): HttpError.IProps { + if (this.body_ === NOT_YET) this.body_ = JSON.parse(this.message); + return { + method: this.method, + path: this.path, + status: this.status, + message: this.body_, + }; + } } +export namespace HttpError { + /** + * Returned type of {@link HttpError.toJSON} method. + */ + export interface IProps { + method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH"; + path: string; + status: number; + message: T; + } +} + +const NOT_YET = {} as any;