|
1 | 1 | use std::path::PathBuf; |
2 | 2 |
|
3 | | -use ohkami::{IntoResponse, Response}; |
| 3 | +use ohkami::{ |
| 4 | + IntoResponse, Response, |
| 5 | + openapi::{self, Schema}, |
| 6 | + serde::Serialize, |
| 7 | +}; |
4 | 8 | use reqwest::header::ToStrError; |
5 | 9 | use thiserror::Error; |
6 | 10 | use tokio::{io, task::JoinError}; |
7 | 11 | use tracing::{error, instrument}; |
8 | 12 |
|
| 13 | +#[derive(Serialize, Schema)] |
| 14 | +#[openapi(component)] |
| 15 | +struct ErrorResponse { |
| 16 | + code: u16, |
| 17 | + message: String, |
| 18 | +} |
| 19 | + |
9 | 20 | #[derive(Debug, Error)] |
10 | 21 | pub enum Error { |
11 | 22 | #[error("I/O error")] |
@@ -36,24 +47,88 @@ impl IntoResponse for Error { |
36 | 47 | #[instrument] |
37 | 48 | fn into_response(self) -> Response { |
38 | 49 | match self { |
39 | | - Error::Io(_) => Response::InternalServerError().with_text("Skin not found"), |
40 | | - Error::Tee(_) => Response::InternalServerError().with_text("Fail to render uv"), |
41 | | - Error::QueryNameNotFound => { |
42 | | - Response::BadRequest().with_text("Query expected name, but got none") |
| 50 | + Error::QueryNameNotFound => Response::BadRequest().with_json(ErrorResponse { |
| 51 | + code: 400, |
| 52 | + message: "Query expected name, but got none".to_string(), |
| 53 | + }), |
| 54 | + Error::Io(e) => { |
| 55 | + tracing::error!("I/O error: {}", e); |
| 56 | + Response::InternalServerError().with_json(ErrorResponse { |
| 57 | + code: 500, |
| 58 | + message: "Skin not found".to_string(), |
| 59 | + }) |
| 60 | + } |
| 61 | + Error::Tee(e) => { |
| 62 | + tracing::error!("Tee error: {}", e); |
| 63 | + Response::InternalServerError().with_json(ErrorResponse { |
| 64 | + code: 500, |
| 65 | + message: "Failed to render UV".to_string(), |
| 66 | + }) |
| 67 | + } |
| 68 | + Error::Reqwest(e) => { |
| 69 | + tracing::error!("Reqwest error: {}", e); |
| 70 | + Response::InternalServerError().with_json(ErrorResponse { |
| 71 | + code: 500, |
| 72 | + message: "External request failed".to_string(), |
| 73 | + }) |
| 74 | + } |
| 75 | + Error::ToStrError(e) => { |
| 76 | + tracing::error!("Header conversion error: {}", e); |
| 77 | + Response::InternalServerError().with_json(ErrorResponse { |
| 78 | + code: 500, |
| 79 | + message: "Invalid header value".to_string(), |
| 80 | + }) |
| 81 | + } |
| 82 | + Error::TaskJoin(e) => { |
| 83 | + tracing::error!("Task join error: {}", e); |
| 84 | + Response::InternalServerError().with_json(ErrorResponse { |
| 85 | + code: 500, |
| 86 | + message: "Background task failed".to_string(), |
| 87 | + }) |
| 88 | + } |
| 89 | + Error::Json(e) => { |
| 90 | + tracing::error!("JSON error: {}", e); |
| 91 | + Response::InternalServerError().with_json(ErrorResponse { |
| 92 | + code: 500, |
| 93 | + message: "JSON processing failed".to_string(), |
| 94 | + }) |
43 | 95 | } |
44 | | - Error::Reqwest(_) => Response::InternalServerError(), |
45 | | - Error::ToStrError(_) => Response::InternalServerError(), |
46 | | - Error::DownloadFailed { |
47 | | - name: _, |
48 | | - error: _, |
49 | | - } => todo!(), |
50 | | - Error::TaskJoin(_join_error) => Response::InternalServerError(), |
51 | | - Error::Json(_error) => Response::InternalServerError(), |
52 | 96 | Error::SaveFailed { |
53 | | - path: _, |
54 | | - name: _, |
55 | | - error: _, |
56 | | - } => Response::InternalServerError(), |
| 97 | + path, |
| 98 | + name, |
| 99 | + error, |
| 100 | + } => { |
| 101 | + tracing::error!("Save failed: {} at {:?}: {}", name, path, error); |
| 102 | + Response::InternalServerError().with_json(ErrorResponse { |
| 103 | + code: 500, |
| 104 | + message: format!("Failed to save {}", name), |
| 105 | + }) |
| 106 | + } |
| 107 | + Error::DownloadFailed { |
| 108 | + name, |
| 109 | + error, |
| 110 | + } => { |
| 111 | + tracing::error!("Download failed: {}: {}", name, error); |
| 112 | + Response::InternalServerError().with_json(ErrorResponse { |
| 113 | + code: 500, |
| 114 | + message: format!("Failed to download {}", name), |
| 115 | + }) |
| 116 | + } |
57 | 117 | } |
58 | 118 | } |
| 119 | + |
| 120 | + fn openapi_responses() -> openapi::Responses { |
| 121 | + openapi::Responses::new([ |
| 122 | + ( |
| 123 | + 400, |
| 124 | + openapi::Response::when("Bad request - invalid query parameters") |
| 125 | + .content("application/json", <ErrorResponse as Schema>::schema()), |
| 126 | + ), |
| 127 | + ( |
| 128 | + 500, |
| 129 | + openapi::Response::when("Internal server error") |
| 130 | + .content("application/json", <ErrorResponse as Schema>::schema()), |
| 131 | + ), |
| 132 | + ]) |
| 133 | + } |
59 | 134 | } |
0 commit comments