From 102ee3f871152677efafa6688722808f31a95e62 Mon Sep 17 00:00:00 2001 From: Stefan Melmuk Date: Sun, 25 Sep 2022 10:55:55 +0200 Subject: [PATCH] add api_not_found catcher for 404 errors in /api --- src/api/core/mod.rs | 16 ++++++++++++++++ src/api/mod.rs | 1 + src/main.rs | 1 + 3 files changed, 18 insertions(+) diff --git a/src/api/core/mod.rs b/src/api/core/mod.rs index f296a3c9..8d180767 100644 --- a/src/api/core/mod.rs +++ b/src/api/core/mod.rs @@ -38,6 +38,7 @@ pub fn routes() -> Vec { // Move this somewhere else // use rocket::serde::json::Json; +use rocket::Catcher; use rocket::Route; use serde_json::Value; @@ -221,3 +222,18 @@ fn config() -> Json { }, })) } + +pub fn catchers() -> Vec { + catchers![api_not_found] +} + +#[catch(404)] +fn api_not_found() -> Json { + Json(json!({ + "error": { + "code": 404, + "reason": "Not Found", + "description": "The requested resource could not be found." + } + })) +} diff --git a/src/api/mod.rs b/src/api/mod.rs index 3b27c6c8..b9e9f38c 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -10,6 +10,7 @@ use serde_json::Value; pub use crate::api::{ admin::routes as admin_routes, + core::catchers as core_catchers, core::purge_sends, core::purge_trashed_ciphers, core::routes as core_routes, diff --git a/src/main.rs b/src/main.rs index b33b4fa1..c877c01c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -426,6 +426,7 @@ async fn launch_rocket(pool: db::DbPool, extra_debug: bool) -> Result<(), Error> .mount([basepath, "/icons"].concat(), api::icons_routes()) .mount([basepath, "/notifications"].concat(), api::notifications_routes()) .register([basepath, "/"].concat(), api::web_catchers()) + .register([basepath, "/api"].concat(), api::core_catchers()) .manage(pool) .manage(api::start_notification_server()) .attach(util::AppHeaders())