11<?php
2- /**
3- * Pterodactyl - Panel
4- * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5- *
6- * This software is licensed under the terms of the MIT license.
7- * https://opensource.org/licenses/MIT
8- */
92
103namespace Pterodactyl \Exceptions ;
114
125use Log ;
136use Throwable ;
7+ use Illuminate \Http \Response ;
8+ use Prologue \Alerts \AlertsMessageBag ;
149
1510class DisplayException extends PterodactylException
1611{
12+ const LEVEL_DEBUG = 'debug ' ;
13+ const LEVEL_INFO = 'info ' ;
1714 const LEVEL_WARNING = 'warning ' ;
1815 const LEVEL_ERROR = 'error ' ;
1916
@@ -32,13 +29,13 @@ class DisplayException extends PterodactylException
3229 */
3330 public function __construct ($ message , Throwable $ previous = null , $ level = self ::LEVEL_ERROR , $ code = 0 )
3431 {
35- $ this -> level = $ level ;
32+ parent :: __construct ( $ message , $ code , $ previous ) ;
3633
3734 if (! is_null ($ previous )) {
3835 Log::{$ level }($ previous );
3936 }
4037
41- parent :: __construct ( $ message , $ code , $ previous ) ;
38+ $ this -> level = $ level ;
4239 }
4340
4441 /**
@@ -48,4 +45,33 @@ public function getErrorLevel()
4845 {
4946 return $ this ->level ;
5047 }
48+
49+ /**
50+ * @return int
51+ */
52+ public function getStatusCode ()
53+ {
54+ return Response::HTTP_BAD_REQUEST ;
55+ }
56+
57+ /**
58+ * Render the exception to the user by adding a flashed message to the session
59+ * and then redirecting them back to the page that they came from. If the
60+ * request originated from an API hit, return the error in JSONAPI spec format.
61+ *
62+ * @param \Illuminate\Http\Request $request
63+ * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
64+ */
65+ public function render ($ request )
66+ {
67+ if ($ request ->expectsJson ()) {
68+ return response ()->json (Handler::convertToArray ($ this , [
69+ 'detail ' => $ this ->getMessage (),
70+ ]), method_exists ($ this , 'getStatusCode ' ) ? $ this ->getStatusCode () : Response::HTTP_BAD_REQUEST );
71+ }
72+
73+ app ()->make (AlertsMessageBag::class)->danger ($ this ->getMessage ())->flash ();
74+
75+ return redirect ()->back ()->withInput ();
76+ }
5177}
0 commit comments