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

responseBodyEnd Callback Reports Zero Byte Count for Failed Requests (4xx/5xx), Even When Backend Returns Error Body #8598

Open
ddas09 opened this issue Nov 30, 2024 · 2 comments

Comments

@ddas09
Copy link

ddas09 commented Nov 30, 2024

Description:
When handling failed requests (such as HTTP 4xx or 5xx responses), the responseBodyEnd callback is invoked with a byteCount of 0, even if the server includes a response body containing error details. This happens regardless of whether the response body is empty or contains information like error messages or stack traces.

I have set up a custom retry interceptor that retries the request up to two times. The backend API returns an HTTP 500 error for the first two attempts and a successful HTTP 200 response for the final retry. However, I noticed that when the backend returns a 500 error, the responseBodyEnd callback always reports a byteCount of 0, even though the body contains error details. On successful (HTTP 200) responses, the byteCount is non-zero, as expected.

Is this expected behavior? As per my understanding the byteCount should reflect the size of the body, even for error responses.

Below is the sample code snippets -
Backend API (Express):

const internalError = catchAsync(async (req: Request, res: Response) => {
  const retryCount = parseInt(req.headers["x-retry-count"] as string) || 0;

  if (retryCount >= 3) {
    res.status(200).json({ message: "Success after 3 retries!" });
  } else {
    res.status(500).json({ message: "Something went wrong on the server." });
  }
});

Here is the custom retry interceptor I am using to retry the request up to two times:

class RetryInterceptor : Interceptor {
    private val maxRetryCount = 3
    private val retryDelayMillis = 500L

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        var attempt = 0
        var lastException: IOException? = null
        var response: Response? = null

        do {
            attempt++

            try {
                val modifiedRequest = chain.request().newBuilder()
                    .addHeader("X-Retry-Count", attempt.toString())
                    .build()

                response = chain.proceed(modifiedRequest)

                if (response.isSuccessful) {
                    return response
                }
            } catch (e: IOException) {
                lastException = e
            }

            // we need to close the response before retrying the request again
            response?.close()

            if (attempt < maxRetryCount) {
                try {
                    TimeUnit.MILLISECONDS.sleep(attempt * retryDelayMillis)
                } catch (e: InterruptedException) {
                    Thread.currentThread().interrupt()
                }
            }
        } while (attempt < maxRetryCount)

        throw lastException ?: IOException("Unknown error after $maxRetryCount retries")
    }
@swankjesse
Copy link
Collaborator

Looks like a bug!

@ddas09
Copy link
Author

ddas09 commented Nov 30, 2024

@swankjesse, is there any other way to determine the response body size using the Events listener API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants