Log and bail out on failing to convert lifetime

This commit is contained in:
Jan-Erik Rediger 2019-05-20 11:37:52 +02:00
Родитель b8dc19d966
Коммит 2d936138b0
3 изменённых файлов: 11 добавлений и 23 удалений

Просмотреть файл

@ -95,11 +95,7 @@ pub extern "C" fn glean_new_boolean_metric(
) -> u64 {
BOOLEAN_METRICS.insert_with_log(|| {
let send_in_pings = unsafe { from_raw_string_array(send_in_pings, send_in_pings_len) };
let lifetime = Lifetime::try_from(lifetime)
.map_err(|_| {
log::error!("[Bool] Failed to convert from lifetime value {}", lifetime);
})
.unwrap_or(Lifetime::Ping);
let lifetime = Lifetime::try_from(lifetime)?;
Ok(BooleanMetric::new(CommonMetricData {
name: name.into_string(),
@ -122,14 +118,7 @@ pub extern "C" fn glean_new_string_metric(
) -> u64 {
STRING_METRICS.insert_with_log(|| {
let send_in_pings = unsafe { from_raw_string_array(send_in_pings, send_in_pings_len) };
let lifetime = Lifetime::try_from(lifetime)
.map_err(|_| {
log::error!(
"[String] Failed to convert from lifetime value {}",
lifetime
);
})
.unwrap_or(Lifetime::Ping);
let lifetime = Lifetime::try_from(lifetime)?;
Ok(StringMetric::new(CommonMetricData {
name: name.into_string(),
@ -152,14 +141,7 @@ pub extern "C" fn glean_new_counter_metric(
) -> u64 {
COUNTER_METRICS.insert_with_log(|| {
let send_in_pings = unsafe { from_raw_string_array(send_in_pings, send_in_pings_len) };
let lifetime = Lifetime::try_from(lifetime)
.map_err(|_| {
log::error!(
"[Counter] Failed to convert from lifetime value {}",
lifetime
);
})
.unwrap_or(Lifetime::Ping);
let lifetime = Lifetime::try_from(lifetime)?;
Ok(CounterMetric::new(CommonMetricData {
name: name.into_string(),

Просмотреть файл

@ -4,6 +4,8 @@
use std::convert::TryFrom;
use crate::error::{Error, ErrorKind};
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Lifetime {
/// The metric is reset with each sent ping
@ -31,14 +33,14 @@ impl Lifetime {
}
impl TryFrom<i32> for Lifetime {
type Error = (); // FIXME: Proper error type needed
type Error = Error;
fn try_from(value: i32) -> Result<Lifetime, Self::Error> {
match value {
0 => Ok(Lifetime::Ping),
1 => Ok(Lifetime::Application),
2 => Ok(Lifetime::User),
_ => Err(()),
e => Err(ErrorKind::Lifetime(e))?,
}
}
}

Просмотреть файл

@ -27,6 +27,10 @@ pub type Result<T> = result::Result<T, Error>;
/// [`Error`]: std.struct.Error.html
#[derive(Debug, Fail)]
pub enum ErrorKind {
/// Lifetime conversion failed
#[fail(display = "Lifetime conversion from {} failed", _0)]
Lifetime(i32),
/// FFI-Support error
#[fail(display = "Invalid handle")]
Handle(HandleError),