Skip to content

Commit 228d6b1

Browse files
committed
Clean up exception handling code, closes pterodactyl#81
Makes sure things get logged properly.
1 parent e0bff4d commit 228d6b1

File tree

6 files changed

+96
-136
lines changed

6 files changed

+96
-136
lines changed

app/Exceptions/Handler.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Pterodactyl\Exceptions;
44

5+
use Log;
6+
57
use Exception;
68
use DisplayException;
79
use DisplayValidationException;
@@ -46,39 +48,18 @@ public function report(Exception $exception)
4648
* @param \Exception $e
4749
* @return \Illuminate\Http\Response
4850
*/
49-
public function render($request, Exception $e)
51+
public function render($request, Exception $exception)
5052
{
51-
if ($e instanceof ModelNotFoundException) {
52-
$e = new NotFoundHttpException($e->getMessage(), $e);
53-
}
54-
5553
if ($request->isXmlHttpRequest() || $request->ajax() || $request->is('remote/*')) {
56-
57-
$exception = 'An exception occured while attempting to perform this action, please try again.';
58-
59-
if ($e instanceof DisplayException) {
60-
$exception = $e->getMessage();
61-
}
62-
63-
// Live environment, just return a nice error.
64-
if(!env('APP_DEBUG', false)) {
65-
return response()->json([
66-
'error' => $exception
67-
], 500);
68-
}
69-
70-
// If we are debugging, return the exception in it's full manner.
71-
return response()->json([
72-
'error' => (empty($e->getMessage())) ? $exception : $e->getMessage(),
73-
'code' => $e->getCode(),
74-
'file' => $e->getFile(),
75-
'line' => $e->getLine(),
76-
'trace' => $e->getTrace(),
54+
$response = response()->json([
55+
'error' => ($exception instanceof DisplayException) ? $exception->getMessage() : 'An unhandled error occured while attempting to process this request.'
7756
], 500);
7857

58+
// parent::render() will log it, we are bypassing it in this case.
59+
Log::error($exception);
7960
}
8061

81-
return parent::render($request, $e);
62+
return (isset($response)) ? $response : parent::render($request, $e);
8263
}
8364

8465
/**

app/Http/Controllers/Admin/NodesController.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
use Pterodactyl\Models;
3232
use Pterodactyl\Repositories\NodeRepository;
33+
use Pterodactyl\Exceptions\DisplayException;
34+
use Pterodactyl\Exceptions\DisplayValidationException;
3335

3436
use Pterodactyl\Http\Controllers\Controller;
3537
use Illuminate\Http\Request;
@@ -79,9 +81,9 @@ public function postNew(Request $request)
7981
return redirect()->route('admin.nodes.view', [
8082
'id' => $new
8183
]);
82-
} catch (\Pterodactyl\Exceptions\DisplayValidationException $e) {
84+
} catch (DisplayValidationException $e) {
8385
return redirect()->route('admin.nodes.new')->withErrors(json_decode($e->getMessage()))->withInput();
84-
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
86+
} catch (DisplayException $e) {
8587
Alert::danger($e->getMessage())->flash();
8688
} catch (\Exception $e) {
8789
Log::error($e);
@@ -134,9 +136,9 @@ public function postView(Request $request, $id)
134136
'id' => $id,
135137
'tab' => 'tab_settings'
136138
]);
137-
} catch (\Pterodactyl\Exceptions\DisplayValidationException $e) {
139+
} catch (DisplayValidationException $e) {
138140
return redirect()->route('admin.nodes.view', $id)->withErrors(json_decode($e->getMessage()))->withInput();
139-
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
141+
} catch (DisplayException $e) {
140142
Alert::danger($e->getMessage())->flash();
141143
} catch (\Exception $e) {
142144
Log::error($e);
@@ -194,12 +196,12 @@ public function postAllocations(Request $request, $id)
194196

195197
try {
196198
if(empty($processedData)) {
197-
throw new \Pterodactyl\Exceptions\DisplayException('It seems that no data was passed to this function.');
199+
throw new DisplayException('It seems that no data was passed to this function.');
198200
}
199201
$node = new NodeRepository;
200202
$node->addAllocations($id, $processedData);
201203
Alert::success('Successfully added new allocations to this node.')->flash();
202-
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
204+
} catch (DisplayException $e) {
203205
Alert::danger($e->getMessage())->flash();
204206
} catch (\Exception $e) {
205207
Log::error($e);

app/Http/Controllers/Admin/ServersController.php

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,18 @@ public function postNewServer(Request $request)
121121
{
122122

123123
try {
124-
125124
$server = new ServerRepository;
126125
$response = $server->create($request->all());
127-
128126
return redirect()->route('admin.servers.view', [ 'id' => $response ]);
129-
130-
} catch (\Exception $e) {
131-
132-
if ($e instanceof \Pterodactyl\Exceptions\DisplayValidationException) {
133-
return redirect()->route('admin.servers.new')->withErrors(json_decode($e->getMessage()))->withInput();
134-
} else if ($e instanceof \Pterodactyl\Exceptions\DisplayException) {
135-
Alert::danger($e->getMessage())->flash();
136-
} else {
137-
Debugbar::addException($e);
138-
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
139-
}
140-
127+
} catch (DisplayValidationException $ex) {
128+
return redirect()->route('admin.servers.new')->withErrors(json_decode($ex->getMessage()))->withInput();
129+
} catch (DisplayException $ex) {
130+
Alert::danger($ex->getMessage())->flash();
131+
return redirect()->route('admin.servers.new')->withInput();
132+
} catch (\Exception $ex) {
133+
Log::error($ex);
134+
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
141135
return redirect()->route('admin.servers.new')->withInput();
142-
143136
}
144137

145138
}
@@ -259,27 +252,26 @@ public function postUpdateServerDetails(Request $request, $id)
259252
'id' => $id,
260253
'tab' => 'tab_details'
261254
]);
262-
263-
} catch (\Exception $e) {
264-
265-
if ($e instanceof \Pterodactyl\Exceptions\DisplayValidationException) {
266-
return redirect()->route('admin.servers.view', [
267-
'id' => $id,
268-
'tab' => 'tab_details'
269-
])->withErrors(json_decode($e->getMessage()))->withInput();
270-
} else if ($e instanceof \Pterodactyl\Exceptions\DisplayException) {
271-
Alert::danger($e->getMessage())->flash();
272-
} else {
273-
Log::error($e);
274-
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
275-
}
276-
255+
} catch (DisplayValidationException $ex) {
256+
return redirect()->route('admin.servers.view', [
257+
'id' => $id,
258+
'tab' => 'tab_details'
259+
])->withErrors(json_decode($ex->getMessage()))->withInput();
260+
} catch (DisplayException $ex) {
261+
Alert::danger($ex->getMessage())->flash();
262+
return redirect()->route('admin.servers.view', [
263+
'id' => $id,
264+
'tab' => 'tab_details'
265+
])->withInput();
266+
} catch (\Exception $ex) {
267+
Log::error($ex);
268+
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
277269
return redirect()->route('admin.servers.view', [
278270
'id' => $id,
279271
'tab' => 'tab_details'
280272
])->withInput();
281-
282273
}
274+
283275
}
284276

285277
public function postUpdateServerToggleBuild(Request $request, $id) {
@@ -321,20 +313,22 @@ public function postUpdateServerUpdateBuild(Request $request, $id)
321313
'cpu' => $request->input('cpu'),
322314
]);
323315
Alert::success('Server details were successfully updated.')->flash();
324-
} catch (\Exception $e) {
325-
326-
if ($e instanceof \Pterodactyl\Exceptions\DisplayValidationException) {
327-
return redirect()->route('admin.servers.view', [
328-
'id' => $id,
329-
'tab' => 'tab_build'
330-
])->withErrors(json_decode($e->getMessage()))->withInput();
331-
} else if ($e instanceof \Pterodactyl\Exceptions\DisplayException) {
332-
Alert::danger($e->getMessage())->flash();
333-
} else {
334-
Log::error($e);
335-
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
336-
}
316+
} catch (DisplayValidationException $ex) {
317+
return redirect()->route('admin.servers.view', [
318+
'id' => $id,
319+
'tab' => 'tab_build'
320+
])->withErrors(json_decode($ex->getMessage()))->withInput();
321+
} catch (DisplayException $ex) {
322+
Alert::danger($ex->getMessage())->flash();
323+
return redirect()->route('admin.servers.view', [
324+
'id' => $id,
325+
'tab' => 'tab_build'
326+
]);
327+
} catch (\Exception $ex) {
328+
Log::error($ex);
329+
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
337330
}
331+
338332
return redirect()->route('admin.servers.view', [
339333
'id' => $id,
340334
'tab' => 'tab_build'
@@ -348,16 +342,17 @@ public function deleteServer(Request $request, $id, $force = null)
348342
$server->deleteServer($id, $force);
349343
Alert::success('Server was successfully deleted from the panel and the daemon.')->flash();
350344
return redirect()->route('admin.servers');
351-
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
352-
Alert::danger($e->getMessage())->flash();
353-
} catch(\Exception $e) {
354-
Log::error($e);
355-
Alert::danger('An unhandled exception occured while attemping to add this server. Please try again.')->flash();
345+
} catch (DisplayException $ex) {
346+
Alert::danger($ex->getMessage())->flash();
347+
} catch(\Exception $ex) {
348+
Log::error($ex);
349+
Alert::danger('An unhandled exception occured while attemping to delete this server. Please try again.')->flash();
350+
} finally {
351+
return redirect()->route('admin.servers.view', [
352+
'id' => $id,
353+
'tab' => 'tab_delete'
354+
]);
356355
}
357-
return redirect()->route('admin.servers.view', [
358-
'id' => $id,
359-
'tab' => 'tab_delete'
360-
]);
361356
}
362357

363358
public function postToggleInstall(Request $request, $id)
@@ -366,7 +361,7 @@ public function postToggleInstall(Request $request, $id)
366361
$server = new ServerRepository;
367362
$server->toggleInstall($id);
368363
Alert::success('Server status was successfully toggled.')->flash();
369-
} catch (\Pterodactyl\Exceptions\DisplayException $ex) {
364+
} catch (DisplayException $ex) {
370365
Alert::danger($ex->getMessage())->flash();
371366
} catch(\Exception $ex) {
372367
Log::error($ex);
@@ -408,7 +403,7 @@ public function postDatabase(Request $request, $id)
408403
'_token'
409404
]));
410405
Alert::success('Added new database to this server.')->flash();
411-
} catch (\Pterodactyl\Exceptions\DisplayValidationException $ex) {
406+
} catch (DisplayValidationException $ex) {
412407
return redirect()->route('admin.servers.view', [
413408
'id' => $id,
414409
'tab' => 'tab_database'
@@ -430,7 +425,7 @@ public function postSuspendServer(Request $request, $id)
430425
$repo = new ServerRepository;
431426
$repo->suspend($id);
432427
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.');
433-
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
428+
} catch (DisplayException $e) {
434429
Alert::danger($e->getMessage())->flash();
435430
} catch(\Exception $e) {
436431
Log::error($e);
@@ -449,7 +444,7 @@ public function postUnsuspendServer(Request $request, $id)
449444
$repo = new ServerRepository;
450445
$repo->unsuspend($id);
451446
Alert::success('Server has been unsuspended on the system. Access has been re-enabled.');
452-
} catch (\Pterodactyl\Exceptions\DisplayException $e) {
447+
} catch (DisplayException $e) {
453448
Alert::danger($e->getMessage())->flash();
454449
} catch(\Exception $e) {
455450
Log::error($e);

0 commit comments

Comments
 (0)