Error handling gives the response and retrieval for error conditions for the current software environment. Also, it is the process of prediction, distinguishing, and resolution of system errors. The error handling mechanism helps Odoo catch runtime errors
using try-catch-finally block, it comprises of two types they are
- General in promises
- Odoo specifically
Both of these mechanisms catch the runtime errors and catch the exceptions that are thrown. Let’s go through each mechanism and its rejection for the control flow and errors.
General in promises
In this General in promises, the mechanism is mainly focused on promises; it doesn’t neglect the control flow; it means promises should not reject it, but for some cases, it whereas promises reject for errors. While that is the case, you'll have a couple of resolutions of your promise with, for instance, status codes which you could have to check within the then handlers and a single catch handler at the cease of the promise chain.
Eg:-
function a() {
x.y(); // <-- this is an error: x is undefined
return Promise.resolve(1);
}
function b() {
return Promise.reject(2);
}
a().catch(console.log); // will log the error in a
a().then(b).catch(console.log); // will log the error in a, the then is not executed
b().catch(console.log); // will log the rejected reason of b (2)
Promise.resolve(1)
.then(b) // the then is executed, it executes b
.then(...) // this then is not executed
.catch(console.log); // will log the rejected reason of b (2)
This example emphasizes that function a defines the x and y where x is defined, and it was an error that returns a promise. when a().catch will catches the error and while we console it, that the error And also when a function b exists which return the promise where reject the errors, a().then(b).catch which catches it gives the output when then is not executed
Similarly, b().catch will give a log of the rejected reason when we console it.
Odoo specifically
We are then moving to essential errors coping with the mechanism of javascript. In Odoo, it takes place that we use promise rejection for manipulating waft, like in mutexes and different concurrency primitives described in module net. Concurrency: We also need to execute the catch for business reasons, but no longer whilst there may be coding blunders within the definition of the promise or the handlers. For this, we've got introduced to the concept of guardedCatch. It's far called a capture, but not whilst the rejected purpose is an error.
Function hey() {
if (someCondition) {
return Promise.reject("someCondition is truthy");
}
return Promise.resolve();
}
// ...
var promise =hey();
promise.then(function (result) { console.log("everything went fine"); })
// this will be called if hey returns a rejected promise, but not if it has an error
promise.guardedCatch(function (reason) { console.log(reason); });
// ...
var anotherPromise =
hey().then(function () { console.log("everything went fine"); })
// this will be called if hey returns a rejected promise,
// but not if it has an error
.guardedCatch(console.log);
var promiseWithError = Promise.resolve().then(function () {
x.y(); // <-- this is an error: x is undefined
});
promiseWithError.guardedCatch(function (reason) {console.log(reason);}); // will not be called
promiseWithError.catch(function (reason) {console.log(reason);}); // will be called
Here in this example, it illustrates a function where it shows an if condition returns a promise rejects of that particular condition, that function stored in a variable which catches the reason for error handling where it consoles the reason. And another promise stored in a variable will be called if the function hey returns a rejected promise but not if it has an error it catches.
Then It also follows that the promise resolves a function where x.y x is undefined and catches the reason in promiseWithError.guardedCatch .Using this mechanism gives responsibility for the error procedures very well.