If you want to use $latestError/message you need to make sure your Java Action is throwing an Exception. The message you place in the exception will be available in the message attribute.
If you want to use the log functionality instead and access these messages you would need to use something like the Logging module from the Marketplace. This redirects log messages to an entity in your domain model that you can Retrieve as an object in a microflow.
https://marketplace.mendix.com/link/component/159
Hope this helps.
I am not quite sure but I believe you can always find the “latestError” object where you can find the error message. If you use this as input for your widget, you can show the message you want
You do not have to retrieve, it will be availabe in your microflow and you could use like this to show it to your end users: