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

unexpected_http_status_code can panic b/c using unwrap #11

Closed
hseeberger opened this issue Feb 10, 2022 · 2 comments
Closed

unexpected_http_status_code can panic b/c using unwrap #11

hseeberger opened this issue Feb 10, 2022 · 2 comments
Assignees

Comments

@hseeberger
Copy link
Owner

response
    .text()
    .await
    .map_err(|e| format!("Failed to get response body as text: {e}"))
    .and_then(|text| {
        serde_json::from_str::<Value>(&text)
            .map(|v| v["error"]["message"].to_string())
            .map_err(|e| format!("Failed to parse error response: {e}"))
    })
    .unwrap(),

(https://github.com/hseeberger/pub-sub-client/blob/main/pub-sub-client/src/error/mod.rs#L41)

If either text().await or serde_json::from_str returns an Err, unwrap will panic.

@hseeberger hseeberger self-assigned this Feb 10, 2022
@hseeberger
Copy link
Owner Author

The above code should produce a String thereby never panicking. The map_err calls already convert any error into a String, therefore we should use .unwrap_or_else(identity) instead of .unwrap.

@hseeberger
Copy link
Owner Author

Closed via #12.

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

No branches or pull requests

1 participant