Fix error handling for large files (and endpoints called as non-json)

This commit is contained in:
Dane Everitt 2020-04-06 21:59:14 -07:00
parent 7f2b477538
commit f9878d842c
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 21 additions and 12 deletions

View file

@ -1,11 +1,4 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Http\Server; namespace Pterodactyl\Exceptions\Http\Server;
@ -13,4 +6,11 @@ use Pterodactyl\Exceptions\DisplayException;
class FileSizeTooLargeException extends DisplayException class FileSizeTooLargeException extends DisplayException
{ {
/**
* FileSizeTooLargeException constructor.
*/
public function __construct()
{
parent::__construct('The file you are attempting to open is too large to view in the file editor.');
}
} }

View file

@ -49,9 +49,7 @@ class DaemonFileRepository extends DaemonRepository
$length = (int) $response->getHeader('Content-Length')[0] ?? 0; $length = (int) $response->getHeader('Content-Length')[0] ?? 0;
if ($notLargerThan && $length > $notLargerThan) { if ($notLargerThan && $length > $notLargerThan) {
throw new FileSizeTooLargeException( throw new FileSizeTooLargeException;
trans('server.files.exceptions.max_size')
);
} }
return $response->getBody()->__toString(); return $response->getBody()->__toString();

View file

@ -172,7 +172,7 @@ return [
| This array includes the MIME filetypes that can be edited via the web. | This array includes the MIME filetypes that can be edited via the web.
*/ */
'files' => [ 'files' => [
'max_edit_size' => env('PTERODACTYL_FILES_MAX_EDIT_SIZE', 50000), 'max_edit_size' => env('PTERODACTYL_FILES_MAX_EDIT_SIZE', 1024 * 512),
'editable' => [ 'editable' => [
'application/json', 'application/json',
'application/javascript', 'application/javascript',

View file

@ -29,7 +29,18 @@ export default http;
*/ */
export function httpErrorToHuman (error: any): string { export function httpErrorToHuman (error: any): string {
if (error.response && error.response.data) { if (error.response && error.response.data) {
const { data } = error.response; let { data } = error.response;
// Some non-JSON requests can still return the error as a JSON block. In those cases, attempt
// to parse it into JSON so we can display an actual error.
if (typeof data === 'string') {
try {
data = JSON.parse(data);
} catch (e) {
// do nothing, bad json
}
}
if (data.errors && data.errors[0] && data.errors[0].detail) { if (data.errors && data.errors[0] && data.errors[0].detail) {
return data.errors[0].detail; return data.errors[0].detail;
} }