Kogito Boundary Error and Catch All Subprocess together - kogito

I have a node within my kogito workflow with a boundary error for a specific type of exception (e.g. BusinessException), and a separate catch all subprocess that should deal with all other exception types that are thrown. (currently configured to catch Throwable).
I would like to handle BusinessExceptions, e.g. CreditCardBlacklisted via Boundary errors.
However, when the node throws the CreditCardBlacklistedException, the boundary error is ignored and the catch all event subprocess is initiated.
Picture Of Workflow
How can I get this scenario to work so that when CreditCardBlacklistedException is thrown it goes to the Handle Blacklisted node and not the catch all sub process?

Fixed in the JIRA issue KOGITO-8191 to be released in version 1.31.0.Final.

Related

Exception in subscription terminates Observable in Angular but not in Node

I have this example code sequence that I either run in Angular or in a node app. Behavior is different, and I would like to know why.
of(1, 2, 3, 4).subscribe((x) => {
console.log(`x: ${x}`);
if (x === 2) {
throw new Error('xxx');
}
});
When executing in node all four numbers are written to the console. Exception is shown afterwards.
When executing in Angular, e.g. in any ngOnInit(), it only logs 1 and 2 and then the Observable is terminated. The consequence is that any exception in a subscription block breaks the program.
Is there any description of why and how this difference in behavior.
Addendum: Reading more on the subject, the Angular (or more general the browser) behavior is what is to be expected. It is really the node.js behavior that is surprising. Why does the node.js program not stop after two digits?
From the comments section, you are actually trying to figure out the difference in behavior between the rxjs v6 and v7 in terms of handling the consumer thrown unhandled errors.
In general throwing errors from the observers is a bad idea as it will lead to blocking of the observables if shared between multiple observers. This problem is handled in the v6 by improving the error handling mechanism as mentioned here SafeSubscriber and more specifically in next. Below snapshot of the next
next(value?: T): void {
if (!this.isStopped && this._next) {
const { _parentSubscriber } = this;
if (!config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
this.__tryOrUnsub(this._next, value);
} else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) {
this.unsubscribe();
}
}
}
If you see from the above snapshot when an unhandled error is thrown from the observer the code is trying to subscribe and then if error it is unsubscribing the observable in __tryOrUnsub. Finally the error is added to call stack and thrown once the call stack is empty from method hostReportError(once all the observers complete asynchronously). Below is the sample stackblitz to demonstrate the same.
https://stackblitz.com/edit/angular-ivy-v7dojh?file=src%2Fapp%2Fhello.component.ts
This logic further changed in v7 as mentioned by the this commit. The unsubscribing logic is removed and the asynchronous error call stack is maintained as shown below sample stackblitz, due to which the all the 4 elements are printed and the error is printed to console once the call stack is empty asynchronously usning reportUnhandledError -
https://stackblitz.com/edit/angular-ivy-p8zj2k?file=src%2Fapp%2Fhello.component.ts

node option: making it quite when an error is thrown

