More work on the API utilizing Laravel 5.5 exception rendering
Also corrects API format to maintain JSONAPI spec
This commit is contained in:
parent
f30f4b45ba
commit
54b6fb5ebd
28 changed files with 464 additions and 391 deletions
|
@ -11,9 +11,12 @@ namespace Pterodactyl\Exceptions;
|
|||
|
||||
use Log;
|
||||
use Throwable;
|
||||
use Prologue\Alerts\AlertsMessageBag;
|
||||
|
||||
class DisplayException extends PterodactylException
|
||||
{
|
||||
const LEVEL_DEBUG = 'debug';
|
||||
const LEVEL_INFO = 'info';
|
||||
const LEVEL_WARNING = 'warning';
|
||||
const LEVEL_ERROR = 'error';
|
||||
|
||||
|
@ -32,13 +35,13 @@ class DisplayException extends PterodactylException
|
|||
*/
|
||||
public function __construct($message, Throwable $previous = null, $level = self::LEVEL_ERROR, $code = 0)
|
||||
{
|
||||
$this->level = $level;
|
||||
parent::__construct($message, $code, $previous);
|
||||
|
||||
if (! is_null($previous)) {
|
||||
Log::{$level}($previous);
|
||||
}
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->level = $level;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,4 +51,25 @@ class DisplayException extends PterodactylException
|
|||
{
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the exception to the user by adding a flashed message to the session
|
||||
* and then redirecting them back to the page that they came from. If the
|
||||
* request originated from an API hit, return the error in JSONAPI spec format.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function render($request)
|
||||
{
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json(Handler::convertToArray($this, [
|
||||
'detail' => $this->getMessage(),
|
||||
]), 500);
|
||||
}
|
||||
|
||||
app()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash();
|
||||
|
||||
return redirect()->back()->withInput();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue