Skip to content

Commit 9e33bd4

Browse files
committed
Show better error message on exceeding absolute request body cap
This got dinged a few times yesterday on sentry (https://sentry.ppy.sh/organizations/ppy/issues/73418/?project=12) because somebody hit the absolute 200MB cap a few times. This change is designed to (a) stop that from dinging on sentry because there's really no reason to log it as error, and (b) give the user a better error message to work with because the current one was kinda opaque (would just show "BadRequest" or similar).
1 parent 74d5b53 commit 9e33bd4

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

osu.Server.BeatmapSubmission/BeatmapSubmissionController.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -493,22 +493,8 @@ private void checkPackageSize(long packageSizeBytes, BeatmapPackageParseResult p
493493
if (packageSizeBytes > allowableSizeBytes)
494494
{
495495
throw new InvariantException($"The beatmap package is too large. "
496-
+ $"The size of the package with the requested changes applied is {humaniseSize(packageSizeBytes)}. "
497-
+ $"The maximum allowable size is {humaniseSize(allowableSizeBytes)}.");
498-
}
499-
500-
static string humaniseSize(double sizeBytes)
501-
{
502-
string humanisedSize;
503-
504-
if (sizeBytes < 1024)
505-
humanisedSize = $@"{sizeBytes}B";
506-
else if (sizeBytes < 1024 * 1024)
507-
humanisedSize = $@"{sizeBytes / 1024:#.0}kB";
508-
else
509-
humanisedSize = $@"{sizeBytes / 1024 / 1024:#.0}MB";
510-
511-
return humanisedSize;
496+
+ $"The size of the package with the requested changes applied is {FormatUtils.HumaniseSize(packageSizeBytes)}. "
497+
+ $"The maximum allowable size is {FormatUtils.HumaniseSize(allowableSizeBytes)}.");
512498
}
513499
}
514500

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
namespace osu.Server.BeatmapSubmission
5+
{
6+
public class FormatUtils
7+
{
8+
public static string HumaniseSize(double sizeBytes)
9+
{
10+
string humanisedSize;
11+
12+
if (sizeBytes < 1024)
13+
humanisedSize = $@"{sizeBytes}B";
14+
else if (sizeBytes < 1024 * 1024)
15+
humanisedSize = $@"{sizeBytes / 1024:#.0}kB";
16+
else
17+
humanisedSize = $@"{sizeBytes / 1024 / 1024:#.0}MB";
18+
19+
return humanisedSize;
20+
}
21+
}
22+
}

osu.Server.BeatmapSubmission/ModelStateValidationFilter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.AspNetCore.Mvc;
55
using Microsoft.AspNetCore.Mvc.Filters;
6+
using osu.Server.BeatmapSubmission.Models.API.Responses;
67

78
namespace osu.Server.BeatmapSubmission
89
{
@@ -28,7 +29,15 @@ public void OnActionExecuting(ActionExecutingContext context)
2829
continue;
2930

3031
foreach (var error in value.Errors)
32+
{
33+
if (string.IsNullOrEmpty(key) && (error.ErrorMessage.Contains("Request body too large") || error.ErrorMessage.Contains("Multipart body length limit")))
34+
{
35+
context.Result = new ErrorResponse($"Request body too large. Size must be lower than {FormatUtils.HumaniseSize(Program.ABSOLUTE_REQUEST_SIZE_LIMIT_BYTES)}.").ToActionResult();
36+
return;
37+
}
38+
3139
errorList.Add($"{{ field: \"{key}\", message: \"{error.ErrorMessage}\", exception: \"{error.Exception}\" }}");
40+
}
3241
}
3342

3443
logger.LogError($"""

0 commit comments

Comments
 (0)