From 683df3e58cbd56bd64fd88ed28bd0e8cbaa3c578 Mon Sep 17 00:00:00 2001 From: David Crespo Date: Tue, 3 Dec 2024 14:43:30 -0600 Subject: [PATCH] extract error map logic to helper for reasons of cuteness --- nexus/src/app/metrics.rs | 58 +++++++++++++++------------------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/nexus/src/app/metrics.rs b/nexus/src/app/metrics.rs index 7f2bd8c1e0..5b77e681b1 100644 --- a/nexus/src/app/metrics.rs +++ b/nexus/src/app/metrics.rs @@ -140,29 +140,15 @@ impl super::Nexus { self.timeseries_client .oxql_query(query) .await - .map(|result| { - // TODO-observability: The query method returns information - // about the duration of the OxQL query and the database - // resource usage for each contained SQL query. We should - // publish this as a timeseries itself, so that we can track - // improvements to query processing. - // - // For now, simply return the tables alone. - result.tables - }) - .map_err(|e| match e { - oximeter_db::Error::DatabaseUnavailable(_) - | oximeter_db::Error::Connection(_) => { - Error::ServiceUnavailable { - internal_message: e.to_string(), - } - } - oximeter_db::Error::Oxql(_) - | oximeter_db::Error::TimeseriesNotFound(_) => { - Error::invalid_request(e.to_string()) - } - _ => Error::InternalError { internal_message: e.to_string() }, - }) + // TODO-observability: The query method returns information + // about the duration of the OxQL query and the database + // resource usage for each contained SQL query. We should + // publish this as a timeseries itself, so that we can track + // improvements to query processing. + // + // For now, simply return the tables alone. + .map(|result| result.tables) + .map_err(map_timeseries_err) } /// Run an OxQL query against the timeseries database, scoped to a specific project. @@ -188,18 +174,18 @@ impl super::Nexus { .oxql_query(filtered_query) .await .map(|result| result.tables) - .map_err(|e| match e { - oximeter_db::Error::DatabaseUnavailable(_) - | oximeter_db::Error::Connection(_) => { - Error::ServiceUnavailable { - internal_message: e.to_string(), - } - } - oximeter_db::Error::Oxql(_) - | oximeter_db::Error::TimeseriesNotFound(_) => { - Error::invalid_request(e.to_string()) - } - _ => Error::InternalError { internal_message: e.to_string() }, - }) + .map_err(map_timeseries_err) + } +} + +fn map_timeseries_err(e: oximeter_db::Error) -> Error { + match e { + oximeter_db::Error::DatabaseUnavailable(_) + | oximeter_db::Error::Connection(_) => Error::unavail(&e.to_string()), + oximeter_db::Error::Oxql(_) + | oximeter_db::Error::TimeseriesNotFound(_) => { + Error::invalid_request(e.to_string()) + } + _ => Error::internal_error(&e.to_string()), } }