|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Exceptions; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use DisplayException; |
| 7 | +use Debugbar; |
| 8 | +use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 9 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 10 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 11 | +use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
| 12 | + |
| 13 | +class Handler extends ExceptionHandler |
| 14 | +{ |
| 15 | + /** |
| 16 | + * A list of the exception types that should not be reported. |
| 17 | + * |
| 18 | + * @var array |
| 19 | + */ |
| 20 | + protected $dontReport = [ |
| 21 | + HttpException::class, |
| 22 | + ModelNotFoundException::class, |
| 23 | + ]; |
| 24 | + |
| 25 | + /** |
| 26 | + * Report or log an exception. |
| 27 | + * |
| 28 | + * This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
| 29 | + * |
| 30 | + * @param \Exception $e |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function report(Exception $e) |
| 34 | + { |
| 35 | + return parent::report($e); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Render an exception into an HTTP response. |
| 40 | + * |
| 41 | + * @param \Illuminate\Http\Request $request |
| 42 | + * @param \Exception $e |
| 43 | + * @return \Illuminate\Http\Response |
| 44 | + */ |
| 45 | + public function render($request, Exception $e) |
| 46 | + { |
| 47 | + if ($e instanceof ModelNotFoundException) { |
| 48 | + $e = new NotFoundHttpException($e->getMessage(), $e); |
| 49 | + } |
| 50 | + |
| 51 | + if ($request->isXmlHttpRequest() || $request->ajax() || $request->is('/api/*')) { |
| 52 | + |
| 53 | + $exception = 'An exception occured while attempting to perform this action, please try again.'; |
| 54 | + |
| 55 | + if ($e instanceof DisplayException) { |
| 56 | + $exception = $e->getMessage(); |
| 57 | + } |
| 58 | + |
| 59 | + // Live environment, just return a nice error. |
| 60 | + if(!env('APP_DEBUG', false)) { |
| 61 | + return response()->json([ |
| 62 | + 'error' => $exception |
| 63 | + ], 500); |
| 64 | + } |
| 65 | + |
| 66 | + // If we are debugging, return the exception in it's full manner. |
| 67 | + return response()->json([ |
| 68 | + 'error' => $e->getMessage(), |
| 69 | + 'code' => $e->getCode(), |
| 70 | + 'file' => $e->getFile(), |
| 71 | + 'line' => $e->getLine() |
| 72 | + ], 500); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + return parent::render($request, $e); |
| 77 | + } |
| 78 | +} |
0 commit comments