thruster_grpc/
error.rs

1use thruster::errors::ThrusterError as Error;
2
3use crate::context::ProtoContext as Ctx;
4use crate::status::ProtoStatus;
5
6pub trait ProtoErrorSet<T> {
7    fn already_exists_error(context: Ctx<T>) -> Error<Ctx<T>>;
8    fn parsing_error(context: Ctx<T>, error: &str) -> Error<Ctx<T>>;
9    fn generic_error(context: Ctx<T>) -> Error<Ctx<T>>;
10    fn unauthorized_error(context: Ctx<T>) -> Error<Ctx<T>>;
11    fn not_found_error(context: Ctx<T>) -> Error<Ctx<T>>;
12}
13
14impl<T> ProtoErrorSet<T> for Error<Ctx<T>> {
15    fn already_exists_error(mut context: Ctx<T>) -> Error<Ctx<T>> {
16        context.status(409);
17        context.set_proto_status(ProtoStatus::AlreadyExists as u16);
18        Error {
19            context,
20            message: format!("Already exists"),
21            cause: None,
22        }
23    }
24
25    fn parsing_error(mut context: Ctx<T>, error: &str) -> Error<Ctx<T>> {
26        context.status(400);
27        context.set_proto_status(ProtoStatus::InvalidArgument as u16);
28        Error {
29            context,
30            message: format!("Failed to parse '{}'", error),
31            cause: None,
32        }
33    }
34
35    fn generic_error(mut context: Ctx<T>) -> Error<Ctx<T>> {
36        context.status(400);
37        context.set_proto_status(ProtoStatus::InvalidArgument as u16);
38        Error {
39            context,
40            message: "Something didn't work!".to_string(),
41            cause: None,
42        }
43    }
44
45    fn unauthorized_error(mut context: Ctx<T>) -> Error<Ctx<T>> {
46        context.status(401);
47        context.set_proto_status(ProtoStatus::Unauthenticated as u16);
48        Error {
49            context,
50            message: "Unauthorized".to_string(),
51            cause: None,
52        }
53    }
54
55    fn not_found_error(mut context: Ctx<T>) -> Error<Ctx<T>> {
56        context.status(404);
57        context.set_proto_status(ProtoStatus::NotFound as u16);
58        Error {
59            context,
60            message: "Not found".to_string(),
61            cause: None,
62        }
63    }
64}