sofa
sofa copied to clipboard
Better error handling
Summary: The issue started from the fact that the inversion of Matrix returns a boolean to inform the user if the matrix has been effectively inverted or not, and displays a message in the Messaging console. A lot of bad points:
- almost nobody checks the return value,
- the really bad dependency on the Messaging system in a core component
- the message itself does not really say who could not invert the matrix
More generally this emphasizes the lack of a harmonious way to inform the user to handle errors, and how to be sure the user does something with it.
The common way to do it in the industry:
- error codes,
- exception,
- the 'expected' idiom : Systematic Error Handling in C++ by Dr. Andrei Alexandrescu ,
- for sure there are many more, feel free to tell in this issue
All of them have their cons and their pros, but I wont explain here (lots of blogs are better than me ;)
- https://ned14.github.io/outcome/motivation/
- https://bell0bytes.eu/expected/
- https://foonathan.net/2017/12/exceptions-vs-expected/
Demo with the use of std::exception: #1882
Demo with the 'expected' idiom: #1925
We would be glad to read the opinion on this topic from all @sofa-framework/reviewers
FWIW at Anatoscope we've been using exceptions for quite a while now, and we generally std::throw_with_nested
to provide additional context as exceptions bubble up (this is handy when pulling on long engine/data chains) to help debugging scene issues.
One of the main issues is to get proper exception translation across c++/python boundaries (both ways) while retaining as much of the original error as possible.
IMHO the "expected" approach is currently lacking on ergonomics (unlike say, haskell or rust's equivalents) and will probably put some more pressure on compile times if used widely. On the other hand it does solve some of the pain points with exceptions (types/speed).
I recall there was some discussion in the C++ committee a few years back in order to have some kind of lightweight exception mechanism (see e.g. https://www.youtube.com/watch?v=os7cqJ5qlzo) but this is still mostly future work as far as I can tell.
Anyway, my two cents :)
HTH,