Skip to content

refactor(core): Improve Error Message with Defective Value#6130

Merged
Xuanwo merged 16 commits intoapache:mainfrom
phreed:improve-error-message-patch
May 1, 2025
Merged

refactor(core): Improve Error Message with Defective Value#6130
Xuanwo merged 16 commits intoapache:mainfrom
phreed:improve-error-message-patch

Conversation

@phreed
Copy link
Copy Markdown
Contributor

@phreed phreed commented Apr 30, 2025

The expect message produced by this fragment is not ideal.

Here is an example of its presentation.
"input request url must be valid: InvalidIpv4Address"

Which issue does this PR close?

Closes #6123

Rationale for this change

It is difficult to correct a defective value if the value is unknown.
This change prints the defective value.

What changes are included in this PR?

Update to an error message to make correcting the error more tractable.

Are there any user-facing changes?

The message contains information about a defective uri.

The expect message produced here is not ideal.
Here is an example of its presentation.
"input request url must be valid: InvalidIpv4Address"
@phreed phreed requested a review from Xuanwo as a code owner April 30, 2025 21:45
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 30, 2025
phreed added 7 commits April 30, 2025 16:48
forgot that expect() takes a 'str'.
The line is too long.
Indentation is too far.
removed extra trailing blank
collapsed multiline into single line
collapse onto a single line
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Apr 30, 2025
It seems this specific compiler lint or rule in your project's setup is triggered by the pattern of unwrapping a Result using .expect() when the unwrapped value is then used in a way that is part of a method call chain elsewhere in the code.
reqwest::Url::from_str(&uri.to_string()).expect("input request url must be valid"),
)
.headers(parts.headers);
let url = reqwest::Url::from_str(&uri.to_string())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi, it's better to return an error instead. We can use something like:

let url = reqwest::Url::from_str(&uri.to_string()).map_err(|err| Error::new("request ur is invalid").context("uri", uri).source(err))?;

Copy link
Copy Markdown
Contributor Author

@phreed phreed May 1, 2025

Choose a reason for hiding this comment

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

I noticed there is a very similar piece of code at https://github.com/apache/opendal/blob/v0.53.1/core/src/raw/http_util/client.rs#L189-L195

let mut resp = req_builder.send().await.map_err(|err| {
    Error::new(ErrorKind::Unexpected, "send http request")
        .with_operation("http_util::Client::send")
        .with_context("url", uri.to_string())
        .with_temporary(is_temporary_error(&err))
        .set_source(err)
})?;

I am imitating this fragment but omitting the with_temporary().
Should they both be using with_operation("http_util::Client::send::fetch")? (instead of just http_util::Client::send)

Same point at https://github.com/apache/opendal/blob/v0.53.1/core/src/raw/http_util/client.rs#L228

phreed added 7 commits May 1, 2025 09:06
It has been suggested that it is better to return an error instead of a except() or panic!()
The Error constructor takes two arguments, the first is the ErrorKind
Create block for producing the Error.
remove extra white space
mimic the error produced on the send on lines 190-197
corrected operation message
The send function calls fetch.
@Xuanwo Xuanwo changed the title Improve Error Message with Defective Value refactor(core): Improve Error Message with Defective Value May 1, 2025
Copy link
Copy Markdown
Member

@Xuanwo Xuanwo left a comment

Choose a reason for hiding this comment

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

Thank you very much for this change!

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label May 1, 2025
@Xuanwo Xuanwo merged commit faf1298 into apache:main May 1, 2025
242 checks passed
@phreed phreed deleted the improve-error-message-patch branch May 13, 2025 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Error Message is Partially Helpful

2 participants