Skip to content
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

SDK-2352: Enhanced error reason #421

Merged
merged 16 commits into from
Oct 31, 2023
Merged

SDK-2352: Enhanced error reason #421

merged 16 commits into from
Oct 31, 2023

Conversation

irotech
Copy link
Collaborator

@irotech irotech commented Oct 24, 2023

No description provided.

@irotech irotech force-pushed the SDK-2352_error_reason branch from 8cd75a9 to be5b151 Compare October 24, 2023 10:25
@irotech irotech force-pushed the SDK-2352_error_reason branch 4 times, most recently from 67e12c1 to d5d68fc Compare October 26, 2023 16:38
@irotech irotech requested a review from bentaye October 27, 2023 14:34
Comment on lines 26 to 30
String.format("Error[code='%s' description='%s' reason='%s']",
e.getCode(),
e.getDescription(),
e.getReason()
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like what the implementation fo toString() should look like... as in:

Optional.ofNullable(profile.getError())
    .map(ErrorDetails::toString)
    .orElse("")

@JsonProperty(Property.REASON)
public Builder reason(Map<String, Object> reason) {
try {
this.reason = new ObjectMapper().writeValueAsString(reason);
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need to create an ObjectMapper instance every time we de-serialize a JSON. Make it a constant (maybe even a "global" constant if it's important to enforce the same config wherever it is used).

try {
this.reason = new ObjectMapper().writeValueAsString(reason);
} catch (JsonProcessingException e) {
throw new RuntimeException("The reason of the failed share has an unexpected format");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Usually an exception message will say:

  • What failed
  • Why it failed
  • (optional) what it would take to not fail

So something like:

String.format("Failed to parse failure receipt error reason: '%s'", e.getMessage())

Additionally it's not a very good practice to swallow exceptions like this. Either pass the exception as a cause or include the message of the exception when creating the message for the exception that will be (re)thrown.
(a third option would be to log the stack trace and not propagate the cause, however it would require a logging framework which I don't know if this has)

When it comes to including the message it might also be useful to include every message along the exception chain (in case there's more than one cause) along the lines of:

<ex msg>: <msg of cause of ex>: <msg of cause of cause of ex>

(this comment applies elsewhere where the same pattern exists)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I ended up creating an exception for it

@@ -65,10 +67,18 @@ public UserContent getUserContent() {
return userContent;
}

public boolean hasError() {
return getError().isPresent();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Avoid calling methods within a class that will only access member variables. Whenever you need to use a member var just use the member var:

return error != null

@irotech irotech force-pushed the SDK-2352_error_reason branch from 8367d11 to abe861e Compare October 30, 2023 16:26
@irotech irotech force-pushed the SDK-2352_error_reason branch from abe861e to e5a5b32 Compare October 30, 2023 16:34
@irotech irotech requested a review from bentaye October 30, 2023 16:38
@irotech irotech requested a review from pn-santos October 30, 2023 16:38
@irotech irotech force-pushed the SDK-2352_error_reason branch from e5a5b32 to 8cce312 Compare October 30, 2023 16:54
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

34.4% 34.4% Coverage
0.0% 0.0% Duplication

@irotech irotech merged commit a15beab into DEVELOPMENT Oct 31, 2023
9 checks passed
@irotech irotech deleted the SDK-2352_error_reason branch October 31, 2023 09:00
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.

3 participants