Skip to content

Commit 36cd6be

Browse files
committed
Return error json when validate failed
1 parent 1ca8f02 commit 36cd6be

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

aiscript-runtime/src/endpoint.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ use crate::error::ServerError;
3939

4040
type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
4141

42-
impl IntoResponse for ServerError {
43-
fn into_response(self) -> Response {
44-
(axum::http::StatusCode::BAD_REQUEST, self.to_string()).into_response()
45-
}
46-
}
47-
4842
#[derive(Clone)]
4943
pub struct Field {
5044
name: String,

aiscript-runtime/src/error.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use aiscript_vm::VmError;
2-
use axum::extract::rejection;
2+
use axum::{
3+
Json,
4+
extract::rejection,
5+
response::{IntoResponse, Response},
6+
};
37
use thiserror::Error;
48

59
#[derive(Error, Debug)]
@@ -29,3 +33,15 @@ pub enum ServerError {
2933
// #[error("Internal server error: {0}")]
3034
// InternalError(String),
3135
}
36+
37+
impl IntoResponse for ServerError {
38+
fn into_response(self) -> Response {
39+
// Convert the error to a JSON object with an "error" field
40+
let error_json = serde_json::json!({
41+
"error": self.to_string()
42+
});
43+
44+
// Return as a JSON response with BAD_REQUEST status
45+
(axum::http::StatusCode::BAD_REQUEST, Json(error_json)).into_response()
46+
}
47+
}

0 commit comments

Comments
 (0)