about summary refs log tree commit diff
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs59
1 files changed, 50 insertions, 9 deletions
diff --git a/src/error.rs b/src/error.rs
index a9b01bd..b84cf9b 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -121,27 +121,40 @@ impl ApiError {
     /// the inline `map_err` sites agree.
     pub fn from_db(error: crate::db::DbError, operation_id: &str) -> ApiError {
         match error {
-            crate::db::DbError::Unavailable(detail) => ApiError::new(
-                StatusCode::SERVICE_UNAVAILABLE,
-                "db_unavailable",
-                "Database unavailable",
-                detail,
-                operation_id,
-            ),
+            crate::db::DbError::Unavailable(detail) => {
+                crate::db::record_failure(crate::db::FailureKind::Unavailable, detail.clone());
+                ApiError::new(
+                    StatusCode::SERVICE_UNAVAILABLE,
+                    "db_unavailable",
+                    "Database unavailable",
+                    detail,
+                    operation_id,
+                )
+            }
             crate::db::DbError::Query(error) => {
                 // A connection that died under the query is not the
                 // query's fault. Forget the pool so the next request
                 // cannot draw the same dead sockets -- this is the one
                 // place every database error passes through, so the
                 // recovery does not depend on each caller remembering it.
+                let detail = error.to_string();
+                let lost = crate::db::is_connection_loss_detail(&detail);
+                crate::db::record_failure(
+                    if lost {
+                        crate::db::FailureKind::ConnectionLost
+                    } else {
+                        crate::db::FailureKind::Query
+                    },
+                    detail.clone(),
+                );
                 let api_error = ApiError::new(
                     StatusCode::INTERNAL_SERVER_ERROR,
                     "database_error",
                     "Database error",
-                    error.to_string(),
+                    detail,
                     operation_id,
                 );
-                if crate::db::is_connection_loss(&crate::db::DbError::Query(error)) {
+                if lost {
                     crate::db::forget_pool();
                 }
                 api_error
@@ -153,10 +166,38 @@ impl ApiError {
 impl IntoResponse for ApiError {
     fn into_response(self) -> Response {
         let status = StatusCode::from_u16(self.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
+        // Server-side failures get one line on stderr each, as JSON, so a
+        // caller's report ("500 at 12:31, request id ...") can be matched to
+        // what actually happened. 4xx are not logged: they are the caller's
+        // business (and a busy 401/422 stream would drown this out), but a
+        // 5xx is ours, and until this existed the service kept no record of
+        // one at all.
+        if status.is_server_error() {
+            eprintln!(
+                "{}",
+                serde_json::json!({
+                    "event": "error",
+                    "status": status.as_u16(),
+                    "code": self.code,
+                    "operation_id": self.operation_id,
+                    "request_id": self.request_id,
+                    "took_ms": self.took_ms,
+                    "detail": truncate(&self.detail, 500),
+                })
+            );
+        }
         (status, Json(self)).into_response()
     }
 }
 
+/// `text` cut to `limit` characters (not bytes), for one log line.
+fn truncate(text: &str, limit: usize) -> String {
+    if text.chars().count() <= limit {
+        return text.to_string();
+    }
+    text.chars().take(limit).collect::<String>() + "…"
+}
+
 /// The 401 every token-gated endpoint can return, as one shared
 /// `IntoResponses` type (an `ApiError` body under the hood) instead of
 /// writing out `(status = 401, description = "...", body = ApiError)`