From 55850f15296bb43bb809f1ad3f441ee1f0f526f3 Mon Sep 17 00:00:00 2001 From: Dave Hadka Date: Mon, 17 Aug 2020 21:16:30 -0500 Subject: [PATCH] Return HttpClientError instead of Error, have properties for status and result --- RELEASES.md | 3 +++ index.ts | 21 ++++++++++++++------- package-lock.json | 2 +- package.json | 2 +- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 79f9a58..ca7a38e 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,5 +1,8 @@ ## Releases +## 1.0.9 +Throw HttpClientError instead of a generic Error from the \Json() helper methods when the server responds with a non-successful status code. + ## 1.0.7 Update NPM dependencies and add 429 to the list of HttpCodes diff --git a/index.ts b/index.ts index a1f5491..75be6be 100644 --- a/index.ts +++ b/index.ts @@ -70,6 +70,18 @@ const RetryableHttpVerbs: string[] = ['OPTIONS', 'GET', 'DELETE', 'HEAD'] const ExponentialBackoffCeiling = 10 const ExponentialBackoffTimeSlice = 5 +export class HttpClientError extends Error { + constructor(message: string, statusCode: number) { + super(message) + this.name = 'HttpClientError' + this.statusCode = statusCode + Object.setPrototypeOf(this, HttpClientError.prototype) + } + + public statusCode: number + public result?: any +} + export class HttpClientResponse implements ifm.IHttpClientResponse { constructor(message: http.IncomingMessage) { this.message = message @@ -733,13 +745,8 @@ export class HttpClient { msg = 'Failed request: (' + statusCode + ')' } - let err: Error = new Error(msg) - - // attach statusCode and body obj (if available) to the error object - err['statusCode'] = statusCode - if (response.result) { - err['result'] = response.result - } + let err = new HttpClientError(msg, statusCode) + err.result = response.result reject(err) } else { diff --git a/package-lock.json b/package-lock.json index 7a48ba6..6555357 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@actions/http-client", - "version": "1.0.7", + "version": "1.0.9", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 49f2e08..82e5a95 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@actions/http-client", - "version": "1.0.7", + "version": "1.0.9", "description": "Actions Http Client", "main": "index.js", "scripts": {