From 68189d4872fcd2f9ba23c168a534b8bce2f26bb5 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 17 Jun 2018 03:46:50 +0200 Subject: refactor(main): Log error responses from Stackdriver In some cases Stackdriver seems to return error responses for batched inserts. This change will log the error response body and status from Stackdriver for all insertion errors. --- src/main.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index f6c4ea0d9060..7a032f98f3e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,7 @@ //! * TODO 2018-06-15: Extract timestamps from journald instead of //! relying on ingestion timestamps. +#[macro_use] extern crate failure; #[macro_use] extern crate hyper; #[macro_use] extern crate log; #[macro_use] extern crate serde_derive; @@ -42,7 +43,6 @@ extern crate chrono; extern crate env_logger; -extern crate failure; extern crate reqwest; extern crate serde; extern crate systemd; @@ -386,13 +386,17 @@ fn prepare_request(entries: &[LogEntry]) -> Value { /// Perform the log entry insertion in Stackdriver Logging. fn write_entries(client: &Client, token: &Token, request: Value) -> Result<()> { - client.post(ENTRIES_WRITE_URL) + let mut response = client.post(ENTRIES_WRITE_URL) .header(header::Authorization(format!("Bearer {}", token.token))) .json(&request) - .send()? - .error_for_status()?; + .send()?; - Ok(()) + if response.status().is_success() { + Ok(()) + } else { + let body = response.text().unwrap_or_else(|_| "no response body".into()); + bail!("{} ({})", body, response.status()) + } } /// Attempt to read the initial cursor position from the configured -- cgit 1.4.1