surrealdb_core/rpc/
rpc_error.rs

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
use thiserror::Error;

use crate::err;

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum RpcError {
	#[error("Parse error")]
	ParseError,
	#[error("Invalid request")]
	InvalidRequest,
	#[error("Method not found")]
	MethodNotFound,
	#[error("Method not allowed")]
	MethodNotAllowed,
	#[error("Invalid params")]
	InvalidParams,
	#[error("There was a problem with the database: {0}")]
	InternalError(err::Error),
	#[error("Live Query was made, but is not supported")]
	LqNotSuported,
	#[error("RT is enabled for the session, but LQ is not supported by the context")]
	BadLQConfig,
	#[error("A GraphQL request was made, but GraphQL is not supported by the context")]
	BadGQLConfig,
	#[error("Error: {0}")]
	Thrown(String),
}

impl From<err::Error> for RpcError {
	fn from(e: err::Error) -> Self {
		use err::Error;
		match e {
			Error::RealtimeDisabled => RpcError::LqNotSuported,
			_ => RpcError::InternalError(e),
		}
	}
}

impl From<&str> for RpcError {
	fn from(e: &str) -> Self {
		RpcError::Thrown(e.to_string())
	}
}

impl From<RpcError> for err::Error {
	fn from(value: RpcError) -> Self {
		use err::Error;
		match value {
			RpcError::InternalError(e) => e,
			RpcError::Thrown(e) => Error::Thrown(e),

			_ => Error::Thrown(value.to_string()),
		}
	}
}