diff --git a/lambda-runtime-client/src/error.rs b/lambda-runtime-client/src/error.rs index 6dd5356e..25d12492 100644 --- a/lambda-runtime-client/src/error.rs +++ b/lambda-runtime-client/src/error.rs @@ -9,30 +9,30 @@ use hyper; use serde_derive::Serialize; use serde_json; -/// Error type description for the `ErrorResponse` event. This type should be returned -/// for errors that were handled by the function code or framework. -pub const ERROR_TYPE_HANDLED: &str = "Handled"; -/// Error type description for the `ErrorResponse` event. This type is used for unhandled, -/// unexpcted errors. -pub const ERROR_TYPE_UNHANDLED: &str = "Unhandled"; +/// Error type description for the `ErrorResponse` event. +#[derive(Serialize)] +pub enum ErrorType { + /// This type should be returned + /// for errors that were handled by the function code or framework. + Handled, + /// This type is used for unhandled, + /// unexpcted errors. + Unhandled, +} /// This object is used to generate requests to the Lambda Runtime APIs. /// It is used for both the error response APIs and fail init calls. /// custom error types should implement the `RuntimeError` trait and return /// this object to be compatible with the APIs. #[derive(Serialize)] +#[serde(rename_all="camelCase")] pub struct ErrorResponse { /// The error message generated by the application. - #[serde(rename = "errorMessage")] pub error_message: String, - /// The error type for Lambda. This can be `Handled` or `Unhandled`. - /// Developers can use the `ERROR_TYPE_HANDLED` and `ERROR_TYPE_UNHANDLED` - /// constants to populate this field. - #[serde(rename = "errorType")] - pub error_type: String, + /// The error type for Lambda. + pub error_type: ErrorType, /// The stack trace for the exception as vector of strings. In the framework, /// this value is automatically populated using the `backtrace` crate. - #[serde(rename = "stackTrace")] pub stack_trace: Option>, } @@ -45,10 +45,10 @@ impl ErrorResponse { /// /// # Return /// A populated `RuntimeError` object that can be used with the Lambda Runtime API. - pub fn handled(message: String) -> ErrorResponse { + pub fn handled(message: M) -> ErrorResponse where M: Into { ErrorResponse { - error_message: message, - error_type: String::from(ERROR_TYPE_HANDLED), + error_message: message.into(), + error_type: ErrorType::Handled, stack_trace: Option::default(), } } @@ -61,10 +61,10 @@ impl ErrorResponse { /// /// # Return /// A populated `RuntimeError` object that can be used with the Lambda Runtime API. - pub fn unhandled(message: String) -> ErrorResponse { + pub fn unhandled(message: M) -> ErrorResponse where M: Into { ErrorResponse { - error_message: message, - error_type: String::from(ERROR_TYPE_UNHANDLED), + error_message: message.into(), + error_type: ErrorType::Unhandled, stack_trace: Option::default(), } } @@ -182,3 +182,24 @@ impl RuntimeApiError for ApiError { err } } + +#[cfg(test)] +mod tests { + use super::ErrorResponse; + + #[test] + fn handled_error_response_serializes() { + assert_eq!( + serde_json::to_string(&ErrorResponse::handled("💀")).expect("failed to serialize ErrorResponse"), + r#"{"errorMessage":"💀","errorType":"Handled","stackTrace":null}"# + ) + } + + #[test] + fn unhandled_error_response_serializes() { + assert_eq!( + serde_json::to_string(&ErrorResponse::unhandled("💀")).expect("failed to serialize ErrorResponse"), + r#"{"errorMessage":"💀","errorType":"Unhandled","stackTrace":null}"# + ) + } +} \ No newline at end of file diff --git a/lambda-runtime/src/error.rs b/lambda-runtime/src/error.rs index 975d7919..3c6b0f76 100644 --- a/lambda-runtime/src/error.rs +++ b/lambda-runtime/src/error.rs @@ -70,7 +70,7 @@ impl error::RuntimeApiError for RuntimeError { let backtrace = format!("{:?}", self.stack_trace); error::ErrorResponse { error_message: String::from(self.description()), - error_type: String::from(error::ERROR_TYPE_HANDLED), + error_type: error::ErrorType::Handled, stack_trace: Option::from(backtrace.lines().map(|s| s.to_string()).collect::>()), } } @@ -173,7 +173,7 @@ impl error::RuntimeApiError for HandlerError { let backtrace = format!("{:?}", self.backtrace); error::ErrorResponse { error_message: String::from(self.description()), - error_type: String::from(error::ERROR_TYPE_HANDLED), + error_type: error::ErrorType::Handled, stack_trace: Option::from(backtrace.lines().map(|s| s.to_string()).collect::>()), } }