1
1
mirror of https://github.com/pterodactyl/panel.git synced 2024-11-26 02:52:30 +01:00

Clean up exception handling code, closes #81

Makes sure things get logged properly.
This commit is contained in:
Dane Everitt 2016-09-07 16:12:06 -04:00
parent e0bff4db8e
commit 228d6b1b21
6 changed files with 96 additions and 136 deletions

View File

@ -2,6 +2,8 @@
namespace Pterodactyl\Exceptions; namespace Pterodactyl\Exceptions;
use Log;
use Exception; use Exception;
use DisplayException; use DisplayException;
use DisplayValidationException; use DisplayValidationException;
@ -46,39 +48,18 @@ class Handler extends ExceptionHandler
* @param \Exception $e * @param \Exception $e
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function render($request, Exception $e) public function render($request, Exception $exception)
{ {
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
if ($request->isXmlHttpRequest() || $request->ajax() || $request->is('remote/*')) { if ($request->isXmlHttpRequest() || $request->ajax() || $request->is('remote/*')) {
$response = response()->json([
$exception = 'An exception occured while attempting to perform this action, please try again.'; 'error' => ($exception instanceof DisplayException) ? $exception->getMessage() : 'An unhandled error occured while attempting to process this request.'
if ($e instanceof DisplayException) {
$exception = $e->getMessage();
}
// Live environment, just return a nice error.
if(!env('APP_DEBUG', false)) {
return response()->json([
'error' => $exception
], 500);
}
// If we are debugging, return the exception in it's full manner.
return response()->json([
'error' => (empty($e->getMessage())) ? $exception : $e->getMessage(),
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace(),
], 500); ], 500);
// parent::render() will log it, we are bypassing it in this case.
Log::error($exception);
} }
return parent::render($request, $e); return (isset($response)) ? $response : parent::render($request, $e);
} }
/** /**

View File

@ -30,6 +30,8 @@ use DB;
use Pterodactyl\Models; use Pterodactyl\Models;
use Pterodactyl\Repositories\NodeRepository; use Pterodactyl\Repositories\NodeRepository;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -79,9 +81,9 @@ class NodesController extends Controller
return redirect()->route('admin.nodes.view', [ return redirect()->route('admin.nodes.view', [
'id' => $new 'id' => $new
]); ]);
} catch (\Pterodactyl\Exceptions\DisplayValidationException $e) { } catch (DisplayValidationException $e) {
return redirect()->route('admin.nodes.new')->withErrors(json_decode($e->getMessage()))->withInput(); return redirect()->route('admin.nodes.new')->withErrors(json_decode($e->getMessage()))->withInput();
} catch (\Pterodactyl\Exceptions\DisplayException $e) { } catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash(); Alert::danger($e->getMessage())->flash();
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error($e); Log::error($e);
@ -134,9 +136,9 @@ class NodesController extends Controller
'id' => $id, 'id' => $id,
'tab' => 'tab_settings' 'tab' => 'tab_settings'
]); ]);
} catch (\Pterodactyl\Exceptions\DisplayValidationException $e) { } catch (DisplayValidationException $e) {
return redirect()->route('admin.nodes.view', $id)->withErrors(json_decode($e->getMessage()))->withInput(); return redirect()->route('admin.nodes.view', $id)->withErrors(json_decode($e->getMessage()))->withInput();
} catch (\Pterodactyl\Exceptions\DisplayException $e) { } catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash(); Alert::danger($e->getMessage())->flash();
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error($e); Log::error($e);
@ -194,12 +196,12 @@ class NodesController extends Controller
try { try {
if(empty($processedData)) { if(empty($processedData)) {
throw new \Pterodactyl\Exceptions\DisplayException('It seems that no data was passed to this function.'); throw new DisplayException('It seems that no data was passed to this function.');
} }
$node = new NodeRepository; $node = new NodeRepository;
$node->addAllocations($id, $processedData); $node->addAllocations($id, $processedData);
Alert::success('Successfully added new allocations to this node.')->flash(); Alert::success('Successfully added new allocations to this node.')->flash();
} catch (\Pterodactyl\Exceptions\DisplayException $e) { } catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash(); Alert::danger($e->getMessage())->flash();
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error($e); Log::error($e);

View File

@ -121,25 +121,18 @@ class ServersController extends Controller
{ {
try { try {
$server = new ServerRepository; $server = new ServerRepository;
$response = $server->create($request->all()); $response = $server->create($request->all());
return redirect()->route('admin.servers.view', [ 'id' => $response ]); return redirect()->route('admin.servers.view', [ 'id' => $response ]);
} catch (DisplayValidationException $ex) {
} catch (\Exception $e) { return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
if ($e instanceof \Pterodactyl\Exceptions\DisplayValidationException) { Alert::danger($ex->getMessage())->flash();
return redirect()->route('admin.servers.new')->withErrors(json_decode($e->getMessage()))->withInput(); return redirect()->route('admin.servers.new')->withInput();
} else if ($e instanceof \Pterodactyl\Exceptions\DisplayException) { } catch (\Exception $ex) {
Alert::danger($e->getMessage())->flash(); Log::error($ex);
} else { Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
Debugbar::addException($e);
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
}
return redirect()->route('admin.servers.new')->withInput(); return redirect()->route('admin.servers.new')->withInput();
} }
} }
@ -259,27 +252,26 @@ class ServersController extends Controller
'id' => $id, 'id' => $id,
'tab' => 'tab_details' 'tab' => 'tab_details'
]); ]);
} catch (DisplayValidationException $ex) {
} catch (\Exception $e) { return redirect()->route('admin.servers.view', [
'id' => $id,
if ($e instanceof \Pterodactyl\Exceptions\DisplayValidationException) { 'tab' => 'tab_details'
return redirect()->route('admin.servers.view', [ ])->withErrors(json_decode($ex->getMessage()))->withInput();
'id' => $id, } catch (DisplayException $ex) {
'tab' => 'tab_details' Alert::danger($ex->getMessage())->flash();
])->withErrors(json_decode($e->getMessage()))->withInput(); return redirect()->route('admin.servers.view', [
} else if ($e instanceof \Pterodactyl\Exceptions\DisplayException) { 'id' => $id,
Alert::danger($e->getMessage())->flash(); 'tab' => 'tab_details'
} else { ])->withInput();
Log::error($e); } catch (\Exception $ex) {
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash(); Log::error($ex);
} Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
return redirect()->route('admin.servers.view', [ return redirect()->route('admin.servers.view', [
'id' => $id, 'id' => $id,
'tab' => 'tab_details' 'tab' => 'tab_details'
])->withInput(); ])->withInput();
} }
} }
public function postUpdateServerToggleBuild(Request $request, $id) { public function postUpdateServerToggleBuild(Request $request, $id) {
@ -321,20 +313,22 @@ class ServersController extends Controller
'cpu' => $request->input('cpu'), 'cpu' => $request->input('cpu'),
]); ]);
Alert::success('Server details were successfully updated.')->flash(); Alert::success('Server details were successfully updated.')->flash();
} catch (\Exception $e) { } catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.view', [
if ($e instanceof \Pterodactyl\Exceptions\DisplayValidationException) { 'id' => $id,
return redirect()->route('admin.servers.view', [ 'tab' => 'tab_build'
'id' => $id, ])->withErrors(json_decode($ex->getMessage()))->withInput();
'tab' => 'tab_build' } catch (DisplayException $ex) {
])->withErrors(json_decode($e->getMessage()))->withInput(); Alert::danger($ex->getMessage())->flash();
} else if ($e instanceof \Pterodactyl\Exceptions\DisplayException) { return redirect()->route('admin.servers.view', [
Alert::danger($e->getMessage())->flash(); 'id' => $id,
} else { 'tab' => 'tab_build'
Log::error($e); ]);
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash(); } catch (\Exception $ex) {
} Log::error($ex);
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
} }
return redirect()->route('admin.servers.view', [ return redirect()->route('admin.servers.view', [
'id' => $id, 'id' => $id,
'tab' => 'tab_build' 'tab' => 'tab_build'
@ -348,16 +342,17 @@ class ServersController extends Controller
$server->deleteServer($id, $force); $server->deleteServer($id, $force);
Alert::success('Server was successfully deleted from the panel and the daemon.')->flash(); Alert::success('Server was successfully deleted from the panel and the daemon.')->flash();
return redirect()->route('admin.servers'); return redirect()->route('admin.servers');
} catch (\Pterodactyl\Exceptions\DisplayException $e) { } catch (DisplayException $ex) {
Alert::danger($e->getMessage())->flash(); Alert::danger($ex->getMessage())->flash();
} catch(\Exception $e) { } catch(\Exception $ex) {
Log::error($e); Log::error($ex);
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash(); Alert::danger('An unhandled exception occured while attemping to delete this server. Please try again.')->flash();
} finally {
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_delete'
]);
} }
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_delete'
]);
} }
public function postToggleInstall(Request $request, $id) public function postToggleInstall(Request $request, $id)
@ -366,7 +361,7 @@ class ServersController extends Controller
$server = new ServerRepository; $server = new ServerRepository;
$server->toggleInstall($id); $server->toggleInstall($id);
Alert::success('Server status was successfully toggled.')->flash(); Alert::success('Server status was successfully toggled.')->flash();
} catch (\Pterodactyl\Exceptions\DisplayException $ex) { } catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash(); Alert::danger($ex->getMessage())->flash();
} catch(\Exception $ex) { } catch(\Exception $ex) {
Log::error($ex); Log::error($ex);
@ -408,7 +403,7 @@ class ServersController extends Controller
'_token' '_token'
])); ]));
Alert::success('Added new database to this server.')->flash(); Alert::success('Added new database to this server.')->flash();
} catch (\Pterodactyl\Exceptions\DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.view', [ return redirect()->route('admin.servers.view', [
'id' => $id, 'id' => $id,
'tab' => 'tab_database' 'tab' => 'tab_database'
@ -430,7 +425,7 @@ class ServersController extends Controller
$repo = new ServerRepository; $repo = new ServerRepository;
$repo->suspend($id); $repo->suspend($id);
Alert::success('Server has been suspended on the system. All running processes have been stopped and will not be startable until it is un-suspended.'); Alert::success('Server has been suspended on the system. All running processes have been stopped and will not be startable until it is un-suspended.');
} catch (\Pterodactyl\Exceptions\DisplayException $e) { } catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash(); Alert::danger($e->getMessage())->flash();
} catch(\Exception $e) { } catch(\Exception $e) {
Log::error($e); Log::error($e);
@ -449,7 +444,7 @@ class ServersController extends Controller
$repo = new ServerRepository; $repo = new ServerRepository;
$repo->unsuspend($id); $repo->unsuspend($id);
Alert::success('Server has been unsuspended on the system. Access has been re-enabled.'); Alert::success('Server has been unsuspended on the system. Access has been re-enabled.');
} catch (\Pterodactyl\Exceptions\DisplayException $e) { } catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash(); Alert::danger($e->getMessage())->flash();
} catch(\Exception $e) { } catch(\Exception $e) {
Log::error($e); Log::error($e);

View File

@ -24,12 +24,11 @@
namespace Pterodactyl\Http\Controllers\Server; namespace Pterodactyl\Http\Controllers\Server;
use Log; use Log;
use Debugbar;
use Pterodactyl\Models; use Pterodactyl\Models;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Node;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
use Pterodactyl\Repositories; use Pterodactyl\Repositories;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -72,18 +71,17 @@ class AjaxController extends Controller
*/ */
public function getStatus(Request $request, $uuid) public function getStatus(Request $request, $uuid)
{ {
$server = Models\Server::getByUUID($uuid);
$server = Server::getByUUID($uuid);
if (!$server) { if (!$server) {
return response()->json([], 404); return response()->json([], 404);
} }
$client = Node::guzzleRequest($server->node); $client = Models\Node::guzzleRequest($server->node);
try { try {
$res = $client->request('GET', '/server', [ $res = $client->request('GET', '/server', [
'headers' => Server::getGuzzleHeaders($uuid) 'headers' => Models\Server::getGuzzleHeaders($uuid)
]); ]);
if($res->getStatusCode() === 200) { if($res->getStatusCode() === 200) {
return response()->json(json_decode($res->getBody())); return response()->json(json_decode($res->getBody()));
@ -104,7 +102,7 @@ class AjaxController extends Controller
public function postDirectoryList(Request $request, $uuid) public function postDirectoryList(Request $request, $uuid)
{ {
$server = Server::getByUUID($uuid); $server = Models\Server::getByUUID($uuid);
$this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/'); $this->directory = '/' . trim(urldecode($request->input('directory', '/')), '/');
$this->authorize('list-files', $server); $this->authorize('list-files', $server);
@ -128,17 +126,11 @@ class AjaxController extends Controller
try { try {
$directoryContents = $controller->returnDirectoryListing($this->directory); $directoryContents = $controller->returnDirectoryListing($this->directory);
} catch (\Exception $e) { } catch (DisplayException $ex) {
return response($ex->getMessage(), 500);
Debugbar::addException($e); } catch (\Exception $ex) {
$exception = 'An error occured while attempting to load the requested directory, please try again.'; Log::error($ex);
return response('An error occured while attempting to load the requested directory, please try again.', 500);
if ($e instanceof DisplayException) {
$exception = $e->getMessage();
}
return response($exception, 500);
} }
return view('server.files.list', [ return view('server.files.list', [
@ -161,7 +153,7 @@ class AjaxController extends Controller
public function postSaveFile(Request $request, $uuid) public function postSaveFile(Request $request, $uuid)
{ {
$server = Server::getByUUID($uuid); $server = Models\Server::getByUUID($uuid);
$this->authorize('save-files', $server); $this->authorize('save-files', $server);
$controller = new Repositories\Daemon\FileRepository($uuid); $controller = new Repositories\Daemon\FileRepository($uuid);
@ -169,17 +161,11 @@ class AjaxController extends Controller
try { try {
$controller->saveFileContents($request->input('file'), $request->input('contents')); $controller->saveFileContents($request->input('file'), $request->input('contents'));
return response(null, 204); return response(null, 204);
} catch (\Exception $e) { } catch (DisplayException $ex) {
return response($ex->getMessage(), 500);
Debugbar::addException($e); } catch (\Exception $ex) {
$exception = 'An error occured while attempting to save that file, please try again.'; Log::error($ex);
return response('An error occured while attempting to save this file, please try again.', 500);
if ($e instanceof DisplayException) {
$exception = $e->getMessage();
}
return response($exception, 500);
} }
} }
@ -193,7 +179,7 @@ class AjaxController extends Controller
public function postSetConnection(Request $request, $uuid) public function postSetConnection(Request $request, $uuid)
{ {
$server = Server::getByUUID($uuid); $server = Models\Server::getByUUID($uuid);
$allocation = Models\Allocation::findOrFail($server->allocation); $allocation = Models\Allocation::findOrFail($server->allocation);
$this->authorize('set-connection', $server); $this->authorize('set-connection', $server);
@ -210,11 +196,11 @@ class AjaxController extends Controller
'default' => $request->input('connection'), 'default' => $request->input('connection'),
]); ]);
return response('The default connection for this server has been updated. Please be aware that you will need to restart your server for this change to go into effect.'); return response('The default connection for this server has been updated. Please be aware that you will need to restart your server for this change to go into effect.');
} catch (\Pterodactyl\Exceptions\DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
return response()->json([ return response()->json([
'error' => json_decode($ex->getMessage(), true), 'error' => json_decode($ex->getMessage(), true),
], 503); ], 503);
} catch (\Pterodactyl\Exceptions\DisplayException $ex) { } catch (DisplayException $ex) {
return response()->json([ return response()->json([
'error' => $ex->getMessage(), 'error' => $ex->getMessage(),
], 503); ], 503);

View File

@ -25,7 +25,6 @@ namespace Pterodactyl\Http\Controllers\Server;
use Auth; use Auth;
use DB; use DB;
use Debugbar;
use Uuid; use Uuid;
use Alert; use Alert;
use Log; use Log;
@ -133,18 +132,13 @@ class ServerController extends Controller
try { try {
$fileContent = $controller->returnFileContents($file); $fileContent = $controller->returnFileContents($file);
} catch (\Exception $e) { } catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
Debugbar::addException($e); return redirect()->route('server.files.index', $uuid);
$exception = 'An error occured while attempting to load the requested file for editing, please try again.'; } catch (\Exception $ex) {
Log::error($ex);
if ($e instanceof DisplayException) { Alert::danger('An error occured while attempting to load the requested file for editing, please try again.')->flash();
$exception = $e->getMessage();
}
Alert::danger($exception)->flash();
return redirect()->route('server.files.index', $uuid); return redirect()->route('server.files.index', $uuid);
} }
return view('server.files.edit', [ return view('server.files.edit', [

View File

@ -121,6 +121,7 @@ class TaskController extends Controller
$repo->delete($id); $repo->delete($id);
return response()->json([], 204); return response()->json([], 204);
} catch (\Exception $ex) { } catch (\Exception $ex) {
Log::error($ex);
return response()->json([ return response()->json([
'error' => 'A server error occured while attempting to delete this task.' 'error' => 'A server error occured while attempting to delete this task.'
], 503); ], 503);
@ -147,6 +148,7 @@ class TaskController extends Controller
'status' => $resp 'status' => $resp
]); ]);
} catch (\Exception $ex) { } catch (\Exception $ex) {
Log::error($ex);
return response()->json([ return response()->json([
'error' => 'A server error occured while attempting to toggle this task.' 'error' => 'A server error occured while attempting to toggle this task.'
], 503); ], 503);