Commit

Author:

Hash:

Timestamp:

+3 -27 +/-1 browse

Kevin Schoon [me@kevinschoon.com]

4c27fb6aa6143352a101e89bb1690c1ea4fe7196

Wed, 10 Apr 2024 20:16:48 +0000 (1.1 years ago)

use thiserror in web2/error.rs
1diff --git a/ayllu/src/web2/error.rs b/ayllu/src/web2/error.rs
2index 1f7d718..14b3954 100644
3--- a/ayllu/src/web2/error.rs
4+++ b/ayllu/src/web2/error.rs
5 @@ -1,5 +1,3 @@
6- use std::error::Error as StdError;
7- use std::fmt::Display;
8 use std::io::Error as IoError;
9
10 use axum::{body::Body, response::IntoResponse, response::Response};
11 @@ -12,9 +10,11 @@ use ayllu_rpc::tarpc::client::RpcError;
12
13 /// Error maps known error types into errors that can be translated into HTTP
14 /// status codes, e.g. Io::NotFound -> 404
15- #[derive(Debug, Clone)]
16+ #[derive(thiserror::Error, Debug, Clone)]
17 pub enum Error {
18+ #[error("Generic failure: {0}")]
19 Message(String),
20+ #[error("Resource not found: {0}")]
21 NotFound(String),
22 }
23
24 @@ -27,12 +27,6 @@ impl IntoResponse for Error {
25 }
26 }
27
28- impl From<Box<dyn StdError>> for Error {
29- fn from(value: Box<dyn StdError>) -> Self {
30- Error::Message(value.to_string())
31- }
32- }
33-
34 impl From<ApiError> for Error {
35 fn from(value: ApiError) -> Self {
36 Error::Message(format!("RPC [ayllu]: {:?}", value))
37 @@ -75,21 +69,3 @@ impl From<SqlError> for Error {
38 Error::Message(format!("SQL: {:?}", value))
39 }
40 }
41-
42- impl StdError for Error {
43- fn source(&self) -> Option<&(dyn StdError + 'static)> {
44- match self {
45- Error::Message(_) => None,
46- Error::NotFound(_) => None,
47- }
48- }
49- }
50-
51- impl Display for Error {
52- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53- match self {
54- Error::Message(message) => write!(f, "{}", message),
55- Error::NotFound(message) => write!(f, "{}", message),
56- }
57- }
58- }