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

UnhandledPromiseRejection when returing Promise.reject on interceptor #87

Open
ashalfarhan opened this issue Aug 31, 2022 · 6 comments
Open
Labels

Comments

@ashalfarhan
Copy link

ashalfarhan commented Aug 31, 2022

I'm trying to test my interceptor logic, whether calling the error reporter (like Sentry), showing toast error, and resetting the auth state if we received a 401 response from our server. The problem that we're facing is that we got the following error every time we ran the test:

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "AxiosError".] 
{
  code: 'ERR_UNHANDLED_REJECTION'
}

Based on Interceptors Docs we need to return a rejected promise on the second parameter (onRejected) for the interceptor, but when I remove the Promise.reject calls (just returning the error), the test successfully ran.

But that turns out to break my app.

Additional info that might help

Any idea how I test my interceptor logic without breaking my app? Thanks in advance!

@kingjan1999 kingjan1999 added the bug label Sep 1, 2022
@kingjan1999
Copy link
Collaborator

Hi,

thanks for raising this issue. You're right, response interceptors returning a promise are not correctly handled at the moment. I'll fix this.

@kingjan1999
Copy link
Collaborator

Hi,

I published 4.7.0-beta on npm. This should fix the UnhandledPromiseRejection at least and make the test work. Can you install this version and try if it works?

This still does not implement correct interceptor behavior, as the result of the onRejected handler is discarded in any case. However, for the moment this might be enough.

@ashalfarhan
Copy link
Author

Hi @kingjan1999. I've installed the 4.7.0-beta and it works. Thanks!

@rosstroha
Copy link

rosstroha commented Sep 6, 2022

Hello, I am also trying to test response interceptors. My test is expecting the function-under-test to throw an error, which is what I believe Promise.reject normally does, and it is failing because no error is being thrown. This is happening with 4.7.0-beta

If I'm testing it incorrectly, please let me know, but here is what I'm doing:

    it('will throw unauthorized if the status is 401', async () => {
      await expect(() => {
        const promise = service.getTheThing()
        mockAxios.mockError({ response: { status: 401, data: {} }})
        return promise
      }).rejects.toThrow(UnauthorizedError)
    })

The interceptors:

    axios.interceptors.response.use((response) => {
      return response
    }, (error: AxiosError) => {
      if (error?.response?.status) {
        // This function just returns the appropriate error object to be thrown by Promise.reject()
        return Promise.reject(this.checkExceptionForStatus(error))
      }

      return Promise.reject(error)
    })
    

@kingjan1999
Copy link
Collaborator

@rosstroha I think you stumbled about the behavior I described above with "interceptors are not corrected properly".

The current implementation in 4.7.0-beta just avoids the error being unhandled completely, but it does not handle it properly whatsoever. I'll fix this before releasing 4.7.0 , once I've found out why this isn't working at the moment.

@rosstroha
Copy link

@rosstroha I think you stumbled about the behavior I described above with "interceptors are not corrected properly".

The current implementation in 4.7.0-beta just avoids the error being unhandled completely, but it does not handle it properly whatsoever. I'll fix this before releasing 4.7.0 , once I've found out why this isn't working at the moment.

Fair enough :) Thank you @kingjan1999!

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

No branches or pull requests

3 participants