Repository navigation
Expand file tree
/
Copy patherror.rs
More file actions
98 lines (88 loc) · 2.94 KB
/
Copy patherror.rs
File metadata and controls
98 lines (88 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! The error module defines the error types that can be returned
//! by custom handlers as well as the runtime itself.
use std::{env, error::Error, fmt};
use lambda_runtime_client::error::ApiError;
use lambda_runtime_errors::LambdaErrorExt;
/// The `RuntimeError` object is returned by the custom runtime as it polls
/// for new events and tries to execute the handler function. The error
/// is primarily used by other methods within this crate and should not be relevant
/// to developers building Lambda functions. Handlers are expected to return
/// the `HandlerError` defined in this module.
#[derive(Debug, Clone)]
pub struct RuntimeError {
msg: String,
/// The request id that generated this error
pub(crate) request_id: Option<String>,
/// Whether the error is recoverable or not.
pub(crate) recoverable: bool,
}
impl RuntimeError {
/// Creates a new `RuntimeError` that is unrecoverable and it will cause the
/// runtime to panic in order to force a restart of the execution environment.
/// When a new `RuntimeError` is created the stack trace for the error is collected
/// automatically using the `backtrace` crate.
///
/// # Arguments
///
/// * `msg` The error message to be attached to the error.
///
/// # Returns
/// A new `RuntimeError` instance with the `recoverable` property set to `false`.
pub(crate) fn unrecoverable(msg: &str) -> RuntimeError {
let mut new_error = RuntimeError::new(msg);
new_error.recoverable = false;
new_error
}
/// Creates a new `RuntimeError` with the given properties. The stack trace for the
/// error is collected automatically using the `backtrace` crate.
///
/// # Arguments
///
/// * `msg` The error message
///
/// # Returns
/// A new `RuntimeError` instance.
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)
}
}
// This is important for other errors to wrap this one.
impl Error for RuntimeError {
fn description(&self) -> &str {
&self.msg
}
fn cause(&self) -> Option<&Error> {
// Generic error, underlying cause isn't tracked.
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
}
}