Display error notifications using Nuxt.js rather than redirecting to a new page

Is there a way to configure my Nuxt application to handle server errors by displaying a notification or toast message instead of redirecting to a custom error page? I've been searching for examples or tutorials on error handling methods but have only come across guides on creating custom error pages. Any advice would be appreciated. Thank you!

https://i.sstatic.net/ngNFc.png

Answer №1

If you're struggling with this issue, I found a helpful solution that guided me in the right direction here

This article also sheds light on the topic here

The trick is to create a wrapper component called ErrorBoundary and enclose your template within it.

Check out my ErrorBoundary.vue

<template lang="">
   <div class="position-relative">
    <slot></slot>
   </div>
</template>
<script>
export default {
  name: 'ErrorBoundary',
  data: () => ({
    error: false
  }),
  errorCaptured (err, vm, info) {
    this.$axios.post('/api/log-error', {message: err.message, stack: err.stack, info: info});
    this.$toasted.error(err.message, {
      position: 'bottom-right'
    }).goAway(4000);
    return false;
  },
}
</script>

Here's an outline of my /layouts/default.vue

<error-boundary>
 <my-whole-website-here></my-whole-website-here>
</error-boundary>

I've integrated error logging through axios for server communication. A toast notification pops up displaying the error message. Pay attention to the return false; statement at the end of this function, as it helps avoid default error handling and prevents redirection to an error page.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm currently in the process of creating a snake game using HTML5. Can you help me identify any issues or problems that

I am experiencing an issue where nothing is appearing on the screen. Below are the contents of my index.html file: <html> <body> <canvas id="canvas" width="480" height="480" style="background-color:grey;"></canvas> <script src=" ...

Typescript encountering onClick function error during the build process

My current challenge involves creating a submit function for a button in my application. However, when I attempt to build the project, I encounter a typing error that is perplexing me. Despite trying various methods, I am unable to decipher how to resolve ...

Remove the list by conducting a comparison analysis

<html> <head> <title> Manipulating List Items Using JavaScript </title> <script type="text/javascript"> function appendNewNode(){ if(!document.getElementById) return; var newlisttext = document.changeform.newlist.val ...

sending an array via xmlhttprequest

I am attempting to send all the marks entered by the user to another page for processing, but only the value of the first mark entered in the table is being sent. Is there a way to send all the values of the marks entered rather than just the first value ...

``There seems to be a malfunction with the modal feature in the asp.net

Within my strongly typed view, I have a loop that iterates over a list of objects fetched from a database. Each object is displayed within a jumbotron element, accompanied by a button labeled "Had role before". When this button is clicked, a modal opens wh ...

What causes the less-than-perfect smoothness in my CSS3/jQuery transitions and how can I improve their fluidity?

After tirelessly searching for answers all over the web, I've hit a dead end and now I turn to you for assistance. My current dilemma revolves around the lack of smoothness in my animations, whether it be through jQuery .animate or CSS3 transitions. ...

Using the Unleash Feature server in a browser for React projects: A step-by-step guide

I'm currently working on implementing the unleash feature toggle in my React Project. Everything is running smoothly with the backend server (thanks to the Java SDK), but I've hit a roadblock when it comes to making unleash requests from the brow ...

Tips for deleting a row from a JavaScript list

Plunker I have a list from which I want to remove a row. The initial code is as follows: var newArray = _.filter($scope.componentList, function(arrayItem) { return rowId !== arrayItem.rowId; }); $scope.componentList = newArray; This filter function will ...

When using the image-webpack-loader with Vue-cli 3 and Webpack 4, there is an issue loading images in the .webp format

Having trouble loading images in my Vue components with the .webp extension. <v-parallax :src="require('@/assets/images/hero.webp')"> I installed the image-webpack-loader module yarn add image-webpack-loader --dev In my vue.config.js f ...

Using jQuery to trigger alert only once variable has been updated

I have a question that may seem too basic, but I can't find the solution. How do I make sure that the variables are updated before triggering the alert? I've heard about using callbacks, but in this case, there are two functions and I'm not ...

Why is the Google Maps API not displaying the map once the app is deployed on Heroku?

const express = require('express'); var app = express(); var bodyParser = require('body-parser'); var exphbs = require('express-handlebars'); var cors = require('cors'); //setting up the view engine app.engine(&apos ...

Error message: "User not found during passport authentication"

I am currently in the process of developing an authentication system for my website. However, I am encountering a specific error message when attempting to log in with any combination on the website: ReferenceError: user is not defined user is not define ...

Attempting to mark fields as required with asterisks using React Final Form in a React project

Is there a way to display an asterisk next to a label on a form using React Final Form? Here is what I have tried: <Field name="requestDescription" {...(<span style={{ color: 'red' }}>*</span&g ...

Transform js into a more dynamic format to avoid redundancy when displaying items upon click

I came across a simple lightbox code snippet, but it's based on IDs and I plan to use it for more than 20 items. I don't want to manually write out 20 JavaScript lines when there could be a more efficient way to handle it dynamically. My JS skill ...

Emphasize sections of text within a chart

Looking for a Specific Solution: I've encountered similar problems before, but this one has a unique twist. What I'm trying to achieve is to search for a substring within a table, highlight that substring, and hide all other rows (tr's) th ...

A user error is generated when making a call to an Elrond Smart Contract using the JavaScript SDK

I'm attempting to invoke a function from an Elrond Smart Contract to generate an NFT using their JavaScript SDK. Here is the error I encountered: TransactionCompletionStrategy.isCompleted(), found event: signalError Transaction return code: user er ...

A comprehensive guide on making an AJAX call to a self-hosted servlet

I created a servlet in Eclipse IDE for Java EE that posts data as an XML page and hosted it on a Tomcat server. The servlet can be accessed at http://localhost:8080/Checkers/CheckersServlet. When I open this URL in Firefox, the XML loads correctly. Now, I& ...

When working with AngularJS, using ng-repeat to loop through elements may encounter difficulties if the key contains a colon ":"

I am facing an issue while trying to loop through a simple JSON in AngularJS using ng-repeat. The specific JSON I am working with is as follows: $scope.lines = { data: [ { "properties": { "name": "My test", ...

Is Firefox preventing the running of asynchronous JavaScript code?

My experience with writing tests for my web application in the Selenium Framework has been smooth sailing with both Chrome and Edge. However, I've encountered a problem specifically with Firefox - the asynchronous script keeps timing out. I suspect i ...

Updating a table in Javascript after deleting a specific row

Is there a way to automatically reindex rows in a table after deleting a row? For example, if I delete row 1, can the remaining rows be reordered so that they are numbered sequentially? function reindexRows(tableID) { try { var t ...