Skip to content

Commit 79a5c34

Browse files
shusann01116syphar
authored andcommitted
Handle SizeLimit error
1 parent 8aca72f commit 79a5c34

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/web/source.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ struct SourcePage {
153153
file: Option<File>,
154154
file_content: Option<String>,
155155
canonical_url: CanonicalUrl,
156+
is_file_too_large: bool,
156157
is_latest_url: bool,
157158
use_direct_platform_links: bool,
158159
}
@@ -226,7 +227,7 @@ pub(crate) async fn source_browser_handler(
226227

227228
// try to get actual file first
228229
// skip if request is a directory
229-
let blob = if !params.path.ends_with('/') {
230+
let (blob, is_file_too_large) = if !params.path.ends_with('/') {
230231
match storage
231232
.fetch_source_file(
232233
&params.name,
@@ -238,17 +239,23 @@ pub(crate) async fn source_browser_handler(
238239
.await
239240
.context("error fetching source file")
240241
{
241-
Ok(blob) => Some(blob),
242-
Err(err) => {
243-
if err.is::<PathNotFoundError>() {
244-
None
245-
} else {
246-
return Err(err.into());
242+
Ok(blob) => (Some(blob), false),
243+
Err(err) => match err {
244+
err if err.is::<PathNotFoundError>() => (None, false),
245+
// if file is too large, set is_file_too_large to true
246+
err if err.downcast_ref::<std::io::Error>().is_some_and(|err| {
247+
err.get_ref()
248+
.map(|err| err.is::<crate::error::SizeLimitReached>())
249+
.unwrap_or(false)
250+
}) =>
251+
{
252+
(None, true)
247253
}
248-
}
254+
_ => return Err(err.into()),
255+
},
249256
}
250257
} else {
251-
None
258+
(None, false)
252259
};
253260

254261
let canonical_url = CanonicalUrl::from_path(format!(
@@ -305,6 +312,7 @@ pub(crate) async fn source_browser_handler(
305312
file,
306313
file_content,
307314
canonical_url,
315+
is_file_too_large,
308316
is_latest_url: params.version.is_latest(),
309317
use_direct_platform_links: true,
310318
}

templates/crate/source.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
2828
{%- block body -%}
2929
<div class="container package-page-container small-bottom-pad">
3030
<div class="pure-g">
31+
{# If the file exeeded the maximum size, display a warning #}
32+
{%- if is_file_too_large -%}
33+
<div class="pure-u-1 pure-u-sm-17-24 pure-u-md-19-24">
34+
<div class="warning">
35+
<p>
36+
This file is too large to display.
37+
</p>
38+
</div>
39+
</div>
40+
{%- endif -%}
41+
3142
<div id="side-menu" class="pure-u-1 {% if file_content %}pure-u-sm-7-24 pure-u-md-5-24 source-view{% endif %}">
3243
<div class="pure-menu package-menu">
3344
<ul class="pure-menu-list">

0 commit comments

Comments
 (0)