-
Notifications
You must be signed in to change notification settings - Fork 125
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
Force Lambda failure #558
Open
bnusunny
wants to merge
7
commits into
main
Choose a base branch
from
force_lambda_failure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−0
Open
Force Lambda failure #558
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
96e6f72
The implementation looks complete and well-structured. The changes ad…
bnusunny dfc55d1
fix: Resolve type inference errors in parse_status_codes tests
bnusunny 110b63b
docs: Add AWS_LWA_ERROR_STATUS_CODES configuration to README
bnusunny 30f9872
docs: Add documentation for AWS_LWA_ERROR_STATUS_CODES environment va…
bnusunny 3e6c7d0
Solve linting error
bnusunny 87b0b03
Solve the cargo warming
bnusunny 02ca950
test: Add integration test for HTTP error status codes handling
bnusunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
|
@@ -78,6 +78,7 @@ pub struct AdapterOptions { | |
pub compression: bool, | ||
pub invoke_mode: LambdaInvokeMode, | ||
pub authorization_source: Option<String>, | ||
pub error_status_codes: Option<Vec<u16>>, | ||
} | ||
|
||
impl Default for AdapterOptions { | ||
|
@@ -116,10 +117,33 @@ impl Default for AdapterOptions { | |
.as_str() | ||
.into(), | ||
authorization_source: env::var("AWS_LWA_AUTHORIZATION_SOURCE").ok(), | ||
error_status_codes: env::var("AWS_LWA_ERROR_STATUS_CODES") | ||
.ok() | ||
.map(|codes| parse_status_codes(&codes)), | ||
} | ||
} | ||
} | ||
|
||
fn parse_status_codes(input: &str) -> Vec<u16> { | ||
input | ||
.split(',') | ||
.flat_map(|part| { | ||
let part = part.trim(); | ||
if part.contains('-') { | ||
let range: Vec<&str> = part.split('-').collect(); | ||
if range.len() == 2 { | ||
if let (Ok(start), Ok(end)) = (range[0].parse::<u16>(), range[1].parse::<u16>()) { | ||
return (start..=end).collect::<Vec<_>>(); | ||
} | ||
} | ||
vec![] | ||
} else { | ||
part.parse::<u16>().map_or(vec![], |code| vec![code]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the above, suggest to add a warning. |
||
} | ||
}) | ||
.collect() | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct Adapter<C, B> { | ||
client: Arc<Client<C, B>>, | ||
|
@@ -134,6 +158,7 @@ pub struct Adapter<C, B> { | |
compression: bool, | ||
invoke_mode: LambdaInvokeMode, | ||
authorization_source: Option<String>, | ||
error_status_codes: Option<Vec<u16>>, | ||
} | ||
|
||
impl Adapter<HttpConnector, Body> { | ||
|
@@ -171,6 +196,7 @@ impl Adapter<HttpConnector, Body> { | |
compression: options.compression, | ||
invoke_mode: options.invoke_mode, | ||
authorization_source: options.authorization_source.clone(), | ||
error_status_codes: options.error_status_codes.clone(), | ||
} | ||
} | ||
} | ||
|
@@ -341,6 +367,17 @@ impl Adapter<HttpConnector, Body> { | |
|
||
let mut app_response = self.client.request(request).await?; | ||
|
||
// Check if status code should trigger an error | ||
if let Some(error_codes) = &self.error_status_codes { | ||
let status = app_response.status().as_u16(); | ||
if error_codes.contains(&status) { | ||
return Err(Error::from(format!( | ||
"Request failed with configured error status code: {}", | ||
status | ||
))); | ||
} | ||
} | ||
|
||
// remove "transfer-encoding" from the response to support "sam local start-api" | ||
app_response.headers_mut().remove("transfer-encoding"); | ||
|
||
|
@@ -373,6 +410,20 @@ mod tests { | |
use super::*; | ||
use httpmock::{Method::GET, MockServer}; | ||
|
||
#[test] | ||
fn test_parse_status_codes() { | ||
assert_eq!(parse_status_codes("500,502-504,422"), vec![500, 502, 503, 504, 422]); | ||
assert_eq!( | ||
parse_status_codes("500, 502-504, 422"), // with spaces | ||
vec![500, 502, 503, 504, 422] | ||
); | ||
assert_eq!(parse_status_codes("500"), vec![500]); | ||
assert_eq!(parse_status_codes("500-502"), vec![500, 501, 502]); | ||
assert_eq!(parse_status_codes("invalid"), Vec::<u16>::new()); | ||
assert_eq!(parse_status_codes("500-invalid"), Vec::<u16>::new()); | ||
assert_eq!(parse_status_codes(""), Vec::<u16>::new()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_status_200_is_ok() { | ||
// Start app server | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe better to print a warning if the env var format is incorrect.