Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add custom error handling option #88

Merged
merged 15 commits into from
Apr 26, 2024
Merged

Conversation

mattldawson
Copy link
Collaborator

Adds an option for users to provide a custom error handling function through the C API that will be called for any exceptions thrown by the C++ libraries.

closes #38

I'm not sure if this is the best design. I think it will work for the CAM-SIMA use case where they just want to provide a single function that gets called for any error. But, I'm not sure if a global function pointer for errors is the best solution. I don't mind changing this approach entirely if anyone has a better solution.

@mattldawson mattldawson marked this pull request as draft April 12, 2024 23:21
@mattldawson mattldawson marked this pull request as ready for review April 12, 2024 23:38
@K20shores
Copy link
Collaborator

The only thing I really dislike is random numbers we have to provide as the code argument. I can't think of a better way right now

@@ -34,7 +35,7 @@ program test_micm_fort_api


write(*,*) "[test micm fort api] Creating MICM solver..."
micm => micm_t(config_path, errcode)
micm => micm_t(config_path)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no recourse here for when creating micm fails. The error function is called, I think, but the program still thinks that micm is valid.

return micm;
} catch (const std::exception &e) {
delete micm;
Error(909039518, std::string("Error creating MICM solver: " + std::string(e.what())).c_str());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these codes are really meant to make where the error happened identifiable, maybe this is another time where a macro is useful and we use __LINE__ and __FILE__ to do this instead?

@mattldawson
Copy link
Collaborator Author

The only thing I really dislike is random numbers we have to provide as the code argument. I can't think of a better way right now

Yeah, I agree. After more discussion and checking with the CAM-SIMA developers, I think I might redo this PR to just have the MUSICA C API functions always return an error code and message by catching any exceptions thrown in MICM. I will also explore options that don't require us to create random numbers for error codes.

@mattldawson mattldawson marked this pull request as draft April 16, 2024 21:25
@mattldawson mattldawson marked this pull request as ready for review April 25, 2024 21:09
@mattldawson
Copy link
Collaborator Author

I think this is ready for another look now. All the API functions return an error object that has a code, category, and message. The combination of the code and category can be used to identify specific errors, and there are some helper functions to do this. Let me know what you think!

Copy link
Collaborator

@K20shores K20shores left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

Copy link

@mwaxmonsky mwaxmonsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @mattldawson!

Had two suggestions to use base functions in the error handler that might be worth while but nothing that should hold up the PR.

src/util.cpp Outdated
Comment on lines 28 to 35
Error NoError()
{
Error error;
error.code_ = 0;
error.category_ = ToConstString("");
error.message_ = ToConstString("Success");
return error;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Error NoError()
{
Error error;
error.code_ = 0;
error.category_ = ToConstString("");
error.message_ = ToConstString("Success");
return error;
}
Error NoError()
{
return ToError(ToConstString(""), 0, ToConstString("Success"));
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

src/util.cpp Outdated
Comment on lines 51 to 58
Error ToError(const std::system_error& e)
{
Error error;
error.code_ = e.code().value();
error.category_ = ToConstString(e.code().category().name());
error.message_ = ToConstString(e.what());
return error;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Error ToError(const std::system_error& e)
{
Error error;
error.code_ = e.code().value();
error.category_ = ToConstString(e.code().category().name());
error.message_ = ToConstString(e.what());
return error;
}
Error ToError(const std::system_error& e)
{
return ToError(ToConstString(e.code().category().name()),
e.code().value(),
ToConstString(e.what()));

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Copy link
Collaborator

@boulderdaze boulderdaze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@mattldawson mattldawson merged commit c833bda into main Apr 26, 2024
40 checks passed
@mattldawson mattldawson deleted the develop-38-error-handling branch April 26, 2024 15:15
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add error handling for MUSICA interface
4 participants