Enable Dark Mode!
error-handling-in-javascript-using-the-odoo-16-erp.jpg
By: Sonu S

Error Handling in Javascript Using the Odoo 16 Erp

Technical Odoo 16

A loosely typed language is JavaScript. There are no compile-time errors. Thus, accessing an undefined variable, calling an undefined function, etc., may occasionally result in a runtime error. Similar to other programming languages like Java or C, JavaScript offers an error-handling system that uses a try-catch-finally block to catch runtime errors.

try
{
    // code part that may throw an error
}
catch(ex)
{
    // code part to be executed if an error occurs
}
finally{
    // code part to be executed regardless of an error occurs or not
}
Suspicious code that might cause an error should be wrapped in a try block. Write code in a catch block to execute when an error occurs. You can get error information from the catch block's parameters. Catch blocks are typically used to record errors or show user-specific messages. Regardless of whether an error occurs, code in the final block will always be carried out. The remaining task can be finished, or variables that may have changed prior to an error occurring in the try block can be reset using the final block.
The methods are based on two aspects -general promises and Odoo based. General promises are primarily focused on promises. It doesn't ignore the control flow, which means that promises shouldn't reject, but in some circumstances, they do, whereas promises reject for errors. While that is the case, you will have a few resolutions of your promise with, for example, status codes that you may have to check in the then handlers and a single catch handler at the end of the promise chain.
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)

Function sample() {
    if (someCondition) {
        return Promise.reject("someCondition is truthy");
    }
    return Promise.resolve();
}
This example demonstrates how function a defines the variables x and y, where x is defined, and how an error in this case results in a promise. When a() .catch will catch the error and we console it, we know that it is an error. When a function b exists and returns a promise that rejects errors, a() . then(b) . catch will catch the error and output the result when then is skipped.
In Odoo, promise rejection is used to manipulate waft, just like it is in mutexes and other concurrency primitives covered in module net. Concurrency: We must also execute the catch for practical reasons, but we must stop doing so if the promise's definition or the handlers contain coding errors. In order to do this, guardedCatch has been introduced to us. It is still referred to as a capture, but not when the rejected goal is incorrect.
var promise =sample();
promise.then(function (result) { console.log("Message"); })
// this will be called if sample returns a rejected promise, but not if it has an error
promise.guardedCatch(function (reason) { console.log(reason); });
// ...
var anotherPromise =
       sample().then(function () { console.log("Message"); })
                // this will be called if sample 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
It demonstrates a function where it indicates an if condition returns a promise that rejects that specific condition, and it stores that function in a variable that catches the reason for error handling and comforts the reason. And if the function sample returns a rejected promise, another promise kept in a variable will be called, but not if it contains an error.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message