From 4d28be5f6badcfdc2fb059e4a176f0efb5db5fec Mon Sep 17 00:00:00 2001 From: Fleeym Date: Mon, 21 Sep 2026 23:25:56 +0300 Subject: [PATCH] fix: fix ModZipError throwing 500 in a lot of cases ModZipError was returning 500 for a lot of cases: log size limits, invalid zip file, etc. Reduced the 500s to actual internal errors: I/O, etc. --- src/endpoints/mod.rs | 2 ++ src/mod_zip.rs | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/endpoints/mod.rs b/src/endpoints/mod.rs index 7bcb35b..bca07a1 100644 --- a/src/endpoints/mod.rs +++ b/src/endpoints/mod.rs @@ -70,6 +70,8 @@ impl actix_web::ResponseError for ApiError { ApiError::TooManyRequests(..) => StatusCode::TOO_MANY_REQUESTS, ApiError::NotFound(_) => StatusCode::NOT_FOUND, ApiError::BadRequest(..) => StatusCode::BAD_REQUEST, + ApiError::ModZip(err) => err.status_code(), + ApiError::PlatformParseError(_) => StatusCode::BAD_REQUEST, _ => StatusCode::INTERNAL_SERVER_ERROR, } } diff --git a/src/mod_zip.rs b/src/mod_zip.rs index b9b1fec..06bec7a 100644 --- a/src/mod_zip.rs +++ b/src/mod_zip.rs @@ -1,6 +1,7 @@ use std::io::Seek; use std::io::{BufReader, Cursor, Read}; +use actix_web::http::StatusCode; use actix_web::web::Bytes; use image::codecs::png::PngDecoder; use image::codecs::png::PngEncoder; @@ -11,6 +12,8 @@ use zip::ZipArchive; use zip::read::ZipFile; use zip::result::ZipError; +use crate::endpoints::ApiError::ModZip; + #[derive(thiserror::Error, Debug)] pub enum ModZipError { #[error("I/O error: {0}")] @@ -39,6 +42,23 @@ pub enum ModZipError { InvalidBinaries(String), } +impl ModZipError { + pub fn status_code(&self) -> StatusCode { + match self { + ModZipError::IoError(_) => StatusCode::INTERNAL_SERVER_ERROR, + ModZipError::ImageError(e) => match (e) { + ImageError::Limits(_) => StatusCode::BAD_REQUEST, + _ => StatusCode::INTERNAL_SERVER_ERROR, + }, + ModZipError::ZipError(e) => match (e) { + ZipError::InvalidArchive(_) => StatusCode::BAD_REQUEST, + _ => StatusCode::INTERNAL_SERVER_ERROR, + }, + _ => StatusCode::BAD_REQUEST, + } + } +} + pub fn extract_mod_logo(file: &mut ZipFile) -> Result, ModZipError> { const FIVE_MEGABYTES: u64 = 5 * 1000 * 1000; if file.size() > FIVE_MEGABYTES {