From 1ef5d539d58ad2e9251624469c898d64431ce6bb Mon Sep 17 00:00:00 2001 From: Solninja A Date: Tue, 31 Dec 2024 19:54:22 +1000 Subject: [PATCH] Improve API error handling --- actix/src/utils.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/actix/src/utils.rs b/actix/src/utils.rs index bd05325..506e5cf 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -18,6 +18,7 @@ struct URLPair { } // Define JSON struct for response +// Named "ReturnResponse" rather than "Response" because of the previous import. #[derive(Serialize)] pub struct Response { pub(crate) success: bool, @@ -47,8 +48,14 @@ pub fn is_api_ok(http: HttpRequest) -> Response { result } } else { - let result = Response {success: false, error: false, reason: "".to_string(), pass: true}; - result + // If the API key isn't set, but an API Key header is provided + if let Some(_) = auth::api_header(&http) { + let result = Response {success: false, error: true, reason: "API key access was attempted, but no API key is configured".to_string(), pass: false}; + result + } else { + let result = Response {success: false, error: false, reason: "".to_string(), pass: true}; + result + } } }