mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-25 15:16:07 +00:00
Change API errors to be JSON
This commit is contained in:
@@ -13,7 +13,7 @@ use crate::{
|
|||||||
serve_session::ServeSession,
|
serve_session::ServeSession,
|
||||||
web::{
|
web::{
|
||||||
interface::{
|
interface::{
|
||||||
Instance, NotFoundError, ReadResponse, ServerInfoResponse, SubscribeResponse,
|
ErrorResponse, Instance, ReadResponse, ServerInfoResponse, SubscribeResponse,
|
||||||
PROTOCOL_VERSION, SERVER_VERSION,
|
PROTOCOL_VERSION, SERVER_VERSION,
|
||||||
},
|
},
|
||||||
util::{json, json_ok},
|
util::{json, json_ok},
|
||||||
@@ -38,7 +38,12 @@ impl<F: ImfsFetcher> Service for ApiService<F> {
|
|||||||
(&Method::GET, path) if path.starts_with("/api/subscribe/") => {
|
(&Method::GET, path) if path.starts_with("/api/subscribe/") => {
|
||||||
self.handle_api_subscribe(request)
|
self.handle_api_subscribe(request)
|
||||||
}
|
}
|
||||||
_ => json(NotFoundError, StatusCode::NOT_FOUND),
|
(_method, path) => {
|
||||||
|
return json(
|
||||||
|
ErrorResponse::not_found(format!("Route not found: {}", path)),
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,5 +78,24 @@ pub struct SubscribeResponse {
|
|||||||
pub messages: Vec<SubscribeMessage>,
|
pub messages: Vec<SubscribeMessage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// General response type returned from all Rojo routes
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct NotFoundError;
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct ErrorResponse {
|
||||||
|
kind: ErrorResponseKind,
|
||||||
|
details: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ErrorResponse {
|
||||||
|
pub fn not_found<S: Into<String>>(details: S) -> Self {
|
||||||
|
Self {
|
||||||
|
kind: ErrorResponseKind::NotFound,
|
||||||
|
details: details.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub enum ErrorResponseKind {
|
||||||
|
NotFound,
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use crate::{
|
|||||||
imfs::new::ImfsFetcher,
|
imfs::new::ImfsFetcher,
|
||||||
serve_session::ServeSession,
|
serve_session::ServeSession,
|
||||||
web::{
|
web::{
|
||||||
interface::{NotFoundError, SERVER_VERSION},
|
interface::{ErrorResponse, SERVER_VERSION},
|
||||||
util::json,
|
util::json,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -33,7 +33,12 @@ impl<F: ImfsFetcher> Service for UiService<F> {
|
|||||||
(&Method::GET, "/") => self.handle_home(),
|
(&Method::GET, "/") => self.handle_home(),
|
||||||
(&Method::GET, "/visualize/rbx") => self.handle_visualize_rbx(),
|
(&Method::GET, "/visualize/rbx") => self.handle_visualize_rbx(),
|
||||||
(&Method::GET, "/visualize/imfs") => self.handle_visualize_imfs(),
|
(&Method::GET, "/visualize/imfs") => self.handle_visualize_imfs(),
|
||||||
_ => return json(NotFoundError, StatusCode::NOT_FOUND),
|
(_method, path) => {
|
||||||
|
return json(
|
||||||
|
ErrorResponse::not_found(format!("Route not found: {}", path)),
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Box::new(future::ok(response))
|
Box::new(future::ok(response))
|
||||||
|
|||||||
Reference in New Issue
Block a user