Skip to content

Conversation

rainerschoe
Copy link
Member

@rainerschoe rainerschoe commented Dec 6, 2024

  • remove some unwrap, panic and expect uses
  • Improve error messages
  • rework segment evaluation to return errors instead of panic
  • Add/rework testcases for some error scenarios

I experimented using anyhow to create error chains providing the user with more readable error messages.
For now I terminate anyhow usage in segment evaluation code and use the thiserror types for everything user-facing.
Feedback appreciated.

The behavior differs from the go implementation in strictness. I'd appreciate Input from @saikumar1607 on what is the intended behavior for error handling in entity evaluation. Is it fine to have the strict enforcement as implemented here?

@rainerschoe rainerschoe force-pushed the 2024-12-06_error_handling branch from 5d69ba2 to c9b346f Compare December 6, 2024 18:21
@rainerschoe rainerschoe requested a review from jgsogo December 6, 2024 18:27
.into_iter()
.find(|targeting_rule| targeting_rule_applies_to_entity(segments, targeting_rule, entity))

for targeting_rule in targeting_rules.into_iter(){
Copy link
Member Author

Choose a reason for hiding this comment

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

I failed to find a nice way of propagating errors out of iterator chains. So in many cases like this I needed to fall-back to explicit loops. Any ideas?

Copy link
Collaborator

Choose a reason for hiding this comment

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

if targeting_rule_applies_to_entity(segments, &targeting_rule, entity)
.map_err(
|e|{
// This terminates the use of anyhow in this module, converting all errors:
Copy link
Member Author

Choose a reason for hiding this comment

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

This terminates anyhow use, We might lose some information (stacktrace) here.

Copy link
Collaborator

@jgsogo jgsogo left a comment

Choose a reason for hiding this comment

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

I really think we want to avoid anyhow in library code. With thiserror we can propagate all the info to our users and they can use pattern matching to decide what to do with them.

url = "2.5.2"
http = "1.1.0"
thiserror = "2.0.4"
anyhow = "1.0.94"
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't want anyhow as a dependency for the library

Comment on lines +24 to +26
// For chaining errors creating useful error messages:
use anyhow::{Context, Result as AnyhowResult};
use anyhow::anyhow;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't really want anyhow in library code. It hides most of information about the actual error. Let's reserve anyhow for application.

}
crate::models::ValueKind::Boolean => {
Value::Boolean(model_value.0.as_bool().ok_or(Error::ProtocolError)?)
Value::Boolean(model_value.0.as_bool().ok_or(Error::ProtocolError("Expected Boolean".into()))?)
Copy link
Collaborator

@jgsogo jgsogo Dec 8, 2024

Choose a reason for hiding this comment

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

If we want to, later we can add more typed information (ErrorBooleanExpected, ErrorStringExpected) so the string message is just for humans... and we can also include what was the value we were trying to parse as boolean.

@rainerschoe rainerschoe force-pushed the 2024-12-06_error_handling branch from 4bee4ec to c15af55 Compare December 10, 2024 10:55
@jgsogo jgsogo merged commit d5311db into IBM:main Dec 10, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants