Skip to content

Commit

Permalink
Make the responseError interceptor backward compatible, add a body pr…
Browse files Browse the repository at this point in the history
…operty (#3891)

Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina authored Nov 27, 2024
1 parent de6aea6 commit 3c67a3d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/core/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ class RequestRetryError extends UndiciError {
}

class ResponseError extends UndiciError {
constructor (message, code, { headers, data }) {
constructor (message, code, { headers, body }) {
super(message)
this.name = 'ResponseError'
this.message = message || 'Response error'
this.code = 'UND_ERR_RESPONSE'
this.statusCode = code
this.data = data
this.body = body
this.headers = headers
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/interceptor/response-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ResponseErrorHandler extends DecoratorHandler {
Error.stackTraceLimit = 0
try {
err = new ResponseError('Response Error', this.#statusCode, {
data: this.#body,
body: this.#body,
headers: this.#headers
})
} finally {
Expand Down
2 changes: 1 addition & 1 deletion test/interceptors/response-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('should throw error for error response', async () => {

assert.equal(error.statusCode, 400)
assert.equal(error.message, 'Response Error')
assert.equal(error.data, 'Bad Request')
assert.equal(error.body, 'Bad Request')
})

test('should not throw error for ok response', async () => {
Expand Down

0 comments on commit 3c67a3d

Please sign in to comment.