mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-25 05:34:06 -08:00
Properly alert when invalid JSON is submitted to something that wants JSON
This commit is contained in:
parent
ad52f9df72
commit
fb890fbc30
|
@ -7,6 +7,7 @@ use App\Helpers\Helper;
|
|||
use Illuminate\Validation\ValidationException;
|
||||
use Log;
|
||||
use Throwable;
|
||||
use JsonException;
|
||||
|
||||
|
||||
class Handler extends ExceptionHandler
|
||||
|
@ -17,7 +18,7 @@ class Handler extends ExceptionHandler
|
|||
* @var array
|
||||
*/
|
||||
protected $dontReport = [
|
||||
//
|
||||
JsonException::class
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -53,6 +54,12 @@ class Handler extends ExceptionHandler
|
|||
return redirect()->back()->with('error', trans('general.token_expired'));
|
||||
}
|
||||
|
||||
// Invalid JSON exception
|
||||
// TODO: don't understand why we have to do this when we have the invalidJson() method, below, but, well, whatever
|
||||
if ($e instanceof JsonException) {
|
||||
return response()->json(Helper::formatStandardApiResponse('error', null, 'invalid JSON'), 422);
|
||||
}
|
||||
|
||||
|
||||
// Handle Ajax requests that fail because the model doesn't exist
|
||||
if ($request->ajax() || $request->wantsJson()) {
|
||||
|
|
|
@ -9,6 +9,14 @@ abstract class Request extends FormRequest
|
|||
{
|
||||
protected $rules = [];
|
||||
|
||||
public function json($key = null, $default = null)
|
||||
{
|
||||
if ($this->ajax() || $this->wantsJson()) {
|
||||
json_decode($this->getContent(), false, 512, \JSON_THROW_ON_ERROR); // ignore output, just throw
|
||||
}
|
||||
return parent::json($key, $default);
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return $this->rules;
|
||||
|
|
Loading…
Reference in a new issue