(error: HttpError, status: StatusCode)
| 78 | /// simplicity. |
| 79 | #[allow(clippy::needless_pass_by_value)] |
| 80 | pub fn error_response(error: HttpError, status: StatusCode) -> Response { |
| 81 | let mut response = Response::new(Version::Http11, status); |
| 82 | |
| 83 | let error: &dyn Error = &error; |
| 84 | // Write the Display::display() output all errors (from top to root). |
| 85 | let error_messages = std::iter::successors(Some(error), |sub_error| { |
| 86 | // Dereference necessary to mitigate rustc compiler bug. |
| 87 | // See <https://github.com/rust-lang/rust/issues/141673> |
| 88 | (*sub_error).source() |
| 89 | }) |
| 90 | .map(|error| format!("{error}")) |
| 91 | .collect::<Vec<_>>(); |
| 92 | |
| 93 | info!("HTTP API error response: {}", error_messages.join(": ")); |
| 94 | |
| 95 | // TODO: Move `api` module from `vmm` to dedicated crate and use a common type definition |
| 96 | let json = serde_json::to_string(&error_messages).unwrap(); |
| 97 | |
| 98 | let body = Body::new(json); |
| 99 | response.set_body(body); |
| 100 | |
| 101 | response |
| 102 | } |
| 103 | |
| 104 | /// An HTTP endpoint handler interface |
| 105 | pub trait EndpointHandler { |
no test coverage detected