Global error handler not catching unhandled promise rejection ?
Last Updated :
28 Apr, 2025
Global Error: The starting value problem for a system of ordinary differential equations and the global, or true, error produced by one-step procedures are both studied. On an asymptotic global error expansion, some techniques for approximating this global error are based.
Unhandled promise rejection: When a JavaScript Promise with no rejection handler is denied, the unhandledrejection event is dispatched to the global scope of the script; normally, this is the window, but it might also be a Worker. This helps with debugging and provides fallback error handling for unforeseen circumstances.
Global error handler: Every time an error is thrown within the application, this function is called. The method has access to the error as a parameter for further processing. In this instance, a dialogue is opened to display the error message, and the error is also recorded in the browser console.
Unhandled promise: When a JavaScript Promise with no rejection handler is denied, the unhandledrejection event is dispatched to the global scope of the script; normally, this is the window, but it might also be a Worker. This helps with debugging and provides fallback error handling for unforeseen circumstances.
When an action could not be completed, usually (but not always) when a value is not of the anticipated type, the TypeError object records the error. When an operand or argument provided to a function is incompatible with the type expected by that operator or function, a TypeError may be thrown.
Callbacks made it possible to nest a lot of code, and they weren't very good at handling errors either. The standard error-handling practice for callbacks was to make sure that any potential errors were stored in the callback function's first parameter.
Then, in order to make sure that you treated it properly, you would have to check to see if that parameter exists. This was based on promises, and the catch function was used to handle errors. Any upstream errors are handled by the catch method, which guarantees this.
In order to capture every single instance of a rejected promise at runtime within your program, this guide will show you how to go one step further and add a global promise rejection handler.
Example 1: The ability to apply an event handler to a new, globally accessible property was introduced by browsers in 2016. Every time a promise rejection is not handled, the unhandledrejection event is triggered. You can see an example of a rejected promise that is not handled in the code below.
JavaScript
const P_Example = new Promise((resolve, reject) => {
reject('Ouch');
});
P_Example.then(() => {
console.log(`Its GFG Portal and I'm Amarjeet`);
});
// An Unhandled Promise Rejection Error occur
Output:
Ouch
The promise created by the straightforward code sample above will always be turned down. Because there was no catch technique in place to deal with probable mistakes, an error will happen when an attempt is made to fulfill the promise. The app experienced an uncaught error as a result of this!
To the rescue: the onunhandledrejection event handler! You can offer a global failsafe that will catch all rejected promises that go unhandled by using the onunhandledrejection event handler.
Example 2: The updated code below now includes our global promise rejection handler.
JavaScript
// Below function will handle unhandled promise rejections
const globalPromiseRejectionHandler = (event) => {
console.log('Unhandled promise rejection reason: ', event.reason);
}
// we will assign handler to the corresponding global, window property
window.onunhandledrejection = globalPromiseRejectionHandler;
const myPromise = new Promise((resolve, reject) => {
reject('Something Wrong');
});
myPromise.then(() => {
console.log(`It's GFG Portal`);
});
Output:
Something Wrong
If supplied the second argument,.then also handles errors in promises of all types, whether it be a reject() call or an error reported in a handler (which is the error handler).
.catch should be placed precisely where we want to handle mistakes and where we are aware of how to manage them. The handler ought to examine errors (custom error classes assist) and rethrow any that are unknown.
If there is no way to correct an error, it is acceptable to not use.catch at all.
Similar Reads
How to create a global Promise Rejection Handler in JavaScript ? In this article, we will try to understand firstly how we create a Rejected Promise, and later we will see how we may create a Global Promise Rejection Handler which will try to handle (somehow) that Rejected Promise created earlier in JavaScript. Let us first try to understand how we create a Promi
2 min read
Unhandle promise rejection in a basic catch block In this article, we will try to analyze the case where promise rejection is left unhandled in the basic catch block itself with the output which will receive later on its execution, and further, we will try to analyze its solution through an example. Let us first try to analyze the syntaxes which we
3 min read
Handling Promise rejection with catch while using await In this article, we will try to understand how we may handle Promise's rejection with a try/catch block while using await inside an asynchronous (having prefix as an async keyword) function (or a method). Syntax: Let us first quickly visualize the following illustrated syntax which we may use to cre
2 min read
Find what caused Possible unhandled promise rejection in React Native ? In this article, we will check out the `Possible Unhandled Promise Rejection` error in react native.Step-by-Step ImplementationStep 1: Create a React Native ProjectNow, create a project with the following command.npx create-expo-app app-name --templateNote: Replace the app-name with your app name fo
5 min read
How to Catch and Parse Errors Properly in React form Submission Handler ? We use forms a lot to get information from users, like their email, username, and password. Users send us this info through the forms, and we store it on the web server. The Form is an element that is used on a large scale in many websites which makes it an integral component. Creating a Form in Rea
4 min read