So far I've tried comparing Parse.Promise, Q and Bluebird but I haven't been able to get an error or catch block to handle/capture an async runtime error.
If you believe that one of the libraries mentioned above, can handle/capture async runtime errors and suspect that I may not have written my sample code correctly, please feel free to point out any mistakes: https://github.com/pulkitsinghal/example-promises
Otherwise I'm looking for suggestions for other promise libraries or frameworks to explore.
It is simply impossible for a library to handle errors beyond its control. The only reason promise libraries are throw safe is because promises use return values to assimilate other promises.
If all your code returns promises instead of callbacks, All A+ promise libraries (that's Q and Bluebird in your example but not Parse.promise) will catch runtime errors and bluebird will even report them automatically without the need to .catch them.
Domains are being deprecated and don't really work well in practice and there is no node-wide solution. Your only option really is to stick to promises app-wide. You must kill and restart the server on an uncaught exception since some parts in node code that throw those do not clean very well after themselves when they throw (this is part of why domains are deprecated to begin with).
So:
Promises are throw safe for runtime errors if you use promises app-wide.
If you have to run untrusted code that might throw consider running it in a VM using the vm module.
Sometimes there are node errors that leave you no choice but to restart a server.
Promisify at the lowest level possible to avoid promisified functions throwing.
Here is a broader question that discusses async error-handling in NodeJS.
Related
We have developed a chatbot using Azure bot framework. As part of our CI-CD pipeline, we use Sonar Qube to do static code analysis.
Sonar shows multiple instances of code smells as “Redundant use of await on a return value”. The recommendation from Sonar is not to use await as the async method is expected to use a promise.
However, this approach is taken from the BOT Framework samples provided by Microsoft (https://github.com/microsoft/BotBuilder-Samples/blob/main/samples/typescript_nodejs/13.core-bot/src/dialogs/bookingDialog.ts)
Can you please confirm if Microsoft recommendation has changed or this seems to be false positive alert from SonarQube ?
First of all, this Sonar rule was added about 2 years ago in this Pull Request with this example
I then found those SO articles answering similar questions: article 1, article 2 but it was still unclear to me so I kept on looking.
Finally I reviewed this documentation and found the answer I was looking for in the last example provided.
In the above example, notice there is no await statement after the return keyword, although that would be valid too: The return value of an async function is implicitly wrapped in Promise.resolve - if it's not already a promise itself (as in this example).
Note: The implicit wrapping of return values in Promise.resolve does not imply that return await promiseValue is functionally equivalent to return promiseValue.
I tried the error handling with and without the await on my project and ended up removing the await triggering the warning. So far I haven't seen any difference. I have also noticed that if you wrap the same code inside a try / catch, the Sonar warning isn't raised anymore.
From now on, I will follow Sonar's advice but will update this thread if I encounter an issue.
We have a long established, greatly multiplatform codebase that is currently being ported to WinRT. One of the challenges we're facing is how to handle WinRT's asynchronous style.
For example, we are unsure how to handle WinRT's async file operations. Unsurprisingly, our codebase's API is synchronous. A typical example is our File::Open function which attempts to open a file and return with success or failure. How can we call WinRT functions and yet keep the behavior of our functions the same?
Note that we are unfortunately constrained by legacy: we cannot simply go and change the API to become asynchronous.
Thanks!
I assume you wish to reimplement the library to support WinRT apps while not changin the definitions of the APIs so that existing applications remain compatible.
I think that if you don't include the await keyword when calling an async method you will not do an async operation, it should execute in a synchronous way. But it really doesn't work if the method returns a value (in my experience).
I've been using this code to make a file operation synchronous:
IAsyncOperation<string> contentAsync = FileIO.ReadTextAsync(file);
contentAsync.AsTask().Wait();
string content = contentAsync.GetResults();
If you want to share your code with a platform that does not support async/await - you are probably better off having a different API for the old platform and the new one with switches like
#if SILVERLIGHT
#elif NETFX_CORE
#elif WPF
#endif
Eventually the async APIs are likely to show up in older platforms and you could actually wrap the non-async calls into Tasks to make them async if they don't. Forcing async method to work synchronously is bound to bite you back rather quickly. Your WinRT app might become unresponsive for a few seconds for example and get killed by the OS. Or you could get deadlocks waiting for tasks to complete and blocking the thread they try to complete on.
I'm from an Android background and when looking up a method in the Android developers reference, the information usually includes what exceptions the method can throw (as well as the parameters the method takes in and its return type). I've had a browse of some classes in the MSDN library and this doesn't seem to be the case here. So how, when developing, can I determine what exceptions a method can throw (if it throws any exceptions)?
A concrete example is the DataContext.SubmitChanges() method (MSDN link), which can throw an SqlCeException exception. It seems there's no way of picking up on this unless it's encountered accidentally at run-time.
.NET is a bit different than java in exceptions. There is no throws syntax, where you have to declare what types of exceptions can be thrown from the method. Every method can possibly throw any kind of exception. That's why not always MSDN documentation contains that kind of data.
When you can't find list of possible exceptions on MSDN pages you can search/ask about it on sites like stackoverflow (eg. for DataContext.SubmitChanges()) or just test your app and try to generate the exception to check what type it is.
There is no equivalent to the throws keyword in .net, but you can tell your user what exceptions you know your method may throw in your doc-comments (C# equivalent to java doc)
I've seen many wrappers for the Windows API (MFC, ATL, WTL, etc.) but none of them seem to use exception-handling -- you still have to check the error codes for most functions, which is easy to forget (or to omit due to laziness).
Is there any wrapper out there that actually throws exceptions instead of returning error codes?
The VCL raises exceptions when it encounters errors.
VB 6.0 does not have any global handler.To catch runtime errors,we need to add a handler in each method where we feel an error can occur.But, still some places might be left out.So,we end up getting runtime errors.Adding error handler in all the methods of an application,the only way?
No there is no way to add a global error handler in VB6. However, you do not need to add an error handler in every method. You only really need to add an error handler in every event handler. E.g. Every click event,load event, etc
While errors do propogate upwards, VB6 has no way to do a stack trace, so you never know which method raised the error. Unfortunately, if you need this information, you have to add a handler to each method just to log where you were.
Also: errors do propagate upwards: if method X calls methods Y and Z, a single error handler in method X will cover all three methods.
I discovered this tool yesterday:
http://www.everythingaccess.com/simplyvba-global-error-handler.htm
It is a commercial product that enables global error handling in VB6 and VBA applications.
It has its cost but does its job perfectly. I have seen other tools (free though) helping in this VB6 mangle, but none can cover a true real global error handling like "SimplyVB6 Global Error Handler for VB6" does.
With "SimplyVB6 Global Error Handler for VB6", there is no need to change any line of existing code, and no need to number the lines of code (via a plug-in or something).
Just enable Global error handling (one line of code in the main module) and you are all set.
"SimplyVB6 Global Error Handler for VB6":
can show the call stack with real module and function names, as well as display the source code line.
Works only with P-Code compiled VB6 programs.
can work via early or late binding (no DLL Hell).
I am not in any way affiliated to www.everythingaccess.com, just happy to have found it yesterday afternoon, was kind of looking at this problem again as one of my customers was having bugs in our VB6 application. I was able to test the tool yesterday afternoon, exchanging emails with the www.everythingaccess.com support and getting the evaluation product per mail.
Their web side does not allow yet to download the evaluation version of the VB6 product, you have to email them but they are answering in less than an hour.
on error resume next - is kinda close but its been a while.
you might want to look up any caveats