|
| 1 | +use std::fmt; |
1 | 2 | use std::time;
|
2 | 3 |
|
3 |
| -use hook_common::pgqueue; |
| 4 | +use hook_common::{pgqueue, webhook::WebhookJobError}; |
4 | 5 | use thiserror::Error;
|
5 | 6 |
|
6 |
| -/// Enumeration of errors related to webhook job processing in the WebhookWorker. |
| 7 | +/// Enumeration of error classes handled by `WebhookWorker`. |
7 | 8 | #[derive(Error, Debug)]
|
8 | 9 | pub enum WebhookError {
|
| 10 | + #[error(transparent)] |
| 11 | + Parse(#[from] WebhookParseError), |
| 12 | + #[error(transparent)] |
| 13 | + Request(#[from] WebhookRequestError), |
| 14 | +} |
| 15 | + |
| 16 | +/// Enumeration of parsing errors that can occur as `WebhookWorker` sets up a webhook. |
| 17 | +#[derive(Error, Debug)] |
| 18 | +pub enum WebhookParseError { |
9 | 19 | #[error("{0} is not a valid HttpMethod")]
|
10 | 20 | ParseHttpMethodError(String),
|
11 | 21 | #[error("error parsing webhook headers")]
|
12 | 22 | ParseHeadersError(http::Error),
|
13 | 23 | #[error("error parsing webhook url")]
|
14 | 24 | ParseUrlError(url::ParseError),
|
15 |
| - #[error("a webhook could not be delivered but it could be retried later: {error}")] |
| 25 | +} |
| 26 | + |
| 27 | +/// Enumeration of request errors that can occur as `WebhookWorker` sends a request. |
| 28 | +#[derive(Error, Debug)] |
| 29 | +pub enum WebhookRequestError { |
16 | 30 | RetryableRequestError {
|
17 | 31 | error: reqwest::Error,
|
| 32 | + response: Option<String>, |
18 | 33 | retry_after: Option<time::Duration>,
|
19 | 34 | },
|
20 |
| - #[error("a webhook could not be delivered and it cannot be retried further: {0}")] |
21 |
| - NonRetryableRetryableRequestError(reqwest::Error), |
| 35 | + NonRetryableRetryableRequestError { |
| 36 | + error: reqwest::Error, |
| 37 | + response: Option<String>, |
| 38 | + }, |
| 39 | +} |
| 40 | + |
| 41 | +/// Enumeration of errors that can occur while handling a `reqwest::Response`. |
| 42 | +/// Currently, not consumed anywhere. Grouped here to support a common error type for |
| 43 | +/// `utils::first_n_bytes_of_response`. |
| 44 | +#[derive(Error, Debug)] |
| 45 | +pub enum WebhookResponseError { |
| 46 | + #[error("failed to parse a response as UTF8")] |
| 47 | + ParseUTF8StringError(#[from] std::str::Utf8Error), |
| 48 | + #[error("error while iterating over response body chunks")] |
| 49 | + StreamIterationError(#[from] reqwest::Error), |
| 50 | + #[error("attempted to slice a chunk of length {0} with an out of bounds index of {1}")] |
| 51 | + ChunkOutOfBoundsError(usize, usize), |
| 52 | +} |
| 53 | + |
| 54 | +/// Implement display of `WebhookRequestError` by appending to the underlying `reqwest::Error` |
| 55 | +/// any response message if available. |
| 56 | +impl fmt::Display for WebhookRequestError { |
| 57 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 58 | + match self { |
| 59 | + WebhookRequestError::RetryableRequestError { |
| 60 | + error, response, .. |
| 61 | + } |
| 62 | + | WebhookRequestError::NonRetryableRetryableRequestError { error, response } => { |
| 63 | + let response_message = match response { |
| 64 | + Some(m) => m.to_string(), |
| 65 | + None => "No response from the server".to_string(), |
| 66 | + }; |
| 67 | + writeln!(f, "{}", error)?; |
| 68 | + write!(f, "{}", response_message)?; |
| 69 | + |
| 70 | + Ok(()) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// Implementation of `WebhookRequestError` designed to further describe the error. |
| 77 | +/// In particular, we pass some calls to underyling `reqwest::Error` to provide more details. |
| 78 | +impl WebhookRequestError { |
| 79 | + pub fn is_timeout(&self) -> bool { |
| 80 | + match self { |
| 81 | + WebhookRequestError::RetryableRequestError { error, .. } |
| 82 | + | WebhookRequestError::NonRetryableRetryableRequestError { error, .. } => { |
| 83 | + error.is_timeout() |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + pub fn is_status(&self) -> bool { |
| 89 | + match self { |
| 90 | + WebhookRequestError::RetryableRequestError { error, .. } |
| 91 | + | WebhookRequestError::NonRetryableRetryableRequestError { error, .. } => { |
| 92 | + error.is_status() |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + pub fn status(&self) -> Option<http::StatusCode> { |
| 98 | + match self { |
| 99 | + WebhookRequestError::RetryableRequestError { error, .. } |
| 100 | + | WebhookRequestError::NonRetryableRetryableRequestError { error, .. } => { |
| 101 | + error.status() |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl From<&WebhookRequestError> for WebhookJobError { |
| 108 | + fn from(error: &WebhookRequestError) -> Self { |
| 109 | + if error.is_timeout() { |
| 110 | + WebhookJobError::new_timeout(&error.to_string()) |
| 111 | + } else if error.is_status() { |
| 112 | + WebhookJobError::new_http_status( |
| 113 | + error.status().expect("status code is defined").into(), |
| 114 | + &error.to_string(), |
| 115 | + ) |
| 116 | + } else { |
| 117 | + // Catch all other errors as `app_metrics::ErrorType::Connection` errors. |
| 118 | + // Not all of `reqwest::Error` may strictly be connection errors, so our supported error types may need an extension |
| 119 | + // depending on how strict error reporting has to be. |
| 120 | + WebhookJobError::new_connection(&error.to_string()) |
| 121 | + } |
| 122 | + } |
22 | 123 | }
|
23 | 124 |
|
24 | 125 | /// Enumeration of errors related to initialization and consumption of webhook jobs.
|
|
0 commit comments