Major overhaul

This commit is contained in:
Simon Sessingø
2016-11-19 19:24:05 +01:00
parent ba719cf880
commit 7e63197252
21 changed files with 526 additions and 282 deletions
@@ -1,34 +1,44 @@
<?php
namespace Demo\Handlers;
use Pecee\Handler\IExceptionHandler;
use Pecee\Handlers\IExceptionHandler;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
use Pecee\SimpleRouter\RouterEntry;
class CustomExceptionHandler implements IExceptionHandler
{
public function handleError(Request $request, RouterEntry &$route = null, \Exception $error)
{
// Return json errors if we encounter an error on /api.
/* You can use the exception handler to format errors depending on the request and type. */
if (stripos($request->getUri(), '/api') !== false) {
header('content-type: application/json');
echo json_encode([
response()->json([
'error' => $error->getMessage(),
'code' => $error->getCode()
'code' => $error->getCode(),
]);
die();
}
// else we just throw the error
if ($error->getCode() == 404) {
/* The router will throw the NotFoundHttpException on 404 */
if($error instanceof NotFoundHttpException) {
// Return 404 path
$request->setUri('/404');
/*
* Render your own custom 404-view, rewrite the request to another route,
* or simply return the $request object to ignore the error and continue on rendering the route.
*
* The code below will make the router render our page.notfound route.
*/
$request->setUri(url('page.notfound'));
return $request;
}
throw $error;
}
}