diff --git a/RELEASES.md b/RELEASES.md index ebee9fc..245477d 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.8 Fixed security issue where a redirect (e.g. 302) to another domain would pass headers. The fix was to strip the authorization header if the hostname was different. More [details in PR #27](https://github.com/actions/http-client/pull/27) diff --git a/index.ts b/index.ts index 0756a55..97a3fa4 100644 --- a/index.ts +++ b/index.ts @@ -69,6 +69,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 @@ -742,13 +754,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 e57d099..82e5a95 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@actions/http-client", - "version": "1.0.8", + "version": "1.0.9", "description": "Actions Http Client", "main": "index.js", "scripts": {