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
use std::{
	error::Error,
	fmt::{self, Display},
};

use crate::AnyError;

/// Represents an error ocurring during script execution
#[derive(Debug)]
pub enum JsError {
	/// JSON errors stemming from arguments or return values
	Json(serde_json::Error),

	/// Runtime errors occuring within a JS script
	Runtime(AnyError),
}

impl Error for JsError {}

impl Display for JsError {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			JsError::Json(e) => write!(f, "{}", e),
			JsError::Runtime(e) => write!(f, "{}", e),
		}
	}
}

impl From<AnyError> for JsError {
	fn from(e: AnyError) -> JsError {
		JsError::Runtime(e)
	}
}

impl From<serde_json::Error> for JsError {
	fn from(e: serde_json::Error) -> JsError {
		JsError::Json(e)
	}
}