Skip to content

Commit 99fed53

Browse files
committed
Implement error status
1 parent 37d9577 commit 99fed53

3 files changed

Lines changed: 222 additions & 0 deletions

File tree

examples/axum-example/openapi.json

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,134 @@
8181
}
8282
}
8383
},
84+
"/error": {
85+
"get": {
86+
"operationId": "error_endpoint",
87+
"responses": {
88+
"200": {
89+
"description": "Successful response",
90+
"content": {
91+
"application/json": {
92+
"schema": {
93+
"type": "string"
94+
}
95+
}
96+
}
97+
},
98+
"400": {
99+
"description": "Error response",
100+
"content": {
101+
"application/json": {
102+
"schema": {
103+
"$ref": "#/components/schemas/ErrorResponse"
104+
}
105+
}
106+
}
107+
}
108+
}
109+
}
110+
},
111+
"/error/error-with-status": {
112+
"get": {
113+
"operationId": "error_endpoint_with_status_code",
114+
"responses": {
115+
"200": {
116+
"description": "Successful response",
117+
"content": {
118+
"application/json": {
119+
"schema": {
120+
"type": "string"
121+
}
122+
}
123+
}
124+
},
125+
"400": {
126+
"description": "Error response",
127+
"content": {
128+
"application/json": {
129+
"schema": {
130+
"$ref": "#/components/schemas/ErrorResponse"
131+
}
132+
}
133+
}
134+
}
135+
}
136+
}
137+
},
138+
"/error/error-with-status2": {
139+
"get": {
140+
"operationId": "error_endpoint_with_status_code2",
141+
"responses": {
142+
"200": {
143+
"description": "Successful response",
144+
"content": {
145+
"application/json": {
146+
"schema": {
147+
"type": "string"
148+
}
149+
}
150+
}
151+
},
152+
"400": {
153+
"description": "Error response",
154+
"content": {
155+
"application/json": {
156+
"schema": {
157+
"$ref": "#/components/schemas/ErrorResponse2"
158+
}
159+
}
160+
}
161+
},
162+
"404": {
163+
"description": "Error response",
164+
"content": {
165+
"application/json": {
166+
"schema": {
167+
"$ref": "#/components/schemas/ErrorResponse2"
168+
}
169+
}
170+
}
171+
},
172+
"500": {
173+
"description": "Error response",
174+
"content": {
175+
"application/json": {
176+
"schema": {
177+
"$ref": "#/components/schemas/ErrorResponse2"
178+
}
179+
}
180+
}
181+
}
182+
}
183+
}
184+
},
185+
"/error/error2": {
186+
"get": {
187+
"operationId": "error_endpoint2",
188+
"responses": {
189+
"200": {
190+
"description": "Successful response",
191+
"content": {
192+
"application/json": {
193+
"schema": {
194+
"type": "string"
195+
}
196+
}
197+
}
198+
},
199+
"400": {
200+
"description": "Error response",
201+
"content": {
202+
"application/json": {
203+
"schema": {
204+
"$ref": "#/components/schemas/ErrorResponse2"
205+
}
206+
}
207+
}
208+
}
209+
}
210+
}
211+
},
84212
"/health": {
85213
"get": {
86214
"operationId": "health",
@@ -448,6 +576,36 @@
448576
"nestedStructMapArray"
449577
]
450578
},
579+
"ErrorResponse": {
580+
"type": "object",
581+
"properties": {
582+
"code": {
583+
"type": "integer"
584+
},
585+
"error": {
586+
"type": "string"
587+
}
588+
},
589+
"required": [
590+
"error",
591+
"code"
592+
]
593+
},
594+
"ErrorResponse2": {
595+
"type": "object",
596+
"properties": {
597+
"code": {
598+
"type": "integer"
599+
},
600+
"error": {
601+
"type": "string"
602+
}
603+
},
604+
"required": [
605+
"error",
606+
"code"
607+
]
608+
},
451609
"StructBody": {
452610
"type": "object",
453611
"properties": {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

examples/axum-example/src/routes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use vespera::{
66
axum::{Json, extract::Query},
77
};
88

9+
pub mod error;
910
pub mod health;
1011
pub mod path;
1112
pub mod users;

0 commit comments

Comments
 (0)