diff --git a/package-lock.json b/package-lock.json index a22af9396..211454499 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1103,6 +1103,15 @@ } } }, + "@octokit/request-error": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-1.0.1.tgz", + "integrity": "sha512-i7r3/Pv5p1ZANU+C6NmaO/fghk/qwwVe5k6+seOrI2upPEYjrlyx1u7mlJy5Ip6eZBCHMdiuldgSzs39zyh2QA==", + "requires": { + "deprecation": "^2.0.0", + "once": "^1.4.0" + } + }, "@octokit/rest": { "version": "16.25.5", "resolved": "https://registry.npmjs.org/@octokit/rest/-/rest-16.25.5.tgz", diff --git a/package.json b/package.json index e35f5cd2d..9454ebcaa 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "homepage": "https://github.com/octokit/request.js#readme", "dependencies": { "@octokit/endpoint": "^5.1.0", + "@octokit/request-error": "^1.0.1", "deprecation": "^2.0.0", "is-plain-object": "^3.0.0", "node-fetch": "^2.3.0", diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index 654e0cfd2..58502407a 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -1,8 +1,8 @@ import isPlainObject from "is-plain-object"; import nodeFetch from "node-fetch"; +import { RequestError } from "@octokit/request-error"; import getBuffer from "./get-buffer-response"; -import HttpError from "./http-error"; import { endpoint } from "./types"; export default function fetchWrapper( @@ -52,16 +52,17 @@ export default function fetchWrapper( return; } - throw new HttpError( - response.statusText, - status, + throw new RequestError(response.statusText, status, { headers, - requestOptions - ); + request: requestOptions + }); } if (status === 304) { - throw new HttpError("Not modified", status, headers, requestOptions); + throw new RequestError("Not modified", status, { + headers, + request: requestOptions + }); } if (status >= 400) { @@ -69,12 +70,10 @@ export default function fetchWrapper( .text() .then(message => { - const error = new HttpError( - message, - status, + const error = new RequestError(message, status, { headers, - requestOptions - ); + request: requestOptions + }); try { Object.assign(error, JSON.parse(error.message)); @@ -108,10 +107,13 @@ export default function fetchWrapper( }) .catch(error => { - if (error instanceof HttpError) { + if (error instanceof RequestError) { throw error; } - throw new HttpError(error.message, 500, headers, requestOptions); + throw new RequestError(error.message, 500, { + headers, + request: requestOptions + }); }); } diff --git a/src/http-error.ts b/src/http-error.ts deleted file mode 100644 index 84155acbc..000000000 --- a/src/http-error.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { Deprecation } from "deprecation"; -import once from "once"; -const logOnce = once((deprecation: any) => console.warn(deprecation)); - -import { endpoint, ResponseHeaders } from "./types"; - -export default class HttpError extends Error { - name: string; - status: number; - headers: ResponseHeaders; - request: ReturnType; - code!: number; - constructor( - message: string, - statusCode: number, - headers: ResponseHeaders, - request: ReturnType - ) { - super(message); - - // Maintains proper stack trace (only available on V8) - /* istanbul ignore next */ - if (Error.captureStackTrace) { - Error.captureStackTrace(this, this.constructor); - } - - this.name = "HttpError"; - this.status = statusCode; - Object.defineProperty(this, "code", { - get() { - logOnce( - new Deprecation( - "[@octokit/request] `error.code` is deprecated, use `error.status`." - ) - ); - return statusCode; - } - }); - this.headers = headers; - - // redact request credentials without mutating original request options - const requestCopy = Object.assign({}, request); - if (request.headers.authorization) { - requestCopy.headers = Object.assign({}, request.headers, { - authorization: request.headers.authorization.replace( - / .*$/, - " [REDACTED]" - ) - }); - } - - // client_id & client_secret can be passed as URL query parameters to increase rate limit - // see https://developer.github.com/v3/#increasing-the-unauthenticated-rate-limit-for-oauth-applications - requestCopy.url = requestCopy.url.replace( - /\bclient_secret=\w+/g, - "client_secret=[REDACTED]" - ); - - this.request = requestCopy; - } -} diff --git a/test/request.test.ts b/test/request.test.ts index 927c20d0f..7d13191cb 100644 --- a/test/request.test.ts +++ b/test/request.test.ts @@ -425,30 +425,6 @@ describe("request()", () => { ); }); }); - - it("error.code (deprecated)", () => { - const mock = fetchMock.sandbox().get("path:/orgs/nope", 404); - - const consoleWarn = console.warn; - let warnCalled = 0; - console.warn = () => warnCalled++; - return request("GET /orgs/:org", { - org: "nope", - request: { - fetch: mock - } - }) - .then(() => { - throw new Error("should not resolve"); - }) - - .catch(error => { - expect(error.code).toEqual(404); - expect(warnCalled).toEqual(1); - console.warn = consoleWarn; - }); - }); - it("Just URL", () => { const mock = fetchMock.sandbox().get("path:/", 200);