Skip to content

Commit 071a25f

Browse files
authored
handle api 404 errors (#388)
1 parent 674e8dd commit 071a25f

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

src/web/handlers/mod.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use super::data::{
2-
BalanceResponse, CurrenciesResponse, CurrencyResponse, GeneralSearchFilterPayload,
3-
GeneralSearchResponse, OverviewBalanceResponse, OverviewResponse,
1+
use super::{
2+
data::{
3+
BalanceResponse, CurrenciesResponse, CurrencyResponse, GeneralSearchFilterPayload,
4+
GeneralSearchResponse, OverviewBalanceResponse, OverviewResponse,
5+
},
6+
ErrorResponse,
47
};
58
use crate::{
69
constants::VALID_CURRENCIES,
@@ -9,7 +12,7 @@ use crate::{
912
};
1013
use bill::get_current_identity_node_id;
1114
use rocket::{fs::NamedFile, get, post, serde::json::Json, Shutdown, State};
12-
use std::path::Path;
15+
use std::path::{Path, PathBuf};
1316

1417
pub mod bill;
1518
pub mod company;
@@ -19,13 +22,24 @@ pub mod middleware;
1922
pub mod notifications;
2023
pub mod quotes;
2124

25+
// Lowest prio, fall back to index.html if nothing matches
2226
#[get("/<_..>", rank = 10)]
2327
pub async fn serve_frontend() -> Option<NamedFile> {
2428
NamedFile::open(Path::new(&CONFIG.frontend_serve_folder).join("index.html"))
2529
.await
2630
.ok()
2731
}
2832

33+
// Higher prio than file server and index.html fallback
34+
#[get("/<path..>", rank = 3)]
35+
pub async fn default_api_error_catcher(path: PathBuf) -> Json<ErrorResponse> {
36+
Json(ErrorResponse::new(
37+
"not_found",
38+
format!("We couldn't find the requested path '{}'", path.display()),
39+
404,
40+
))
41+
}
42+
2943
#[get("/")]
3044
pub async fn exit(shutdown: Shutdown, state: &State<ServiceContext>) {
3145
log::info!("Exit called - shutting down...");

src/web/mod.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::service::ServiceContext;
22
use api_docs::ApiDocs;
33
use log::info;
4-
use rocket::http::{Method, Status};
4+
use rocket::http::Method;
55
use rocket::{catch, catchers, routes, Build, Config, Request, Rocket};
66
use rocket_cors::{AllowedHeaders, AllowedOrigins, CorsOptions};
77
use serde::Serialize;
@@ -75,8 +75,10 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
7575

7676
let rocket = rocket::custom(config)
7777
.attach(cors.clone())
78+
// catchers for CORS and API errors
7879
.mount("/api/", rocket_cors::catch_all_options_routes())
79-
.register("/", catchers![default_catcher, not_found])
80+
.mount("/api/", routes![handlers::default_api_error_catcher])
81+
.register("/api/", catchers![not_found])
8082
.manage(context)
8183
.manage(cors)
8284
.mount("/api/exit", routes![handlers::exit])
@@ -179,14 +181,15 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
179181
],
180182
)
181183
.mount(
182-
"/api/",
183-
SwaggerUi::new("/swagger-ui/<_..>").url("/api-docs/openapi.json", ApiDocs::openapi()),
184+
"/",
185+
SwaggerUi::new("/api/swagger-ui/<_..>")
186+
.url("/api/api-docs/openapi.json", ApiDocs::openapi()),
184187
)
188+
// Routes for the frontend - lower rank means higher prio
185189
.mount(
186190
&CONFIG.frontend_url_path,
187191
FileServer::from(&CONFIG.frontend_serve_folder).rank(5),
188192
)
189-
// TODO: fall back to index, but serve static files first
190193
.mount(&CONFIG.frontend_url_path, routes![handlers::serve_frontend]);
191194

192195
info!("HTTP Server Listening on {}", conf.http_listen_url());
@@ -205,17 +208,8 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
205208
rocket
206209
}
207210

208-
#[catch(default)]
209-
pub fn default_catcher(status: Status, _req: &Request) -> Json<ErrorResponse> {
210-
Json(ErrorResponse::new(
211-
"error",
212-
status.reason().unwrap_or("Unknown error").to_string(),
213-
status.code,
214-
))
215-
}
216-
217211
#[catch(404)]
218-
pub fn not_found(req: &Request) -> Json<ErrorResponse> {
212+
fn not_found(req: &Request) -> Json<ErrorResponse> {
219213
Json(ErrorResponse::new(
220214
"not_found",
221215
format!("We couldn't find the requested path '{}'", req.uri()),

0 commit comments

Comments
 (0)