An alternative to try/catch error handler
LandyCuadra opened this issue · 6 comments
Is the feature request related to a problem? Please describe.
I would like to stop using try/catch in schemas methods, so I would like to know if there is a way to create an error collector, that every error that happens in any method get handled by a single block of code
Describe the solution you'd like
`
try {
//Operation
}catch(err){
const {name, message} = err;
$.invalid(name, message);
console.error(`SCHM: ${schema.name} | $${workflow} | ${method} | ${err.stack}`);
}
`
I want to declare the catch handler only one time because it will always do the same
Describe alternatives you've considered
Tried overwritting the DEF.onError function, but could not find a way to send the controller to it
Additional context
What I would like to do is to send a controller to the default error handler to do a $.invalid() after showing the error in console or if there is a global way to return a response on error
@LandyCuadra this is not possible, because ErrorBuilder can be created without controller - for example via EXEC()
method.
@LandyCuadra the example with the undefined variable
is actualy a bug and not something you should need try/catch for.
But if you want to avoid using try/catch you could try something like this:
// global try/catch wrapper
function trycatch(handler) {
return async function($, value) {
let result;
let error;
try {
result = await handler($, value);
} catch(e) {
error = e;
}
if (!error)
$.success(result);
else
$.invalid(); // or any other logic
};
};
schema.setQuery(trycatch(async function($, value) {
// do not use $.success or $.invalid here
// just return data
return { anything: 'here' };
}));
I'm closing issue.