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

Get rid of unwrap #246

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Get rid of unwrap #246

wants to merge 1 commit into from

Conversation

Filip-L
Copy link
Collaborator

@Filip-L Filip-L commented Nov 27, 2024

No description provided.

Comment on lines +50 to +54
id: get_string_field(&app, "id").expect("ID must exist"),
owner: get_string_field(&app, "owner").expect("Owner must exist"),
repo: get_string_field(&app, "repo").expect("Repo must exist"),
pr_number: get_i64_field(&app, "pr_number").expect("PR number must exist"),
issue_number: get_i64_field(&app, "issue_number").expect("Issue number must exist"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any way to trigger a scenario where any of these would fail?

}
let applications = app_data
.into_iter()
.map(|app| ApplicationModel {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Long-term (not this PR) it would be better to replace this with https://docs.rs/serde_json/latest/serde_json/fn.from_value.html

@@ -38,8 +38,10 @@ pub async fn allocators() -> impl Responder {
pub async fn create_allocator_from_json(files: web::Json<ChangedAllocators>) -> impl Responder {
let ChangedAllocators { files_changed } = files.into_inner();
match create_allocator_from_file(files_changed).await {
Ok(()) => HttpResponse::Ok()
.body(serde_json::to_string_pretty("All files processed successfully").unwrap()),
Ok(_) => match serde_json::to_string_pretty("All files processed successfully") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

to_string_pretty won't fail if passed a static string. It's a good candidate for .expect.

Ok(app_file) => HttpResponse::Ok().body(serde_json::to_string_pretty(&app_file).unwrap()),
Ok(app_file) => match serde_json::to_string_pretty(&app_file) {
Ok(response) => HttpResponse::Ok().body(response),
Err(_) => HttpResponse::InternalServerError().body("Failed to parse success message"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Err(_) => HttpResponse::InternalServerError().body("Failed to parse success message"),
Err(_) => HttpResponse::InternalServerError().body("Failed to serialize success message"),

Copy link
Collaborator

Choose a reason for hiding this comment

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

Applies to all cases where it says parse instead of serialize when refering to to_string_pretty.

@@ -170,7 +182,10 @@ pub async fn propose_storage_providers(
)
.await
{
Ok(_) => HttpResponse::Ok().body(serde_json::to_string_pretty("Success").unwrap()),
Ok(_) => match serde_json::to_string_pretty("Success") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

to_string_pretty won't fail if passed a static string. It's a good candidate for .expect.

@@ -523,8 +560,10 @@ pub async fn submit_kyc(info: web::Json<SubmitKYCInfo>) -> impl Responder {
Err(e) => return HttpResponse::BadRequest().body(e.to_string()),
};
match ldn_application.submit_kyc(&info.into_inner()).await {
Ok(()) => HttpResponse::Ok()
.body(serde_json::to_string_pretty("Address verified with score").unwrap()),
Ok(_) => match serde_json::to_string_pretty("Address verified with score") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

to_string_pretty won't fail if passed a static string. It's a good candidate for .expect.

@@ -546,7 +585,10 @@ pub async fn request_kyc(query: web::Query<VerifierActionsQueryParams>) -> impl
.request_kyc(&query.id, &query.owner, &query.repo)
.await
{
Ok(()) => HttpResponse::Ok().body(serde_json::to_string_pretty("Success").unwrap()),
Ok(_) => match serde_json::to_string_pretty("Success") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

to_string_pretty won't fail if passed a static string. It's a good candidate for .expect (won't repeat for any others, please review all these cases).

e
))
})?;
if let Some(allocation_amounts) = allocation_amount.quantity_options {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if allocation_amount.quantity_options is None? Is it acceptable to ignore or should we return an error? Previously it would panic, which suggests we should return an error.

let body = hyper::body::to_bytes(response).await.unwrap();
let body = hyper::body::to_bytes(response)
.await
.map_err(|e| LDNError::Load(format!("Failed to parse to bytes: {}", e)))?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.map_err(|e| LDNError::Load(format!("Failed to parse to bytes: {}", e)))?;
.map_err(|e| LDNError::Load(format!("Failed to serialize to bytes: {}", e)))?;

let last_commit: &CommitData = commits.last().unwrap();
let body = hyper::body::to_bytes(response_body)
.await
.map_err(|e| LDNError::Load(format!("Failed to parse to bytes: {}", e)))?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.map_err(|e| LDNError::Load(format!("Failed to parse to bytes: {}", e)))?;
.map_err(|e| LDNError::Load(format!("Failed to serialize to bytes: {}", e)))?;

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