use crate::callback::CallbackError;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("initialization failed: {0}")]
Initialization(String),
#[error("wasm engine error: {0}")]
WasmEngine(String),
#[error("wasm component error: {0}")]
WasmComponent(#[from] wasmtime::Error),
#[error("execution failed: {0}")]
Execution(String),
#[error("{0}")]
PythonException(String),
#[error("callback error: {0}")]
Callback(#[from] CallbackError),
#[error("resource limit exceeded: {0}")]
ResourceLimit(String),
#[error("execution timed out after {0:?}")]
Timeout(std::time::Duration),
#[error("execution ran out of fuel after {consumed} instructions (limit: {limit})")]
FuelExhausted {
consumed: u64,
limit: u64,
},
#[error("serialization error: {0}")]
Serialization(String),
#[error(
"Python stdlib not found. Set ERYX_PYTHON_STDLIB, use with_python_stdlib(), or use Sandbox::embedded()"
)]
MissingPythonStdlib,
#[error("snapshot error: {0}")]
Snapshot(String),
#[error("execution cancelled")]
Cancelled,
#[error("execution suspended: {0}")]
Suspended(String),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Self::Serialization(err.to_string())
}
}
pub(crate) fn validate_user_code(code: &str) -> Result<(), Error> {
if code.contains('\0') {
return Err(Error::Execution(
"code contains NUL bytes, which cannot be executed".to_string(),
));
}
Ok(())
}