mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-23 08:40:56 -04:00
API Error handling improvements (#1141)
* generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * generic catch all HTTP error handler for API * Client not found error handling via API * Client not found error handling via API
This commit is contained in:
parent
3ccb33ec21
commit
f5e18f3355
@ -1,5 +1,7 @@
|
|||||||
<?php namespace App\Exceptions;
|
<?php namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Braintree\Util;
|
||||||
|
use Illuminate\Support\Facades\Response;
|
||||||
use Redirect;
|
use Redirect;
|
||||||
use Utils;
|
use Utils;
|
||||||
use Exception;
|
use Exception;
|
||||||
@ -67,9 +69,7 @@ class Handler extends ExceptionHandler
|
|||||||
*/
|
*/
|
||||||
public function render($request, Exception $e)
|
public function render($request, Exception $e)
|
||||||
{
|
{
|
||||||
if ($e instanceof ModelNotFoundException) {
|
if ($e instanceof \Illuminate\Session\TokenMismatchException) {
|
||||||
return Redirect::to('/');
|
|
||||||
} elseif ($e instanceof \Illuminate\Session\TokenMismatchException) {
|
|
||||||
// prevent loop since the page auto-submits
|
// prevent loop since the page auto-submits
|
||||||
if ($request->path() != 'get_started') {
|
if ($request->path() != 'get_started') {
|
||||||
// https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e
|
// https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e
|
||||||
@ -82,6 +82,40 @@ class Handler extends ExceptionHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($this->isHttpException($e))
|
||||||
|
{
|
||||||
|
switch ($e->getStatusCode())
|
||||||
|
{
|
||||||
|
// not found
|
||||||
|
case 404:
|
||||||
|
if($request->header('X-Ninja-Token') != '') {
|
||||||
|
//API request which has hit a route which does not exist
|
||||||
|
|
||||||
|
$error['error'] = ['message'=>'Route does not exist'];
|
||||||
|
$error = json_encode($error, JSON_PRETTY_PRINT);
|
||||||
|
$headers = Utils::getApiHeaders();
|
||||||
|
|
||||||
|
return response()->make($error, 404, $headers);
|
||||||
|
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
// internal error
|
||||||
|
case '500':
|
||||||
|
if($request->header('X-Ninja-Token') != '') {
|
||||||
|
//API request which produces 500 error
|
||||||
|
|
||||||
|
$error['error'] = ['message'=>'Internal Server Error'];
|
||||||
|
$error = json_encode($error, JSON_PRETTY_PRINT);
|
||||||
|
$headers = Utils::getApiHeaders();
|
||||||
|
|
||||||
|
return response()->make($error, 500, $headers);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// In production, except for maintenance mode, we'll show a custom error screen
|
// In production, except for maintenance mode, we'll show a custom error screen
|
||||||
if (Utils::isNinjaProd()
|
if (Utils::isNinjaProd()
|
||||||
&& !Utils::isDownForMaintenance()
|
&& !Utils::isDownForMaintenance()
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
<?php namespace App\Http\Requests;
|
<?php namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
use Input;
|
use Input;
|
||||||
use Utils;
|
use Utils;
|
||||||
use App\Libraries\HistoryUtils;
|
use App\Libraries\HistoryUtils;
|
||||||
@ -36,10 +38,26 @@ class EntityRequest extends Request {
|
|||||||
|
|
||||||
$class = Utils::getEntityClass($this->entityType);
|
$class = Utils::getEntityClass($this->entityType);
|
||||||
|
|
||||||
if (method_exists($class, 'trashed')) {
|
try {
|
||||||
$this->entity = $class::scope($publicId)->withTrashed()->firstOrFail();
|
|
||||||
} else {
|
if (method_exists($class, 'trashed')) {
|
||||||
$this->entity = $class::scope($publicId)->firstOrFail();
|
$this->entity = $class::scope($publicId)->withTrashed()->firstOrFail();
|
||||||
|
} else {
|
||||||
|
$this->entity = $class::scope($publicId)->firstOrFail();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch(ModelNotFoundException $e) {
|
||||||
|
|
||||||
|
if(Request::header('X-Ninja-Token') != '') {
|
||||||
|
|
||||||
|
$error['error'] = ['message'=>trans('texts.client_not_found')];
|
||||||
|
$error = json_encode($error, JSON_PRETTY_PRINT);
|
||||||
|
$headers = Utils::getApiHeaders();
|
||||||
|
|
||||||
|
return response()->make($error, 400, $headers);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->entity;
|
return $this->entity;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user