Skip to content
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

interceptors: move throwOnError to interceptor #3331

Merged
merged 28 commits into from
Jul 11, 2024

Conversation

mertcanaltin
Copy link
Member

  • Moved throwOnError handling to response-error interceptor.
  • Updated retry tests to handle 400 status code properly.

Test Issue:

  • Encountered 'BadRequestError' (status 400) in 'should not error if request is not meant to be retried' test.

Need assistance to resolve test failures.

Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove http-errors

Co-authored-by: Robert Nagy <ronagy@icloud.com>
Co-authored-by: Robert Nagy <ronagy@icloud.com>
lib/interceptor/retry.js Outdated Show resolved Hide resolved
Co-authored-by: Robert Nagy <ronagy@icloud.com>
@mertcanaltin mertcanaltin requested a review from ronag June 17, 2024 12:00
Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not forget to add docs and TS types 👍

lib/interceptor/response-error.js Outdated Show resolved Hide resolved
lib/interceptor/response-error.js Outdated Show resolved Hide resolved
lib/interceptor/response-error.js Outdated Show resolved Hide resolved
lib/interceptor/response-error.js Outdated Show resolved Hide resolved
lib/interceptor/retry.js Outdated Show resolved Hide resolved
test/interceptors/response-error.js Outdated Show resolved Hide resolved
Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also apply it to one of the API decoreators?

e.g.

diff --git a/lib/api/api-request.js b/lib/api/api-request.js
index ced5590d..f91b830a 100644
--- a/lib/api/api-request.js
+++ b/lib/api/api-request.js
@@ -13,7 +13,7 @@ class RequestHandler extends AsyncResource {
       throw new InvalidArgumentError('invalid opts')
     }
 
-    const { signal, method, opaque, body, onInfo, responseHeaders, throwOnError, highWaterMark } = opts
+    const { signal, method, opaque, body, onInfo, responseHeaders, highWaterMark } = opts
 
     try {
       if (typeof callback !== 'function') {
@@ -54,7 +54,6 @@ class RequestHandler extends AsyncResource {
     this.trailers = {}
     this.context = null
     this.onInfo = onInfo || null
-    this.throwOnError = throwOnError
     this.highWaterMark = highWaterMark
     this.signal = signal
     this.reason = null
@@ -132,20 +131,14 @@ class RequestHandler extends AsyncResource {
     this.callback = null
     this.res = res
     if (callback !== null) {
-      if (this.throwOnError && statusCode >= 400) {
-        this.runInAsyncScope(getResolveErrorBodyCallback, null,
-          { callback, body: res, contentType, statusCode, statusMessage, headers }
-        )
-      } else {
-        this.runInAsyncScope(callback, null, null, {
-          statusCode,
-          headers,
-          trailers: this.trailers,
-          opaque,
-          body: res,
-          context
-        })
-      }
+      this.runInAsyncScope(callback, null, null, {
+        statusCode,
+        headers,
+        trailers: this.trailers,
+        opaque,
+        body: res,
+        context
+      })
     }
   }
 
@@ -200,7 +193,7 @@ function request (opts, callback) {
   }
 
   try {
-    this.dispatch(opts, new RequestHandler(opts, callback))
+    this.compose(responseErrorInterceptor()).dispatch(opts, new RequestHandler(opts, callback))
   } catch (err) {
     if (typeof callback !== 'function') {
       throw err

Co-authored-by: Robert Nagy <ronagy@icloud.com>
lib/api/index.js Outdated Show resolved Hide resolved
mertcanaltin and others added 2 commits July 1, 2024 17:00
Co-authored-by: Robert Nagy <ronagy@icloud.com>
@mertcanaltin mertcanaltin requested a review from metcoder95 July 1, 2024 14:20
Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests seems to be failing

lib/api/index.js Outdated Show resolved Hide resolved
lib/interceptor/response-error.js Show resolved Hide resolved
lib/interceptor/response-error.js Show resolved Hide resolved
@mertcanaltin
Copy link
Member Author

Tests seems to be failing

Solved 🚀

@mertcanaltin mertcanaltin requested a review from metcoder95 July 8, 2024 18:08
Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add the respective TS types and docs 👍

@mertcanaltin
Copy link
Member Author

Let's add the respective TS types and docs 👍

Which md file should I put the doc in, do you have any suggestions?

@metcoder95
Copy link
Member

Which md file should I put the doc in, do you have any suggestions?

Within Dispatcher.md, there's a section there for interceptors that outlines the built-in ones 👍

Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It LGTM, just smaller comments on the docs 👍

docs/docs/api/Dispatcher.md Outdated Show resolved Hide resolved
docs/docs/api/Dispatcher.md Show resolved Hide resolved
@mertcanaltin mertcanaltin requested a review from metcoder95 July 11, 2024 08:03
lib/interceptor/response-error.js Outdated Show resolved Hide resolved
Co-authored-by: Khafra <maitken033380023@gmail.com>
@mertcanaltin mertcanaltin added the interceptors Pull requests or issues related to Dispatcher Interceptors label Jul 11, 2024
@ronag ronag merged commit 0ea1074 into nodejs:main Jul 11, 2024
30 checks passed
github-actions bot pushed a commit that referenced this pull request Sep 12, 2024
@github-actions github-actions bot mentioned this pull request Dec 3, 2024
@github-actions github-actions bot mentioned this pull request Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport v6.x interceptors Pull requests or issues related to Dispatcher Interceptors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants