Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "axiom-rs"
version = "0.11.1"
version = "0.11.2"
authors = ["Arne Bahlo <arne@axiom.co>"]
edition = "2018"
rust-version = "1.60"
Expand Down
46 changes: 34 additions & 12 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ pub struct Axiom {
pub path: String,
/// The error message.
pub message: Option<String>,
/// The trace id.
#[serde(skip)]
pub trace_id: Option<String>,
}

impl Axiom {
Expand All @@ -131,12 +134,14 @@ impl Axiom {
method: http::Method,
path: String,
message: Option<String>,
trace_id: Option<String>,
) -> Self {
Self {
status,
method,
path,
message,
trace_id,
}
}
}
Expand All @@ -145,18 +150,35 @@ impl std::error::Error for Axiom {}

impl fmt::Display for Axiom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(msg) = self.message.as_ref() {
write!(
f,
"Received {} on {} {}: {}",
self.status, self.method, self.path, msg
)
} else {
write!(
f,
"Received {} on {} {})",
self.method, self.path, self.status
)
match (self.message.as_ref(), self.trace_id.as_ref()) {
(Some(msg), Some(trace_id)) => {
write!(
f,
"Received {} on {} {}: {} (trace id: {})",
self.status, self.method, self.path, msg, trace_id
)
}
(Some(msg), None) => {
write!(
f,
"Received {} on {} {}: {}",
self.status, self.method, self.path, msg
)
}
(None, Some(trace_id)) => {
write!(
f,
"Received {} on {} {} (trace id: {})",
self.status, self.method, self.path, trace_id
)
}
(None, None) => {
write!(
f,
"Received {} on {} {})",
self.method, self.path, self.status
)
}
}
}
}
14 changes: 13 additions & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ impl Response {

pub(crate) async fn check_error(self) -> Result<Response> {
let status = self.inner.status();
let trace_id = self
.headers()
.get("x-axiom-trace-id")
.and_then(|trace_id| trace_id.to_str().ok())
.map(std::string::ToString::to_string);
if !status.is_success() {
// Check if we hit some limits
match self.limits {
Expand All @@ -230,11 +235,18 @@ impl Response {
e.status = status.as_u16();
e.method = self.method;
e.path = self.path;
e.trace_id = trace_id;
Error::Axiom(e)
}
Err(_e) => {
// Decoding failed, we still want an AxiomError
Error::Axiom(Axiom::new(status.as_u16(), self.method, self.path, None))
Error::Axiom(Axiom::new(
status.as_u16(),
self.method,
self.path,
None,
trace_id,
))
}
};
return Err(e);
Expand Down
Loading