Skip to content

Commit 59da766

Browse files
committed
Uploads: Added user-facing message for Laravel post limit handling
Uploads over the post max size Would previously error without a clean user facing message. This catches that error to provide a user friendly message, compatible with our common error handling. Tested on image manager handling. Added test to cover.
1 parent 21badde commit 59da766

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

app/Exceptions/Handler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Auth\AuthenticationException;
77
use Illuminate\Database\Eloquent\ModelNotFoundException;
88
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
9+
use Illuminate\Http\Exceptions\PostTooLargeException;
910
use Illuminate\Http\JsonResponse;
1011
use Illuminate\Http\Request;
1112
use Illuminate\Validation\ValidationException;
@@ -59,6 +60,10 @@ public function report(Throwable $exception)
5960
*/
6061
public function render($request, Throwable $e)
6162
{
63+
if ($e instanceof PostTooLargeException) {
64+
$e = new NotifyException(trans('errors.server_post_limit'), '/', 413);
65+
}
66+
6267
if ($this->isApiRequest($request)) {
6368
return $this->renderApiException($e);
6469
}
@@ -71,7 +76,7 @@ public function render($request, Throwable $e)
7176
*/
7277
protected function isApiRequest(Request $request): bool
7378
{
74-
return strpos($request->path(), 'api/') === 0;
79+
return str_starts_with($request->path(), 'api/');
7580
}
7681

7782
/**

lang/en/errors.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
'cannot_get_image_from_url' => 'Cannot get image from :url',
4545
'cannot_create_thumbs' => 'The server cannot create thumbnails. Please check you have the GD PHP extension installed.',
4646
'server_upload_limit' => 'The server does not allow uploads of this size. Please try a smaller file size.',
47+
'server_post_limit' => 'The server cannot receive the provided amount of data. Try again with less data or a smaller file.',
4748
'uploaded' => 'The server does not allow uploads of this size. Please try a smaller file size.',
4849

4950
// Drawing & Images

tests/ErrorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests;
44

5+
use Illuminate\Foundation\Http\Middleware\ValidatePostSize;
56
use Illuminate\Support\Facades\Log;
67

78
class ErrorTest extends TestCase
@@ -45,4 +46,16 @@ public function test_access_to_non_existing_image_location_provides_404_response
4546
$resp->assertStatus(404);
4647
$resp->assertSeeText('Image Not Found');
4748
}
49+
50+
public function test_posts_above_php_limit_shows_friendly_error()
51+
{
52+
// Fake super large JSON request
53+
$resp = $this->asEditor()->call('GET', '/books', [], [], [], [
54+
'CONTENT_LENGTH' => '10000000000',
55+
'HTTP_ACCEPT' => 'application/json',
56+
]);
57+
58+
$resp->assertStatus(413);
59+
$resp->assertJson(['error' => 'The server cannot receive the provided amount of data. Try again with less data or a smaller file.']);
60+
}
4861
}

0 commit comments

Comments
 (0)