use std::{env, error::Error, fmt};
use lambda_runtime_client::error::ApiError;
use lambda_runtime_errors::LambdaErrorExt;
#[derive(Debug, Clone)]
pub struct RuntimeError {
msg: String,
pub(crate) request_id: Option<String>,
pub(crate) recoverable: bool,
}
impl RuntimeError {
pub(crate) fn unrecoverable(msg: &str) -> RuntimeError {
let mut new_error = RuntimeError::new(msg);
new_error.recoverable = false;
new_error
}
pub(crate) fn new(msg: &str) -> RuntimeError {
RuntimeError {
msg: String::from(msg),
recoverable: true,
request_id: None,
}
}
}
impl LambdaErrorExt for RuntimeError {
fn error_type(&self) -> &str {
if self.recoverable {
"RecoverableRuntimeError"
} else {
"UnrecoverableRuntimeError"
}
}
}
impl fmt::Display for RuntimeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
impl Error for RuntimeError {
fn description(&self) -> &str {
&self.msg
}
fn cause(&self) -> Option<&Error> {
None
}
}
impl From<env::VarError> for RuntimeError {
fn from(e: env::VarError) -> Self {
RuntimeError::unrecoverable(e.description())
}
}
impl From<ApiError> for RuntimeError {
fn from(e: ApiError) -> Self {
let mut err = RuntimeError::new(&format!("{}", e));
err.recoverable = e.is_recoverable();
err
}
}