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

feat: add better error handling for middlewares #792

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,13 @@ async fn index(
return r;
}
Err(e) => {
let e = e.downcast_ref::<PyErr>().unwrap();
error!(
"Error while executing before middleware function for endpoint `{}`: {}",
req.uri().path(),
get_traceback(e.downcast_ref::<PyErr>().unwrap())
get_traceback(e)
);
return Response::internal_server_error(None);
return Response::internal_server_error(None, Some(get_traceback(e)));
}
};
}
Expand All @@ -423,7 +424,7 @@ async fn index(
get_traceback(&e)
);

Response::internal_server_error(None)
Response::internal_server_error(None, Some(get_traceback(&e)))
}
}
} else {
Expand All @@ -450,7 +451,10 @@ async fn index(
response = match execute_middleware_function(&response, &after_middleware).await {
Ok(MiddlewareReturn::Request(_)) => {
error!("After middleware returned a request");
return Response::internal_server_error(Some(&response.headers));
return Response::internal_server_error(
Some(&response.headers),
Some("After middleware returned a request".to_string()),
);
}
Ok(MiddlewareReturn::Response(r)) => {
let response = r;
Expand All @@ -459,12 +463,16 @@ async fn index(
response
}
Err(e) => {
let e = e.downcast_ref::<PyErr>().unwrap();
error!(
"Error while executing after middleware function for endpoint `{}`: {}",
req.uri().path(),
get_traceback(e.downcast_ref::<PyErr>().unwrap())
get_traceback(e)
);
return Response::internal_server_error(
Some(&response.headers),
Some(get_traceback(e)),
);
return Response::internal_server_error(Some(&response.headers));
}
};
}
Expand Down
9 changes: 7 additions & 2 deletions src/types/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,22 @@ impl Response {
}
}

pub fn internal_server_error(headers: Option<&Headers>) -> Self {
pub fn internal_server_error(headers: Option<&Headers>, description: Option<String>) -> Self {
let headers = match headers {
Some(headers) => headers.clone(),
None => Headers::new(None),
};

let description = match description {
Some(description) => description.into_bytes(),
None => "Internal server error".to_owned().into_bytes(),
};

Self {
status_code: 500,
response_type: "text".to_string(),
headers,
description: "Internal server error".to_owned().into_bytes(),
description,
file_path: None,
}
}
Expand Down
Loading