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;
use Log;
use Exception;
use DisplayException;
use DisplayValidationException;
@ -46,39 +48,18 @@ class Handler extends ExceptionHandler
* @param \Exception $e
* @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/*')) {
$exception = 'An exception occured while attempting to perform this action, please try again.';
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(),
$response = response()->json([
'error' => ($exception instanceof DisplayException) ? $exception->getMessage() : 'An unhandled error occured while attempting to process this request.'
], 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\Repositories\NodeRepository;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Http\Request;
@ -79,9 +81,9 @@ class NodesController extends Controller
return redirect()->route('admin.nodes.view', [
'id' => $new
]);
} catch (\Pterodactyl\Exceptions\DisplayValidationException $e) {
} catch (DisplayValidationException $e) {
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();
} catch (\Exception $e) {
Log::error($e);
@ -134,9 +136,9 @@ class NodesController extends Controller
'id' => $id,
'tab' => 'tab_settings'
]);
} catch (\Pterodactyl\Exceptions\DisplayValidationException $e) {
} catch (DisplayValidationException $e) {
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();
} catch (\Exception $e) {
Log::error($e);
@ -194,12 +196,12 @@ class NodesController extends Controller
try {
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->addAllocations($id, $processedData);
Alert::success('Successfully added new allocations to this node.')->flash();
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
} catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash();
} catch (\Exception $e) {
Log::error($e);

View file

@ -121,25 +121,18 @@ class ServersController extends Controller
{
try {
$server = new ServerRepository;
$response = $server->create($request->all());
return redirect()->route('admin.servers.view', [ 'id' => $response ]);
} catch (\Exception $e) {
if ($e instanceof \Pterodactyl\Exceptions\DisplayValidationException) {
return redirect()->route('admin.servers.new')->withErrors(json_decode($e->getMessage()))->withInput();
} else if ($e instanceof \Pterodactyl\Exceptions\DisplayException) {
Alert::danger($e->getMessage())->flash();
} else {
Debugbar::addException($e);
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
}
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
return redirect()->route('admin.servers.new')->withInput();
} 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.new')->withInput();
}
}
@ -259,27 +252,26 @@ class ServersController extends Controller
'id' => $id,
'tab' => 'tab_details'
]);
} catch (\Exception $e) {
if ($e instanceof \Pterodactyl\Exceptions\DisplayValidationException) {
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_details'
])->withErrors(json_decode($e->getMessage()))->withInput();
} else if ($e instanceof \Pterodactyl\Exceptions\DisplayException) {
Alert::danger($e->getMessage())->flash();
} else {
Log::error($e);
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
}
])->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_details'
])->withInput();
} 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', [
'id' => $id,
'tab' => 'tab_details'
])->withInput();
}
}
public function postUpdateServerToggleBuild(Request $request, $id) {
@ -321,20 +313,22 @@ class ServersController extends Controller
'cpu' => $request->input('cpu'),
]);
Alert::success('Server details were successfully updated.')->flash();
} catch (\Exception $e) {
if ($e instanceof \Pterodactyl\Exceptions\DisplayValidationException) {
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_build'
])->withErrors(json_decode($e->getMessage()))->withInput();
} else if ($e instanceof \Pterodactyl\Exceptions\DisplayException) {
Alert::danger($e->getMessage())->flash();
} else {
Log::error($e);
])->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_build'
]);
} 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', [
'id' => $id,
'tab' => 'tab_build'
@ -348,17 +342,18 @@ class ServersController extends Controller
$server->deleteServer($id, $force);
Alert::success('Server was successfully deleted from the panel and the daemon.')->flash();
return redirect()->route('admin.servers');
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
Alert::danger($e->getMessage())->flash();
} catch(\Exception $e) {
Log::error($e);
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
}
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch(\Exception $ex) {
Log::error($ex);
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'
]);
}
}
public function postToggleInstall(Request $request, $id)
{
@ -366,7 +361,7 @@ class ServersController extends Controller
$server = new ServerRepository;
$server->toggleInstall($id);
Alert::success('Server status was successfully toggled.')->flash();
} catch (\Pterodactyl\Exceptions\DisplayException $ex) {
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch(\Exception $ex) {
Log::error($ex);
@ -408,7 +403,7 @@ class ServersController extends Controller
'_token'
]));
Alert::success('Added new database to this server.')->flash();
} catch (\Pterodactyl\Exceptions\DisplayValidationException $ex) {
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.servers.view', [
'id' => $id,
'tab' => 'tab_database'
@ -430,7 +425,7 @@ class ServersController extends Controller
$repo = new ServerRepository;
$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.');
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
} catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash();
} catch(\Exception $e) {
Log::error($e);
@ -449,7 +444,7 @@ class ServersController extends Controller
$repo = new ServerRepository;
$repo->unsuspend($id);
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();
} catch(\Exception $e) {
Log::error($e);

View file

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

View file

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

View file

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