-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error handling #75
Conversation
aff80a4
to
f77dcda
Compare
…ception handling and helper functions
f77dcda
to
44d141d
Compare
Example from the unit tests: CHECK_THROWS(lsl::stream_info("", "emptyname"));
CHECK(std::string("The name of a stream must be non-empty.") == lsl_last_error()); |
That does look pretty good, and I do like the generic implementation. Also nicely declutters some code. There's one thing that would make sense to tackle here, and that's that the Btw I didn't see the last error being zero-initialized. |
On the one hand "My code makes errors faster than I can check them" is a very specific issue, on the other hand it's literally two changed lines (not including the unit test). Added in 95f8277.
Done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff, looks clean to me.
Let's take a simple example:
On a first glance, this should print the time the stream_info was created at.
On a second glance, the format is called
float64
, notdouble64
so an exception should be thrown and the function should return.In reality, a
lsl::stream_info
object is created, but the internal pointer is set to 0 so the call toinfo.created_at()
will dereference a null pointer and everything will crash.This PR first adds the helper macro
LSL_CATCH_EXCEPTIONS
and the helper functioncreate_object_noexcept
for the C-API to convert exceptions to error codes and copy the error message to an internal variable. The last error message can then be retrieved via thelsl_last_error()
function.After that, the C++ wrapper can check for errors (i.e. is the returned pointer valid) or throw an exception with the last error message.