Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 app.config.errorHandler Property

Vue 3 app.config.errorHandler Property


Vue3 Global API Vue 3 Global API

app.config.errorHandler is a very practical property that allows developers to globally capture and handle errors in the application.


1. What is app.config.errorHandler?

app.config.errorHandler is a global configuration option in Vue 3 used to capture and handle uncaught errors in the application. Whether errors are thrown from a component's lifecycle hook functions, event handlers, or asynchronous operations, they can all be captured and handled via app.config.errorHandler .


2. How to Use app.config.errorHandler?

To use app.config.errorHandler, first create a Vue instance and set errorHandler property in the configuration. Here is a simple example:

Example

import

{

createApp

}

from

'vue'

import

App from

'./App.vue'

const

app

=

createApp

(

App

)

app.

config

.

errorHandler

=

(

err

,

vm

,

info

)

=>

{

  console.

error

(

'Captured global error:'

,

err

)

  console.

error

(

'Component instance where the error occurred:'

,

vm

)

  console.

error

(

'Error message:'

,

info

)

}

app.

mount

(

'#app'

)

In this example, we created a Vue application and set errorHandler. When an uncaught error occurs in the application,errorHandler function is called, and the error information, the component instance where the error occurred, and the error information are passed to this function.

Parameter Description:


3. Practical Application Scenarios

app.config.errorHandler In real development there are many application scenarios; here are some common usages:

3.1 Error Logging

In a production environment, capturing and recording error logs is very important. Through app.config.errorHandler, we can send error information to the server for storage and analysis.

Example

app.

config

.

errorHandler

=

(

err

,

vm

,

info

)

=>

{

 

// send error information to the server

  fetch

(

'/log-error'

,

{

    method

:

'POST'

,

    body

:

JSON.

stringify

(

{

err

,

vm

,

info

}

)

 

}

)

  console.

error

(

'Captured global error:'

,

err

)

}

3.2 User-Friendly Prompts

When an error occurs in the application, we can use app.config.errorHandler to display user-friendly prompt messages instead of letting users see a complex error stack.

Example

app.

config

.

errorHandler

=

(

err

,

vm

,

info

)

=>

{

  alert

(

'Sorry, something went wrong with the application. Please try again later.'

)

  console.

error

(

'Captured global error:'

,

err

)

}

3.3 Error Recovery

In some cases, we can try to recover from errors in app.config.errorHandler to avoid application crashes.

Example

app.

config

.

errorHandler

=

(

err

,

vm

,

info

)

=>

{

  console.

error

(

'Captured global error:'

,

err

)

 

// attempt to recover application state

  vm.$forceUpdate

(

)

}


4. Notes


Vue3 Global API Vue 3 Global API