We're using FFI to call potentially unsafe C functions. Sometimes bugs in such functions made our Haskell code that is using them to popup "Segmentation fault/access violation in generated code" message in the console. Under Windows I cannot do anything with that. I cannot see the source of the problem.
Is there a way to disable that message so the unhandled exception propagate to the OS, so we could catch it and see which C-compiled DLL on the call chain gives the error and where there?
Related
use anyhow::Context;
fancy_module::run()
.await
.with_context(|| {
format!("An error has been found")
})?;
From what I understand, when run returns an error, we return "An error has been found". But this message is not really meaningful. I would like to also return the error that run returns. Something like format!("An error has been found {}", e). How do I get e returned by run?
I could do that in multiple lines of code. By fetching the result of run and then having a match statement. Is there a nicer way of doing that?
From what I understand, when run returns an error, we return "An error has been found".
Not correct! context and with_context create a wrapper around the underlying error value to introduce additional context, but the original error is kept within!
Once you try displaying the error, you will see something of this sort:
Error: An error has been found
Caused by:
No such file or directory (os error 2)
(This also shows that "An error has been found" is a poor choice of words for creating additional context.)
I would like to also return the error that run returns. Something like format!("An error has been found {}", e).
That used to be a reasonable design decision in the past, but the current guidelines for error type design are against including the source error in the display message, unless that source error is also not reachable in the source error chain. The context methods will put the error in the source chain, so including that error's message into the top level error's message is ill advised.
How do I get e returned by run?
See the chain method in order to traverse the chain of source errors and so enable you to peek into underlying causes. Still, anyhow's error type was mostly designed to be opaque. If you need better introspection into what the error entails or easier pattern matching on errors, consider using a structural error type instead of anyhow::Error (snafu or thiserror can help you build them).
See also:
Should an Error with a source include that source in the Display output?
What is the difference between "context" and "with_context" in anyhow?
Simplier way to return custom error type with anyhow::Error?
I wanted to try out the new address sanitizer for MSVC, and after enabling it in my project I'm getting an access violation exception which the call stack says originates in __acrt_initialize() Line 291. Some research show that this happens before calling the main function, and indeed, a breakpoint on the first line of WinMain is not reached before this happens. With the sanitizer disabled, I am not getting any exceptions.
I recently completed making an asynchronous version for all the functions in a pure C API, wrapped with N-API to work with JS/TS as a nodejs addon.
The last problem I had to fix was making sure that C POSIX-style errors (ie, returned integer codes) were transferred correctly to the JS at the end of a worker's execution (with the corresponding string, for which we have both an enum of exceptions, and a list of error messages).
When thrown with napi_throw_error (as I did for the synchronous version of all our calls), within the napi_async_complete_callback, these exceptions were never caught at the JS level (I suppose it was because it was within a different async context; I saw online people having a similar problem with ajax). Instead, I opted to just construct my errors as napi_value types, and return these via napi_reject_deferred. This seemed to have the desired effect, of being caught properly when doing a try { await My_NapiWrapper_XYZ() } catch (ex) { ... }.
So I don't really have a problem to fix, but I AM intrigued. These napi_throw_error thrown errors do probably go somewhere. Though I have no idea where. Where should one look to catch an error thrown with napi_throw_error from a napi_async_complete_callback ? Can you give a code example ?
No, they don't go anywhere. It is a bug that I just opened with them:
https://github.com/nodejs/node/issues/41377
There is a general problem with handling exceptions in asynchronous callbacks. Normally, they cannot be catched and should lead to program termination but Node's developers have decided to try to keep it running when they can.
We have a number of sections of code in the format:
try
{
// code
}
catch(std::exception &e)
{
// log exception
}
catch(...)
{
// log unknown exception.
}
Every so often, the unknown exception code triggers, and logs an unknown exception.
I always thought that all exceptions were meant to derive from std::exception, and thus catching std::exception would catch all exceptions.
Is there some other exception that I should be catching?
If my code ends up in the unknown exception handler, is there any way that I can find out what exception was actually caught?
edit
We managed to locate the cause of the problem- despite saying that they had, the customer had not installed .NET 3.5, which our code depends on, and the system fell over when trying to use the XML parser.
Is there some other exception that I should be catching?
This depends on your code. Libraries you call can throw exceptions not derived from std::exception, examples are MFC's CException or Microsoft's _com_error. Also, an access violation might be catched by catch(...), which is the reason why I would not use catch(...) in my code - it's just to broad for me.
2.If my code ends up in the unknown exception handler, is there any way that I can find out what exception was actually caught?
You can run your code in the debugger and configure the debugger to break your program when the exception is thrown (first chance). Then you know exactly which line of code triggers the exception and should be able to see what exactly is thrown.
Occasionally the UnhandledException handler in my app is raised due to an unhandled XAML exception.
The UnhandledExceptionEventArgs contains the message
E_RUNTIME_SETVALUE
and an inner-exception of type ArgumentException
Value does not fall within the expected range.
There is nothing in the call stack other than InitialiseComponent() which I can step into/ over without any exception being thrown.
Any ideas on how to debug further or any experience with E_RUNTIME_SETVALUE issues?
I do remember I had to once add basically no-op value converters to some XAML so I could see what was going on and trace the error. That might help in this case as well.
Also try to turn on mixed-mode debugging to see if more data comes from the native stack.
I had this in UWP and it was because I was using OnIdiom
<OnIdiom x:Key="MyFontSize" x:TypeArguments="x:Double" Tablet="28" Phone="16">
</OnIdiom>
I didn't have desktop included in one of the values and I was running my app on my desktop.