From ad7e037a0b178e3133a72f9b301db31886e6fea6 Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Thu, 2 Jan 2025 14:05:04 +0100 Subject: [PATCH] ref(dif): Handle "too big" error with warning This will make redundant the existing warning in the funciton used to validate a DIF's size, allowing us to delete that warning. This change will help make the code more readable and maintainable. --- src/utils/dif_upload/error.rs | 17 ++++++++++++++--- src/utils/dif_upload/mod.rs | 26 ++++++++++---------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/utils/dif_upload/error.rs b/src/utils/dif_upload/error.rs index 0b8c2c1b0a..9b5c693b42 100644 --- a/src/utils/dif_upload/error.rs +++ b/src/utils/dif_upload/error.rs @@ -1,5 +1,6 @@ //! Error types for the dif_upload module. +use indicatif::HumanBytes; use thiserror::Error; /// Represents an error that makes a DIF invalid. @@ -11,12 +12,22 @@ pub enum ValidationError { InvalidFeatures, #[error("Invalid debug ID")] InvalidDebugId, - #[error("Debug file is too large")] - TooLarge, + #[error( + "Debug file's size ({}) exceeds the maximum allowed size ({})", + HumanBytes(*size as u64), + HumanBytes(*max_size) + )] + TooLarge { size: usize, max_size: u64 }, } /// Handles a DIF validation error by logging it to console /// at the appropriate log level. pub fn handle(dif_name: &str, error: &ValidationError) { - log::debug!("Skipping {}: {}", dif_name, error); + let message = format!("Skipping {}: {}", dif_name, error); + match error { + ValidationError::InvalidFormat + | ValidationError::InvalidFeatures + | ValidationError::InvalidDebugId => log::debug!("{message}"), + ValidationError::TooLarge { .. } => log::warn!("{message}"), + } } diff --git a/src/utils/dif_upload/mod.rs b/src/utils/dif_upload/mod.rs index f7038c4528..abcaf462c4 100644 --- a/src/utils/dif_upload/mod.rs +++ b/src/utils/dif_upload/mod.rs @@ -20,7 +20,6 @@ use std::time::Duration; use anyhow::{bail, format_err, Error, Result}; use console::style; -use indicatif::HumanBytes; use log::{debug, info, warn}; use sha1_smol::Digest; use symbolic::common::{Arch, AsSelf, ByteView, DebugId, SelfCell, Uuid}; @@ -1730,21 +1729,13 @@ impl<'a> DifUpload<'a> { } /// Checks if a file is too large and logs skip message if so. - fn valid_size(&self, name: &str, size: usize) -> bool { + fn valid_size(&self, size: usize) -> bool { let file_size: Result = size.try_into(); - let too_large = match file_size { - Ok(file_size) => file_size > self.max_file_size, - Err(_) => true, - }; - if too_large { - warn!( - "Skipping debug file since it exceeds {}: {} ({})", - HumanBytes(self.max_file_size), - name, - HumanBytes(file_size.unwrap_or(u64::MAX)), - ); + + match file_size { + Ok(file_size) => file_size <= self.max_file_size, + Err(_) => false, // Definitely too big } - !too_large } /// Validates DIF on whether it should be processed. @@ -1766,8 +1757,11 @@ impl<'a> DifUpload<'a> { return Err(ValidationError::InvalidDebugId); } - if !self.valid_size(&dif.name, dif.data().len()) { - return Err(ValidationError::TooLarge); + if !self.valid_size(dif.data().len()) { + return Err(ValidationError::TooLarge { + size: dif.data().len(), + max_size: self.max_file_size, + }); } Ok(())