1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01: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:
David Bomba 2016-11-05 06:20:33 +11:00 committed by GitHub
parent 3ccb33ec21
commit f5e18f3355
2 changed files with 59 additions and 7 deletions

View File

@ -1,5 +1,7 @@
<?php namespace App\Exceptions;
use Braintree\Util;
use Illuminate\Support\Facades\Response;
use Redirect;
use Utils;
use Exception;
@ -67,9 +69,7 @@ class Handler extends ExceptionHandler
*/
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException) {
return Redirect::to('/');
} elseif ($e instanceof \Illuminate\Session\TokenMismatchException) {
if ($e instanceof \Illuminate\Session\TokenMismatchException) {
// prevent loop since the page auto-submits
if ($request->path() != 'get_started') {
// 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
if (Utils::isNinjaProd()
&& !Utils::isDownForMaintenance()

View File

@ -1,5 +1,7 @@
<?php namespace App\Http\Requests;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Input;
use Utils;
use App\Libraries\HistoryUtils;
@ -36,10 +38,26 @@ class EntityRequest extends Request {
$class = Utils::getEntityClass($this->entityType);
if (method_exists($class, 'trashed')) {
$this->entity = $class::scope($publicId)->withTrashed()->firstOrFail();
} else {
$this->entity = $class::scope($publicId)->firstOrFail();
try {
if (method_exists($class, 'trashed')) {
$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;