How to handle all requests/ajax errors globally

how can i handle all the failed requests? is there someplace in the app init to set the default error message in case of error 401 for example in ajax call?

Create a method in the root component, for example:

methods: {
    handleRequestErrors(xhr, status) {
        if(status === 401) {
            // Do what you want here
        }
    },
}

Then in every other place in your app, when you perform a request:

this.$app.request.promise({
    // Request parameters
})
    .catch(error => this.$root.handleRequestErrors(error.xhr, error.status))
2 Likes

If you are just worried about router events, you can use: routerAjaxError

If all events and don’t want to define per event, use:

app.request.setup({
     error: function(xhr, status, message) { /* actions */ }
})

Please note in docs, it’s marked as “Not recommended”

1 Like

Thanks for the suggestion but i still have to go to every request in my app and change something there. witch what i don’t wish to do