Skip to content

Fix onResponseError chain call #92

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

Closed
wants to merge 1 commit into from
Closed

Conversation

Vespand
Copy link
Contributor

@Vespand Vespand commented Dec 29, 2023

Method onResponseError doesn't work (also all $http.interceptors.response with errors handlers)

This PR fix problem

How to reproduce:

  • Replace code in playground/pages/index.vue with above ($http.get should return 404 error code)
<template>
    <div>
        <pre>{{ auth.user }}</pre>
        <div>onResponseErrorWork: {{ onResponseErrorWork }}</div>
    </div>
</template>

<script lang="ts" setup>
const { $auth, $http } = useNuxtApp()
const onResponseErrorWork = ref(false);

$http.onResponseError(error => {
    onResponseErrorWork.value = true;
    console.log('onResponseError: ', error)
});

$http.get('http://localhost:3000/unexist-page-123')
const auth = useAuth()
console.log(auth)
console.log($auth)
</script>

With fix onResponseErrorWork will be true and console.log will call
Without fix onResponseErrorWork will be false, because onResponseError never called in chain after request handler interceptor
Without fix all calls with $http.get and others methods newer throw error (catch never «catch»)

@Denoder
Copy link
Member

Denoder commented Dec 30, 2023

Not sure I should go with with this PR, the true boolean I had set was only meant to work for a specific error to reset. (That error being the response status being 401) Otherwise you are able to set your own resetOnResponseError function.

@Vespand
Copy link
Contributor Author

Vespand commented Dec 30, 2023

This is not about resetOnResponseError setting at all

Any «rejected» interceptor should throw error for next «rejected» interceptor, otherwise error will be silent and any request will be success (no error handling)

There is logic @refactorjs/ofetch for interceptors - https://github.com/refactorjs/ofetch/blob/main/src/ofetch.ts#L239
If function will not return anything - call Promise.reject(error), why current interceptor is different and break this logic in chain?

This PR will add same logic, throwing error

I can rewrite this code, for using onResponseError, which is actually better and cleaner, but onResponseError need to return number, such as

this.responseInterceptor = this.http.onResponseError(error => {
    if (typeof this.auth.options.resetOnResponseError === 'function') {
        this.auth.options.resetOnResponseError(error, this.auth, this.scheme)
    }
    else if (this.auth.options.resetOnResponseError && error?.response?.status === 401) {
        this.scheme.reset?.()
        throw new ExpiredAuthSessionError();
    }
});

But need fix for @refactorjs/ofetch, for returning number for responseInterceptor and similar hooks

onResponseError(fn: (error: any) => any): number {
    return this.interceptors.response.use(undefined, (error: any) => fn(error) || Promise.reject(error))
}

I hope it's cleaner answer what happening and where is problem :)

@Denoder
Copy link
Member

Denoder commented Dec 30, 2023

I understand what you're saying now, I'll put this in v3.1.1 so for now ill close this.

@Denoder Denoder closed this Dec 30, 2023
@Denoder Denoder mentioned this pull request Dec 30, 2023
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

Successfully merging this pull request may close these issues.

2 participants