diff --git a/lib/exceptions.ts b/lib/exceptions.ts index 27385db9e..6466e3425 100644 --- a/lib/exceptions.ts +++ b/lib/exceptions.ts @@ -16,9 +16,11 @@ interface FetchErrorDetails extends Status { } // Deprecated -interface AxiosErrorDetails extends Partial { +interface AxiosErrorDetails { originalError: Error; code?: string; + statusCode?: number; + statusMessage?: string; } export class SignatureValidationFailed extends Error { @@ -43,26 +45,6 @@ export class JSONParseError extends Error { } } -export class HTTPFetchError extends Error { - public status: number; - - public statusText: string; - - public headers: Headers; - - public body: string; - - constructor( - message: Message, - { status, statusText, headers, body }: FetchErrorDetails, - ) { - super(message); - this.name = this.constructor.name; - - Object.assign(this, { status, statusText, headers, body }); - } -} - /* Deprecated */ export class RequestError extends Error { public code: string; @@ -89,19 +71,39 @@ export class ReadError extends Error { } export class HTTPError extends Error { + public statusCode: number; + + public statusMessage: string; + + public originalError: any; + + constructor( + message: Message, + { statusCode, statusMessage, originalError }: AxiosErrorDetails, + ) { + super(message); + this.name = this.constructor.name; + + Object.assign(this, { statusCode, statusMessage, originalError }); + } +} + +export class HTTPFetchError extends Error { public status: number; public statusText: string; - public originalError: any; + public headers: Headers; + + public body: string; constructor( message: Message, - { status, statusText, originalError }: AxiosErrorDetails, + { status, statusText, headers, body }: FetchErrorDetails, ) { super(message); this.name = this.constructor.name; - Object.assign(this, { status, statusText, originalError }); + Object.assign(this, { status, statusText, headers, body }); } } diff --git a/lib/http-axios.ts b/lib/http-axios.ts index 3b6d4d741..0160b4822 100644 --- a/lib/http-axios.ts +++ b/lib/http-axios.ts @@ -167,7 +167,11 @@ export default class HTTPClient { const { status, statusText } = err.response; const { message } = err; - return new HTTPError(message, { status, statusText, originalError: err }); + return new HTTPError(message, { + statusCode: status, + statusMessage: statusText, + originalError: err, + }); } else if (err.code) { const { message, code } = err; return new RequestError(message, { code, originalError: err }); diff --git a/test/http-axios.spec.ts b/test/http-axios.spec.ts index b0c1bab29..73767ff1a 100644 --- a/test/http-axios.spec.ts +++ b/test/http-axios.spec.ts @@ -276,7 +276,7 @@ describe("http", () => { } catch (err) { ok(err instanceof HTTPError); equal(scope.isDone(), true); - equal(err.status, 404); + equal(err.statusCode, 404); } }); diff --git a/test/middleware.spec.ts b/test/middleware.spec.ts index 8e83ae1ef..ae5b981dd 100644 --- a/test/middleware.spec.ts +++ b/test/middleware.spec.ts @@ -100,7 +100,7 @@ describe("middleware", () => { ok(false); } catch (err) { if (err instanceof HTTPError) { - equal(err.status, 415); + equal(err.statusCode, 415); } else { throw err; } @@ -116,7 +116,7 @@ describe("middleware", () => { ok(false); } catch (err) { if (err instanceof HTTPError) { - equal(err.status, 500); + equal(err.statusCode, 500); } else { throw err; } @@ -142,7 +142,7 @@ describe("middleware", () => { ok(false); } catch (err) { if (err instanceof HTTPError) { - equal(err.status, 401); + equal(err.statusCode, 401); } else { throw err; } @@ -160,7 +160,7 @@ describe("middleware", () => { ok(false); } catch (err) { if (err instanceof HTTPError) { - equal(err.status, 401); + equal(err.statusCode, 401); } else { throw err; } @@ -177,7 +177,7 @@ describe("middleware", () => { ok(false); } catch (err) { if (err instanceof HTTPError) { - equal(err.status, 400); + equal(err.statusCode, 400); } else { throw err; } @@ -193,7 +193,7 @@ describe("middleware", () => { ok(false); } catch (err) { if (err instanceof HTTPError) { - equal(err.status, 401); + equal(err.statusCode, 401); } else { throw err; }