Skip to content

Commit f56c7dc

Browse files
authored
Improve extensions API support (#593)
* Improve extensions API support - Set request content-types for JSON payloads - Print the returned status codes in case of errors Signed-off-by: David Calavera <david.calavera@gmail.com> * Fix clippy warnings introduced in 1.67 Signed-off-by: David Calavera <david.calavera@gmail.com> --------- Signed-off-by: David Calavera <david.calavera@gmail.com>
1 parent cccce9d commit f56c7dc

File tree

8 files changed

+38
-32
lines changed

8 files changed

+38
-32
lines changed

lambda-extension/src/extension.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ where
253253
)?;
254254
let res = client.call(req).await?;
255255
if res.status() != http::StatusCode::OK {
256-
return Err(ExtensionError::boxed("unable to initialize the logs api"));
256+
let err = format!("unable to initialize the logs api: {}", res.status());
257+
return Err(ExtensionError::boxed(err));
257258
}
258259
trace!("Registered extension with Logs API");
259260
}
@@ -288,7 +289,8 @@ where
288289
)?;
289290
let res = client.call(req).await?;
290291
if res.status() != http::StatusCode::OK {
291-
return Err(ExtensionError::boxed("unable to initialize the telemetry api"));
292+
let err = format!("unable to initialize the telemetry api: {}", res.status());
293+
return Err(ExtensionError::boxed(err));
292294
}
293295
trace!("Registered extension with Telemetry API");
294296
}
@@ -317,30 +319,30 @@ where
317319

318320
let ep = match ep.ready().await {
319321
Ok(ep) => ep,
320-
Err(error) => {
321-
println!("Inner service is not ready: {:?}", error);
322+
Err(err) => {
323+
println!("Inner service is not ready: {err:?}");
322324
let req = if is_invoke {
323-
requests::init_error(extension_id, &error.to_string(), None)?
325+
requests::init_error(extension_id, &err.to_string(), None)?
324326
} else {
325-
requests::exit_error(extension_id, &error.to_string(), None)?
327+
requests::exit_error(extension_id, &err.to_string(), None)?
326328
};
327329

328330
client.call(req).await?;
329-
return Err(error.into());
331+
return Err(err.into());
330332
}
331333
};
332334

333335
let res = ep.call(event).await;
334-
if let Err(error) = res {
335-
println!("{:?}", error);
336+
if let Err(err) = res {
337+
println!("{err:?}");
336338
let req = if is_invoke {
337-
requests::init_error(extension_id, &error.to_string(), None)?
339+
requests::init_error(extension_id, &err.to_string(), None)?
338340
} else {
339-
requests::exit_error(extension_id, &error.to_string(), None)?
341+
requests::exit_error(extension_id, &err.to_string(), None)?
340342
};
341343

342344
client.call(req).await?;
343-
return Err(error.into());
345+
return Err(err.into());
344346
}
345347
}
346348
Ok(())
@@ -422,7 +424,8 @@ async fn register<'a>(
422424
let req = requests::register_request(&name, events)?;
423425
let res = client.call(req).await?;
424426
if res.status() != http::StatusCode::OK {
425-
return Err(ExtensionError::boxed("unable to register the extension"));
427+
let err = format!("unable to register the extension: {}", res.status());
428+
return Err(ExtensionError::boxed(err));
426429
}
427430

428431
let header = res

lambda-extension/src/logs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ where
174174
let mut service = service.lock().await;
175175
match service.call(logs).await {
176176
Ok(_) => (),
177-
Err(err) => println!("{:?}", err),
177+
Err(err) => println!("{err:?}"),
178178
}
179179
}
180180

lambda-extension/src/requests.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use serde::Serialize;
77
const EXTENSION_NAME_HEADER: &str = "Lambda-Extension-Name";
88
pub(crate) const EXTENSION_ID_HEADER: &str = "Lambda-Extension-Identifier";
99
const EXTENSION_ERROR_TYPE_HEADER: &str = "Lambda-Extension-Function-Error-Type";
10+
const CONTENT_TYPE_HEADER_NAME: &str = "Content-Type";
11+
const CONTENT_TYPE_HEADER_VALUE: &str = "application/json";
1012

1113
pub(crate) fn next_event_request(extension_id: &str) -> Result<Request<Body>, Error> {
1214
let req = build_request()
@@ -24,6 +26,7 @@ pub(crate) fn register_request(extension_name: &str, events: &[&str]) -> Result<
2426
.method(Method::POST)
2527
.uri("/2020-01-01/extension/register")
2628
.header(EXTENSION_NAME_HEADER, extension_name)
29+
.header(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE)
2730
.body(Body::from(serde_json::to_string(&events)?))?;
2831

2932
Ok(req)
@@ -65,14 +68,15 @@ pub(crate) fn subscribe_request(
6568
"buffering": buffering.unwrap_or_default(),
6669
"destination": {
6770
"protocol": "HTTP",
68-
"URI": format!("http://sandbox.localdomain:{}", port_number),
71+
"URI": format!("http://sandbox.localdomain:{port_number}"),
6972
}
7073
});
7174

