File size: 1,168 Bytes
ec1e963
 
 
 
 
 
 
 
37dc643
 
ec1e963
 
 
37dc643
 
 
 
 
 
 
 
 
 
 
ec1e963
 
 
 
 
 
37dc643
 
 
 
 
 
 
 
 
 
aab6ff5
ec1e963
 
37dc643
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
use actix_web::{HttpResponse, ResponseError};
use derive_more::{Display, Error};

#[derive(Debug, Display, Error)]
pub enum MyError {
    #[display(fmt = "Data tidak ditemukan")]
    NotFound,

    #[display(fmt = "Supabase error: {}", _0)]
    Supabase(String),
}

impl From<reqwest::Error> for MyError {
    fn from(err: reqwest::Error) -> Self {
        println!("\n================ ERROR REQWEST ================");
        println!("{:#?}", err);

        if let Some(status) = err.status() {
            println!("STATUS: {}", status);
        }

        println!("================================================\n");

        MyError::Supabase(err.to_string())
    }
}

impl ResponseError for MyError {
    fn error_response(&self) -> HttpResponse {
        match self {
            MyError::NotFound => {
                HttpResponse::NotFound().json(serde_json::json!({
                    "error": "Data tidak ditemukan"
                }))
            }

            MyError::Supabase(msg) => {
                HttpResponse::InternalServerError().json(serde_json::json!({
                    "error": msg
                }))
            }
        }
    }
}