Skip to content

Commit

Permalink
add headers for errors and set retry after error data correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin67 committed Oct 9, 2024
1 parent 590129b commit a00987c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion inngest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::handler::Kind;
pub(crate) const CONTENT_TYPE: &str = "content-type";
pub(crate) const RETRY_AFTER: &str = "retry-after";
pub(crate) const SERVER_TIMING: &str = "server-timing";
pub(crate) const USE_AGENT: &str = "user-agent";
pub(crate) const USER_AGENT: &str = "user-agent";

// Inngest specific ones
pub(crate) const INNGEST_ENV: &str = "x-inngest-env";
Expand Down
58 changes: 47 additions & 11 deletions inngest/src/result.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::fmt::{Debug, Display};
use std::{
fmt::{Debug, Display},
time::Duration,
};

use axum::{
http::{HeaderMap, HeaderValue, StatusCode},
Expand Down Expand Up @@ -142,24 +145,53 @@ impl Drop for FlowControlError {

impl IntoResponse for Error {
fn into_response(self) -> axum::response::Response {
let mut headers = HeaderMap::new();
let sdk = format!("rust:{}", env!("CARGO_PKG_VERSION"));
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),
);
// TODO: framework might need to change
headers.insert(header::INNGEST_FRAMEWORK, HeaderValue::from_static("axum"));
headers.insert(header::INNGEST_SDK, HeaderValue::from_str(&sdk).unwrap());
headers.insert(header::INNGEST_REQ_VERSION, HeaderValue::from_static("1"));

match self {
Error::Dev(err) => match err {
DevError::Basic(msg) => (StatusCode::INTERNAL_SERVER_ERROR, Json(json!(msg))),
DevError::RetryAt(_err) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!("retry after error")),
),
DevError::Basic(msg) => {
(StatusCode::INTERNAL_SERVER_ERROR, headers, Json(json!(msg)))
}
DevError::RetryAt(retry) => {
headers.insert(
header::RETRY_AFTER,
HeaderValue::from(retry.after.as_secs()),
);

(
StatusCode::INTERNAL_SERVER_ERROR,
headers,
Json(json!(StepError {
name: "RetryAfterError".to_string(),
message: retry.message,
stack: retry.cause,
data: None,
})),
)
}
DevError::NoRetry(_err) => (
StatusCode::INTERNAL_SERVER_ERROR,
headers,
Json(json!("no retry error")),
),
},
Error::NoInvokeFunctionResponseError => (
StatusCode::INTERNAL_SERVER_ERROR,
headers,
Json(json!("No invoke response")),
),
_ => (
StatusCode::INTERNAL_SERVER_ERROR,
headers,
Json(json!("NOT IMPLEMENTED")),
),
}
Expand All @@ -173,21 +205,23 @@ pub struct StepError {
pub message: String,
pub stack: Option<String>,
// not part of the spec but it's used in the Go SDK to deserialize into the original user error
#[serde(skip_serializing)]
pub data: Option<serde_json::Value>,
}

pub struct RetryAfterError {
pub message: String,
pub retry_after: i64,
pub after: Duration,
pub cause: Option<String>,
}

impl Display for RetryAfterError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Error: {}, retrying after timestamp: {}",
&self.message, &self.retry_after
"Error: {}, retrying after {}s",
&self.message,
self.after.as_secs()
)
}
}
Expand All @@ -201,8 +235,10 @@ impl Debug for RetryAfterError {

write!(
f,
"Error: {}\nRetrying after timestamp: {}\nCause: {}",
&self.message, &self.retry_after, &cause
"Error: {}\nRetrying after {}s:\nCause: {}",
&self.message,
self.after.as_secs(),
&cause
)
}
}
Expand Down

0 comments on commit a00987c

Please sign in to comment.