-
-
Notifications
You must be signed in to change notification settings - Fork 664
interceptors: move throwOnError to interceptor #3331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4192b38
interceptors: move throwOnError to interceptor
e2321ec
delete http-errors
7855225
Update response-error.js
mertcanaltin ea068eb
Update response-error.js
mertcanaltin 6782734
Update lib/interceptor/response-error.js
mertcanaltin eb29a2a
feat: added new undiciError
75fcf70
feat: enable interceptor by default
b2110d4
feat: always throw error when interceptor is used
e7512d3
feat: add option to throw error on specific status codes
2195af1
feat: export retry interceptor in index.js
124e0db
Update response-error.js
mertcanaltin d731d9f
Update response-error.js
mertcanaltin b0700c3
Update response-error.js
mertcanaltin 821ea1d
Update response-error.js
mertcanaltin 463f9a5
Update response-error.js
mertcanaltin a8c3149
Update response-error.js
mertcanaltin f5adfb2
Update lib/api/index.js
mertcanaltin a1b282f
lint & added more test
43a1b07
Update response-error.js
mertcanaltin 0d6f450
Update response-error.js
mertcanaltin 8980063
Update response-error.js
mertcanaltin 636dd26
fix: test repair & unused values delete
72cea91
Merge branch 'main' of https://github.com/mertcanaltin/undici into ad…
ab619ec
fix: test problem solved
d5b2eb0
added types
e0dd09d
added Interceptor info in md
f10fc30
repair doc
ccd83e6
Update lib/interceptor/response-error.js
mertcanaltin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,110 @@ | ||
'use strict' | ||
|
||
const { parseHeaders } = require('../core/util') | ||
const createHttpError = require('http-errors') | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { DecoratorHandler } = require('undici') | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class Handler extends DecoratorHandler { | ||
#handler | ||
#statusCode | ||
#contentType | ||
#decoder | ||
#headers | ||
#body | ||
#errored | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
constructor (opts, { handler }) { | ||
super(handler) | ||
this.#handler = handler | ||
this.opts = opts | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
onConnect (abort) { | ||
this.#statusCode = 0 | ||
this.#contentType = null | ||
this.#decoder = null | ||
this.#headers = null | ||
this.#body = '' | ||
this.#errored = false | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return this.#handler.onConnect(abort) | ||
} | ||
|
||
onHeaders (statusCode, rawHeaders, resume, statusMessage, headers = parseHeaders(rawHeaders)) { | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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') | ||
} | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
onData (chunk) { | ||
if (this.#statusCode >= 400) { | ||
this.#body += this.#decoder?.decode(chunk, { stream: true }) ?? '' | ||
} else { | ||
return this.#handler.onData(chunk) | ||
} | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
onComplete (rawTrailers) { | ||
if (this.#statusCode >= 400) { | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.#body += this.#decoder?.decode(undefined, { stream: false }) ?? '' | ||
|
||
if (this.#contentType === 'application/json') { | ||
try { | ||
this.#body = JSON.parse(this.#body) | ||
} catch { | ||
// Do nothing... | ||
} | ||
} | ||
|
||
this.#errored = true | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let err | ||
|
||
const stackTraceLimit = Error.stackTraceLimit | ||
Error.stackTraceLimit = 0 | ||
try { | ||
err = Object.assign(createHttpError(this.#statusCode), { | ||
reason: this.#body?.reason, | ||
error: this.#body?.error, | ||
headers: this.#headers, | ||
body: this.#body | ||
}) | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} finally { | ||
Error.stackTraceLimit = stackTraceLimit | ||
} | ||
|
||
if (this.opts.throwOnError !== false && this.opts.error !== false) { | ||
this.#handler.onError(err) | ||
} else { | ||
this.#handler.onComplete(rawTrailers) | ||
} | ||
} else { | ||
this.#handler.onComplete(rawTrailers) | ||
} | ||
} | ||
|
||
onError (err) { | ||
if (this.#errored) { | ||
// Do nothing... | ||
} else { | ||
if (this.opts.throwOnError !== false && this.opts.error !== false) { | ||
this.#handler.onError(err) | ||
} else { | ||
this.#handler.onComplete() | ||
} | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
module.exports = (dispatch) => (opts, handler) => | ||
opts.error !== false && opts.throwOnError !== false | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? dispatch(opts, new Handler(opts, { handler })) | ||
: dispatch(opts, handler) | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 |
---|---|---|
@@ -1,19 +1,27 @@ | ||
'use strict' | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const RetryHandler = require('../handler/retry-handler') | ||
const createResponseErrorInterceptor = require('./response-error') | ||
|
||
module.exports = globalOpts => { | ||
return dispatch => { | ||
const responseErrorInterceptor = createResponseErrorInterceptor(dispatch) | ||
|
||
return function retryInterceptor (opts, handler) { | ||
return dispatch( | ||
opts, | ||
new RetryHandler( | ||
{ ...opts, retryOptions: { ...globalOpts, ...opts.retryOptions } }, | ||
{ | ||
handler, | ||
dispatch | ||
} | ||
) | ||
const wrappedHandler = { | ||
onConnect: handler.onConnect ? handler.onConnect.bind(handler) : undefined, | ||
onHeaders: handler.onHeaders.bind(handler), | ||
onData: handler.onData.bind(handler), | ||
onComplete: handler.onComplete.bind(handler), | ||
onError: handler.onError.bind(handler) | ||
} | ||
|
||
const finalHandler = new RetryHandler( | ||
{ ...opts, retryOptions: { ...globalOpts, ...opts.retryOptions } }, | ||
{ handler: wrappedHandler, dispatch } | ||
) | ||
|
||
return responseErrorInterceptor(opts, finalHandler) | ||
} | ||
} | ||
} |
This file contains hidden or 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,18 @@ | ||
'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 be retried', async (t) => { | ||
mertcanaltin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const response = { statusCode: 400 } | ||
const handler = { | ||
onError: () => {}, | ||
onData: () => {}, | ||
onComplete: () => {} | ||
} | ||
|
||
const interceptor = createResponseErrorInterceptor((opts, handler) => handler.onComplete()) | ||
|
||
assert.doesNotThrow(() => interceptor({ response, throwOnError: false }, handler)) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.