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

Failed requests break SSR even if they are handled #166

Closed
ffigiel opened this issue Oct 10, 2018 · 5 comments
Closed

Failed requests break SSR even if they are handled #166

ffigiel opened this issue Oct 10, 2018 · 5 comments
Labels

Comments

@ffigiel
Copy link

ffigiel commented Oct 10, 2018

I have a nuxt app with a onResponseError handler for handling exceptions.

The handler works great in client mode:
rendered page with an alert

But in server mode (SSR) the entire render breaks if a request fails:
generic nuxt error page

...even though my handler is being called correctly in SSR:
server log showing the handler works

I think that SSR should render a page with the alert, just like in the client mode. Am I doing something wrong?

This question is available on Nuxt community (#c170)
@ghost ghost closed this as completed Oct 10, 2018
@ghost ghost added the cmty:question label Oct 10, 2018
@ghost
Copy link

ghost commented Oct 10, 2018

This issue as been imported as question since it does not respect axios-module issue template. Only bug reports and feature requests stays open to reduce maintainers workload.
If your issue is not a question, please mention the repo admin or moderator to change its type and it will be re-opened automatically.
Your question is available at https://cmty.app/nuxt/axios-module/issues/c170.

@ffigiel
Copy link
Author

ffigiel commented Oct 10, 2018

Hey @pi0 I think it's a bug and not a question

@pi0 pi0 reopened this Oct 10, 2018
@pi0 pi0 added bug and removed cmty:question labels Oct 10, 2018
@pi0
Copy link
Member

pi0 commented Oct 10, 2018

Hey @megapctr. Actually, it is not a bug for this module and to be honest something expected with the current version of nuxt. Any thrown error in AsyncData/Fetch/Plugins/NuxtServerInit will cause SSR errors. We can improve it for sure.

One possible application-wide solution would be returning a fallback value in onResponseError handler: (Not tested but should work)

onResponseError() {
 // ...
 return Promise.resolve({ error: true })
}

The better idea would be to handle them inside asyncData:

async asyncData({ app }) {
   const something = await app.$axios.$get('...')
    .catch(() => Promise.resolve({ error: true }))

// something === { error: true } in case of any error

@ffigiel
Copy link
Author

ffigiel commented Oct 11, 2018

Thanks! Turns out the fix is even simpler, at least in my case:

 export default function({ $axios, store }) {
-  $axios.onResponseError(e => {
+  $axios.onResponseError(async e => {
     const { status, data } = e.response

😁

@pi0
Copy link
Member

pi0 commented Oct 11, 2018

@megacpr Your welcome and happy your problem is resolved. Please also note that in case of error, e.response is not always defined.

@pi0 pi0 closed this as completed Oct 11, 2018
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

2 participants