-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore/drop_interceptors
- Loading branch information
Showing
5 changed files
with
366 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use strict' | ||
|
||
const { parseHeaders } = require('../core/util') | ||
const DecoratorHandler = require('../handler/decorator-handler') | ||
const { ResponseError } = require('../core/errors') | ||
|
||
class Handler extends DecoratorHandler { | ||
#handler | ||
#statusCode | ||
#contentType | ||
#decoder | ||
#headers | ||
#body | ||
|
||
constructor (opts, { handler }) { | ||
super(handler) | ||
this.#handler = handler | ||
} | ||
|
||
onConnect (abort) { | ||
this.#statusCode = 0 | ||
this.#contentType = null | ||
this.#decoder = null | ||
this.#headers = null | ||
this.#body = '' | ||
|
||
return this.#handler.onConnect(abort) | ||
} | ||
|
||
onHeaders (statusCode, rawHeaders, resume, statusMessage, headers = parseHeaders(rawHeaders)) { | ||
this.#statusCode = statusCode | ||
this.#headers = headers | ||
this.#contentType = headers['content-type'] | ||
|
||
if (this.#statusCode < 400) { | ||
return this.#handler.onHeaders(statusCode, rawHeaders, resume, statusMessage, headers) | ||
} | ||
|
||
if (this.#contentType === 'application/json' || this.#contentType === 'text/plain') { | ||
this.#decoder = new TextDecoder('utf-8') | ||
} | ||
} | ||
|
||
onData (chunk) { | ||
if (this.#statusCode < 400) { | ||
return this.#handler.onData(chunk) | ||
} | ||
|
||
this.#body += this.#decoder?.decode(chunk, { stream: true }) ?? '' | ||
} | ||
|
||
onComplete (rawTrailers) { | ||
if (this.#statusCode >= 400) { | ||
this.#body += this.#decoder?.decode(undefined, { stream: false }) ?? '' | ||
|
||
if (this.#contentType === 'application/json') { | ||
try { | ||
this.#body = JSON.parse(this.#body) | ||
} catch { | ||
// Do nothing... | ||
} | ||
} | ||
|
||
let err | ||
const stackTraceLimit = Error.stackTraceLimit | ||
Error.stackTraceLimit = 0 | ||
try { | ||
err = new ResponseError('Response Error', this.#statusCode, this.#headers, this.#body) | ||
} finally { | ||
Error.stackTraceLimit = stackTraceLimit | ||
} | ||
|
||
this.#handler.onError(err) | ||
} else { | ||
this.#handler.onComplete(rawTrailers) | ||
} | ||
} | ||
|
||
onError (err) { | ||
this.#handler.onError(err) | ||
} | ||
} | ||
|
||
module.exports = (dispatch) => (opts, handler) => opts.throwOnError | ||
? dispatch(opts, new Handler(opts, { handler })) | ||
: dispatch(opts, handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict' | ||
|
||
const assert = require('assert') | ||
const { test } = require('node:test') | ||
const createResponseErrorInterceptor = require('../../lib/interceptor/response-error') | ||
|
||
test('should not error if request is not meant to throw error', async (t) => { | ||
const opts = { throwOnError: false } | ||
const handler = { | ||
onError: () => {}, | ||
onData: () => {}, | ||
onComplete: () => {} | ||
} | ||
|
||
const interceptor = createResponseErrorInterceptor((opts, handler) => handler.onComplete()) | ||
|
||
assert.doesNotThrow(() => interceptor(opts, handler)) | ||
}) | ||
|
||
test('should error if request status code is in the specified error codes', async (t) => { | ||
const opts = { throwOnError: true, statusCodes: [500] } | ||
const response = { statusCode: 500 } | ||
let capturedError | ||
const handler = { | ||
onError: (err) => { | ||
capturedError = err | ||
}, | ||
onData: () => {}, | ||
onComplete: () => {} | ||
} | ||
|
||
const interceptor = createResponseErrorInterceptor((opts, handler) => { | ||
if (opts.throwOnError && opts.statusCodes.includes(response.statusCode)) { | ||
handler.onError(new Error('Response Error')) | ||
} else { | ||
handler.onComplete() | ||
} | ||
}) | ||
|
||
interceptor({ ...opts, response }, handler) | ||
|
||
await new Promise(resolve => setImmediate(resolve)) | ||
|
||
assert(capturedError, 'Expected error to be captured but it was not.') | ||
assert.strictEqual(capturedError.message, 'Response Error') | ||
assert.strictEqual(response.statusCode, 500) | ||
}) | ||
|
||
test('should not error if request status code is not in the specified error codes', async (t) => { | ||
const opts = { throwOnError: true, statusCodes: [500] } | ||
const response = { statusCode: 404 } | ||
const handler = { | ||
onError: () => {}, | ||
onData: () => {}, | ||
onComplete: () => {} | ||
} | ||
|
||
const interceptor = createResponseErrorInterceptor((opts, handler) => { | ||
if (opts.throwOnError && opts.statusCodes.includes(response.statusCode)) { | ||
handler.onError(new Error('Response Error')) | ||
} else { | ||
handler.onComplete() | ||
} | ||
}) | ||
|
||
assert.doesNotThrow(() => interceptor({ ...opts, response }, handler)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters