|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use vespera::{ |
| 3 | + Schema, |
| 4 | + axum::{Json, http::StatusCode, response::IntoResponse}, |
| 5 | +}; |
| 6 | + |
| 7 | +#[derive(Serialize, Deserialize, Schema)] |
| 8 | +pub struct ErrorResponse { |
| 9 | + pub error: String, |
| 10 | + pub code: u32, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Serialize, Deserialize, Schema)] |
| 14 | +pub struct ErrorResponse2 { |
| 15 | + pub error: String, |
| 16 | + pub code: u32, |
| 17 | +} |
| 18 | + |
| 19 | +impl IntoResponse for ErrorResponse2 { |
| 20 | + fn into_response(self) -> vespera::axum::response::Response { |
| 21 | + (StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response() |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[vespera::route()] |
| 26 | +pub async fn error_endpoint() -> Result<&'static str, Json<ErrorResponse>> { |
| 27 | + Err(Json(ErrorResponse { |
| 28 | + error: "Internal server error".to_string(), |
| 29 | + code: 500, |
| 30 | + })) |
| 31 | +} |
| 32 | + |
| 33 | +#[vespera::route(path = "/error-with-status")] |
| 34 | +pub async fn error_endpoint_with_status_code() |
| 35 | +-> Result<&'static str, (StatusCode, Json<ErrorResponse>)> { |
| 36 | + Err(( |
| 37 | + StatusCode::INTERNAL_SERVER_ERROR, |
| 38 | + Json(ErrorResponse { |
| 39 | + error: "Internal server error".to_string(), |
| 40 | + code: 500, |
| 41 | + }), |
| 42 | + )) |
| 43 | +} |
| 44 | + |
| 45 | +#[vespera::route(path = "/error2")] |
| 46 | +pub async fn error_endpoint2() -> Result<&'static str, ErrorResponse2> { |
| 47 | + Err(ErrorResponse2 { |
| 48 | + error: "Internal server error".to_string(), |
| 49 | + code: 500, |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +#[vespera::route(path = "/error-with-status2", error_status = [500, 400, 404])] |
| 54 | +pub async fn error_endpoint_with_status_code2() -> Result<&'static str, (StatusCode, ErrorResponse2)> |
| 55 | +{ |
| 56 | + Err(( |
| 57 | + StatusCode::INTERNAL_SERVER_ERROR, |
| 58 | + ErrorResponse2 { |
| 59 | + error: "Internal server error".to_string(), |
| 60 | + code: 500, |
| 61 | + }, |
| 62 | + )) |
| 63 | +} |
0 commit comments