Skip to content

Commit

Permalink
ref(dif): Handle "too big" error with warning
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
szokeasaurusrex committed Jan 7, 2025
1 parent ed061bf commit ad7e037
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
17 changes: 14 additions & 3 deletions src/utils/dif_upload/error.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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}"),
}
}
26 changes: 10 additions & 16 deletions src/utils/dif_upload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<u64, _> = 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.
Expand All @@ -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(())
Expand Down

0 comments on commit ad7e037

Please sign in to comment.