Giter VIP home page Giter VIP logo

Comments (5)

pi0 avatar pi0 commented on May 18, 2024 1

Will be enhanced soon allow customizing behaviour :)

from legacy-modules.

silverbackdan avatar silverbackdan commented on May 18, 2024 1

As of PR #74 - you can now disable the error handler which may be enough to close this issue

from legacy-modules.

PMK avatar PMK commented on May 18, 2024

Because of this hard-coded URI: https://github.com/nuxt-community/modules/blob/master/modules/axios/plugin.js#L72

from legacy-modules.

silverbackdan avatar silverbackdan commented on May 18, 2024

Is it going to be as simple as changing that 401 if statement to this?

if (error.response.status === 401) {
    <% if(options.loginRedirect) { %>
        return this.redirect(process.env.LOGIN_URL || '<%= options.loginURL %>' || '/login' )
    <% } else { %>
        error.statusCode = 401
        error.message = (process.env.UNAUTHORIZED_ERROR_MESSAGE || '<%= options.unauthorizedErrorMessage %>' || 'Unauthorized' )
    <% } %>
} else {
    error.statusCode = error.statusCode || parseInt(error.response.status) || 500
    error.message = error.message || error.response.statusText || (error.statusCode + ' (Internal Server Error)')
}

And perhaps adding some docs about the environment vars, options and 401 handling?

from legacy-modules.

silverbackdan avatar silverbackdan commented on May 18, 2024

this.redirect should be this.app.router.go now as well.

This works but I'm wondering how useful the automatic redirecting is anyway, perhaps leave this up to the user? Happy to make a PR with or without the redirect feature if you'd like me to (unless you're already had more features in mind)

// Nuxt friendly error handler
function errorHandler(error) {
  if (error.response) {
    // Error from backend (non 2xx status code)
    if (error.response.status === 401) {
      <% if(options.loginRedirect) { %>
        return this.app.router.go(process.env.LOGIN_URL || '<%= options.loginURL %>' || '/login' )
      <% } else { %>
        error.statusCode = 401
        error.message = (process.env.UNAUTHORIZED_ERROR_MESSAGE || '<%= options.unauthorizedErrorMessage %>' || 'Unauthorized' )
      <% } %>
    } else {
      error.statusCode = error.statusCode || parseInt(error.response.status) || 500
      error.message = error.message || error.response.statusText || (error.statusCode + ' (Internal Server Error)')
    }
    error.statusCode = error.statusCode || parseInt(error.response.status) || 500
    error.message = error.message || error.response.statusText || (error.statusCode + ' (Internal Server Error)')
  } else if (error.request) {
    // Error while making request
    error.statusCode = error.statusCode || 500
    error.message = error.message || 'request error'
  } else {
    // Something happened in setting up the request that triggered an Error
    error.statusCode = 500
    error.message = error.message || 'axios error'
  }
  // Display error page on unhandled promises
  if(process.browser) {
    return Promise.reject(error)
  } else {
    // Don't throw unhandled promises in SSR context
    return this.app._nuxt.error.call({$options: this.app}, {
      message: error.message,
      statusCode: error.statusCode
    })
  }
}

from legacy-modules.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.