From dc15c89252828d9885d33eee70205de25fff9636 Mon Sep 17 00:00:00 2001 From: Juuso Tuononen Date: Sun, 26 May 2024 15:15:06 +0300 Subject: [PATCH] Possibly fix unspecified error detection --- src/client/response.rs | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/client/response.rs b/src/client/response.rs index f29d2ecc0..1b83fab57 100644 --- a/src/client/response.rs +++ b/src/client/response.rs @@ -38,15 +38,43 @@ impl FcmResponseError { ) -> Option { if let Ok(error) = http_status_code.try_into() { Some(error) - } else if let Some("UNSPECIFIED_ERROR") = response_json.get("error_code").and_then(|v| v.as_str()) { + } else if Self::get_error(response_json) == Some("UNSPECIFIED_ERROR") { Some(Self::Unspecified) + } else if response_json.get("name").is_none() { + Some(Self::Unknown) } else { - match response_json.get("name") { - Some(_) => None, // No error - None => Some(Self::Unknown), - } + None // No error } } + + fn get_error( + response_json: &serde_json::Map, + ) -> Option<&str> { + Self::get_error_using_api_reference(response_json) + .or_else(|| Self::get_error_using_real_response(response_json)) + } + + /// Currently (2024-05-26) FCM API response JSON does not have + /// this location for INVALID_ARGUMENT error. + fn get_error_using_api_reference( + response_json: &serde_json::Map, + ) -> Option<&str> { + response_json + .get("error_code") + .and_then(|v| v.as_str()) + } + + /// Current (2024-05-26) FCM API response JSON location for + /// INVALID_ARGUMENT error and possibly for the other errors + /// as well. + fn get_error_using_real_response( + response_json: &serde_json::Map, + ) -> Option<&str> { + response_json + .get("error") + .and_then(|v| v.get("status")) + .and_then(|v| v.as_str()) + } } impl TryFrom for FcmResponseError {