is there a node cli option that lets it not print an error?
For example, I'd like the command below not to print an uncaught exception, even if it occurs. But I don't simply want to catch it in the code. If there's no exception or error, then it should print (if message to print exists) what it's intended to.
$ node --quiet-on-err(?) this-script-throws-err.js
Thanks,
This is a good use case for redirection (assuming you're using a platform that has /dev/null): node this-script-throws-err.js 2/>dev/null.
Wouldn't catching all uncaught exception work for you? Add this to the code. This is a process wide event handler that gets called whenever an uncaught exception is found.
process.on('uncaughtException', (exception) => {
// handle or ignore error
});

How to deal with pdo errors

I have a question, in the PDO manuial somewhere I read that errors reveal the db connect with username and password (due to a flaw in the zend engine). I see several examples of catching the pdo like this:
catch(PDOException $exception){
return $exception;
}
if the exception is returned, doesn't the user see the error?
Is it better to have disabled the error reporting in the php.ini file, or even do something like
setAttribute(PDO::ERRMODE_SILENT)
instead of the catch statement, or is it better to do a combination of above and redo the catch statement so it doesn't return the error to the user.
This is referring to the pink paragraph on the manual page that says: Warning: If your application does not catch the exception thrown from the PDO constructor, the default action taken by the zend engine is to terminate the script and display a back trace. This back trace will likely reveal the full database connection details, including the username and password. It is your responsibility to catch this exception, either explicitly (via a catch statement) or implicitly via set_exception_handler(). php.net/manual/en/pdo.connections.php.
The user "YOUR COMMON SENSE" marked this as duplicate which is not correct. I don't have an issue with using PDO, Its just a question of dealing with error responses, and correct methodology of error handling.

Is there a Node.js equivalent of Java UncaughtExceptionHandler?

I had some problematic code in a Node.js application which caused an error (manifested as strange output) but not a total crash. It displayed nothing in the console to indicate an error occurred, and I only identified the root cause by a lot of trial and error in commenting out lines selectively.
To save time in future, is there anything like Java's UncaughtExceptionHandler in Node that will catch anything that's causing errors and display them in the console so I can pinpoint bug(s) immediately?
Yes. You can listen for that event by doing this
process.on('uncaughtException', (err) => {
});
It will override the default behaviour of exiting.
Documentation

Handling errors at a global level

I am trying to understand how to build my error handling system for my api.
Let's say I have a the following line in a controller method :
var age = json.info.age;
with
json = {"id":1, "name":"John", info": {"age":27, "sex":"m"}}
Let's say that the object doesn't contain an info field, I'll get the following error TypeError: Cannot read property 'info' of undefined and my server will crash.
Is there a way to make a higher level abstraction and catch all the potential errors that I could have? Or should I have a try/catch system for each of the methods of my controllers?
BEWARE OF THE CODE BELOW, IT WILL BITE YOU WHENEVER IT CAN!
Don't use the code snippet below if you do not understand its
implications, please read the whole answer.
You can use the node way for uncaught errors. Add this in your config/bootstrap.js
Updated the snippet below to add what was said in the comments, also added a warning about using a global to respond to the user.
process.on('uncaughtException', function (err) {
// Handle your errors here
// global.__current__ is added via middleware
// Be aware that this is a bad practice,
// global.__current__ being a global, can change
// without advice, so you might end responding with
// serverError() to a different request than the one
// that originated the error if this one happened async
global.__current__.res.serverError();
})
Now, can doesn't mean should. It really depends on your needs, but do not try to catch BUGS in your code, try to catch at a controller level the issues that might not happen every time but are somehow expected, like a third-party service that responded with empty data, you should handle that in your controller. The uncaughtException is mainly for logging purposes, its better to let your app crash if there is a bug. Or you can do something more complicated (that might be better IMHO), which is to stop receiving requests, respond to the error 500 (or a custom one) to user that requested the faulty endpoint, and try to complete the other requests that do not relate to that controller, then log and shutdown the server. You will need several instances of sails running to avoid zero downtime, but that is material for another question. What you asked is how to get uncaught exceptions at a higher lvl than the controllers.
I suggest you read the node guide for error handling
Also read about domains, even thought they are deprecated you can use them, but you would have to deal with them per controller action, since sails does not provide any help with that.
I hope it helps.
You can check this way if you want to:
if (object != null && object.response != null && object.response.docs != null){
//Do your stuff here with your document
}
I don't really get what is your "object" variable in the first place, so i don't know if you can check it at a different level, is it a sails parameter to your controller ?
So that's how I did it, thanks to Zagen's answer.
module.exports.bootstrap = function(cb) {
process.on('uncaughtException', function (err) {
//Handle your errors here
logger.fatal(err);
global.__current__.res.serverError();
})
cb();
};
I send a generic error 500 to the user if any uncaught exception is thrown, and I log the error to the fatal level. On that way, my server is still accessible 24/7 and I can monitor the logs at another level and trigger an alarm on a fatal error. I can then fix the exception that was thrown.

Resources