7275
let req = build_request()
7376
.method(Method::PUT)
7477
.uri(api.uri())
7578
.header(EXTENSION_ID_HEADER, extension_id)
79+
.header(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE)
7680
.body(Body::from(serde_json::to_string(&data)?))?;
7781

7882
Ok(req)
@@ -91,30 +95,30 @@ pub struct ErrorRequest<'a> {
9195
}
9296

9397
/// Create a new init error request to send to the Extensions API
94-
pub fn init_error<'a>(
98+
pub fn init_error(
9599
extension_id: &str,
96100
error_type: &str,
97-
request: Option<ErrorRequest<'a>>,
101+
request: Option<ErrorRequest<'_>>,
98102
) -> Result<Request<Body>, Error> {
99103
error_request("init", extension_id, error_type, request)
100104
}
101105

102106
/// Create a new exit error request to send to the Extensions API
103-
pub fn exit_error<'a>(
107+
pub fn exit_error(
104108
extension_id: &str,
105109
error_type: &str,
106-
request: Option<ErrorRequest<'a>>,
110+
request: Option<ErrorRequest<'_>>,
107111
) -> Result<Request<Body>, Error> {
108112
error_request("exit", extension_id, error_type, request)
109113
}
110114

111-
fn error_request<'a>(
115+
fn error_request(
112116
error_type: &str,
113117
extension_id: &str,
114118
error_str: &str,
115-
request: Option<ErrorRequest<'a>>,
119+
request: Option<ErrorRequest<'_>>,
116120
) -> Result<Request<Body>, Error> {
117-
let uri = format!("/2020-01-01/extension/{}/error", error_type);
121+
let uri = format!("/2020-01-01/extension/{error_type}/error");
118122

119123
let body = match request {
120124
None => Body::empty(),

lambda-extension/src/telemetry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ where
291291
let mut service = service.lock().await;
292292
match service.call(telemetry).await {
293293
Ok(_) => (),
294-
Err(err) => println!("{:?}", err),
294+
Err(err) => println!("{err:?}"),
295295
}
296296
}
297297

lambda-http/src/ext.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ pub enum PayloadError {
3737
impl fmt::Display for PayloadError {
3838
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3939
match self {
40-
PayloadError::Json(json) => writeln!(f, "failed to parse payload from application/json {}", json),
40+
PayloadError::Json(json) => writeln!(f, "failed to parse payload from application/json {json}"),
4141
PayloadError::WwwFormUrlEncoded(form) => writeln!(
4242
f,
43-
"failed to parse payload from application/x-www-form-urlencoded {}",
44-
form
43+
"failed to parse payload from application/x-www-form-urlencoded {form}"
4544
),
4645
}
4746
}

lambda-http/src/request.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ fn apigw_path_with_stage(stage: &Option<String>, path: &str) -> String {
324324
match stage {
325325
None => path.into(),
326326
Some(stage) if stage == "$default" => path.into(),
327-
Some(stage) => format!("/{}{}", stage, path),
327+
Some(stage) => format!("/{stage}{path}"),
328328
}
329329
}
330330

@@ -418,15 +418,15 @@ fn build_request_uri(
418418
) -> String {
419419
let mut url = match host {
420420
None => {
421-
let rel_url = Url::parse(&format!("http://localhost{}", path)).unwrap();
421+
let rel_url = Url::parse(&format!("http://localhost{path}")).unwrap();
422422
rel_url.path().to_string()
423423
}
424424
Some(host) => {
425425
let scheme = headers
426426
.get(x_forwarded_proto())
427427
.and_then(|s| s.to_str().ok())
428428
.unwrap_or("https");
429-
let url = format!("{}://{}{}", scheme, host, path);
429+
let url = format!("{scheme}://{host}{path}");
430430
Url::parse(&url).unwrap().to_string()
431431
}
432432
};

lambda-runtime-api-client/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ where
6666
(scheme, authority, base_path)
6767
};
6868
let path = parts.uri.path_and_query().expect("PathAndQuery not found");
69-
let pq: PathAndQuery = format!("{}{}", base_path, path).parse().expect("PathAndQuery invalid");
69+
let pq: PathAndQuery = format!("{base_path}{path}").parse().expect("PathAndQuery invalid");
7070

7171
let uri = Uri::builder()
7272
.scheme(scheme.as_ref())

lambda-runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ where
183183
error!("{:?}", err);
184184
let error_type = type_name_of_val(&err);
185185
let msg = if let Some(msg) = err.downcast_ref::<&str>() {
186-
format!("Lambda panicked: {}", msg)
186+
format!("Lambda panicked: {msg}")
187187
} else {
188188
"Lambda panicked".to_string()
189189
};
@@ -268,7 +268,7 @@ where
268268
{
269269
error!("{:?}", err); // logs the error in CloudWatch
270270
let error_type = type_name_of_val(&err);
271-
let msg = format!("{}", err);
271+
let msg = format!("{err}");
272272

273273
EventErrorRequest::new(request_id, error_type, &msg).into_req()
274274
}

0 commit comments

Comments
 (0)