Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit 6ec8994

Browse files
authored
fix(auth): Do not panic when parsing sentry_timestamp (#34)
1 parent 8b2161f commit 6ec8994

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/auth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl FromStr for Auth {
175175
match (kviter.next(), kviter.next()) {
176176
(Some("sentry_timestamp"), Some(ts)) => {
177177
let f = ts.parse().map_err(|_| AuthParseError::InvalidTimestamp)?;
178-
rv.timestamp = Some(timestamp_to_datetime(f));
178+
rv.timestamp = timestamp_to_datetime(f).single();
179179
}
180180
(Some("sentry_client"), Some(client)) => {
181181
rv.client = Some(client.into());

src/utils.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![cfg_attr(not(feature = "with_protocol"), allow(unused))]
2-
use chrono::{DateTime, TimeZone, Utc};
2+
use chrono::{DateTime, LocalResult, TimeZone, Utc};
33

44
/// Converts a datetime object into a float timestamp.
55
pub fn datetime_to_timestamp(dt: &DateTime<Utc>) -> f64 {
@@ -10,14 +10,14 @@ pub fn datetime_to_timestamp(dt: &DateTime<Utc>) -> f64 {
1010
}
1111
}
1212

13-
pub fn timestamp_to_datetime(ts: f64) -> DateTime<Utc> {
13+
pub fn timestamp_to_datetime(ts: f64) -> LocalResult<DateTime<Utc>> {
1414
let secs = ts as i64;
1515
let micros = (ts.fract() * 1_000_000f64) as u32;
16-
Utc.timestamp_opt(secs, micros * 1000).unwrap()
16+
Utc.timestamp_opt(secs, micros * 1000)
1717
}
1818

1919
pub mod ts_seconds_float {
20-
use chrono::{DateTime, TimeZone, Utc};
20+
use chrono::{DateTime, LocalResult, TimeZone, Utc};
2121
use serde::{de, ser};
2222
use std::fmt;
2323

@@ -57,7 +57,14 @@ pub mod ts_seconds_float {
5757
where
5858
E: de::Error,
5959
{
60-
Ok(timestamp_to_datetime(value))
60+
match timestamp_to_datetime(value) {
61+
LocalResult::None => Err(E::custom(format!("No such local time for {}", value))),
62+
LocalResult::Single(date) => Ok(date),
63+
LocalResult::Ambiguous(t1, t2) => Err(E::custom(format!(
64+
"Ambiguous local time, ranging from {:?} to {:?}",
65+
t1, t2
66+
))),
67+
}
6168
}
6269

6370
fn visit_i64<E>(self, value: i64) -> Result<DateTime<Utc>, E>

0 commit comments

Comments
 (0)