Skip to content

Commit

Permalink
Handle SizeLimit error
Browse files Browse the repository at this point in the history
  • Loading branch information
shusann01116 committed May 11, 2024
1 parent cb1b1f3 commit daea5ef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/web/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ struct SourcePage {
file: Option<File>,
file_content: Option<String>,
canonical_url: CanonicalUrl,
is_file_too_large: bool,
is_latest_url: bool,
use_direct_platform_links: bool,
}
Expand Down Expand Up @@ -226,7 +227,7 @@ pub(crate) async fn source_browser_handler(

// try to get actual file first
// skip if request is a directory
let blob = if !params.path.ends_with('/') {
let (blob, is_file_too_large) = if !params.path.ends_with('/') {
match storage
.fetch_source_file(
&params.name,
Expand All @@ -238,17 +239,23 @@ pub(crate) async fn source_browser_handler(
.await
.context("error fetching source file")
{
Ok(blob) => Some(blob),
Err(err) => {
if err.is::<PathNotFoundError>() {
None
} else {
return Err(err.into());
Ok(blob) => (Some(blob), false),
Err(err) => match err {
err if err.is::<PathNotFoundError>() => (None, false),
// if file is too large, set is_file_too_large to true
err if err.downcast_ref::<std::io::Error>().is_some_and(|err| {
err.get_ref()
.map(|err| err.is::<crate::error::SizeLimitReached>())
.unwrap_or(false)
}) =>
{
(None, true)
}
}
_ => return Err(err.into()),
},
}
} else {
None
(None, false)
};

let canonical_url = CanonicalUrl::from_path(format!(
Expand Down Expand Up @@ -305,6 +312,7 @@ pub(crate) async fn source_browser_handler(
file,
file_content,
canonical_url,
is_file_too_large,
is_latest_url: params.version.is_latest(),
use_direct_platform_links: true,
}
Expand Down
11 changes: 11 additions & 0 deletions templates/crate/source.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@
{%- block body -%}
<div class="container package-page-container small-bottom-pad">
<div class="pure-g">
{# If the file exeeded the maximum size, display a warning #}
{%- if is_file_too_large -%}
<div class="pure-u-1 pure-u-sm-17-24 pure-u-md-19-24">
<div class="warning">
<p>
This file is too large to display.
</p>
</div>
</div>
{%- endif -%}

<div id="side-menu" class="pure-u-1 {% if file_content %}pure-u-sm-7-24 pure-u-md-5-24 source-view{% endif %}">
<div class="pure-menu package-menu">
<ul class="pure-menu-list">
Expand Down

0 comments on commit daea5ef

Please sign in to comment.