This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Track response in error details (#29)
Co-authored-by: Brett Hoerner <[email protected]>
- Loading branch information
1 parent
c6d5c67
commit f7e02cc
Showing
6 changed files
with
270 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod config; | ||
pub mod error; | ||
pub mod util; | ||
pub mod worker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::error::WebhookResponseError; | ||
use futures::StreamExt; | ||
use reqwest::Response; | ||
|
||
pub async fn first_n_bytes_of_response( | ||
response: Response, | ||
n: usize, | ||
) -> Result<String, WebhookResponseError> { | ||
let mut body = response.bytes_stream(); | ||
let mut buffer = String::with_capacity(n); | ||
|
||
while let Some(chunk) = body.next().await { | ||
if buffer.len() >= n { | ||
break; | ||
} | ||
|
||
let chunk = chunk?; | ||
let chunk_str = std::str::from_utf8(&chunk)?; | ||
let upper_bound = std::cmp::min(n - buffer.len(), chunk_str.len()); | ||
|
||
if let Some(partial_chunk_str) = chunk_str.get(0..upper_bound) { | ||
buffer.push_str(partial_chunk_str); | ||
} else { | ||
// For whatever reason we are out of bounds. We should never land here | ||
// given the `std::cmp::min` usage, but I am being extra careful by not | ||
// using a slice index that would panic instead. | ||
return Err(WebhookResponseError::ChunkOutOfBoundsError( | ||
chunk_str.len(), | ||
upper_bound, | ||
)); | ||
} | ||
} | ||
|
||
Ok(buffer) | ||
} |
Oops, something went wrong.