Skip to content

Commit

Permalink
Adds new shiny ErrorHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Aug 25, 2016
1 parent 36376f0 commit 999c088
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
12 changes: 3 additions & 9 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
'swagger' => $swagger,
]);

//middleware
// middleware
use Jacobemerick\CommentService\Middleware;

$talus->addMiddleware(new Middleware\Authentication(
Expand All @@ -120,14 +120,8 @@
$talus->addMiddleware(new Middleware\JsonHeader());
$talus->addMiddleware(new Middleware\ParseJsonBody());

$talus->setErrorHandler(function ($req, $res, $e) {
$body = json_encode([
'error' => $e->getMessage(),
]);

$res->getBody()->write($body);
return $res;
});
// error handler
$talus->setErrorHandler(new Jacobemerick\CommentService\ErrorHandler\JsonResponse());

$talus->run();

Expand Down
27 changes: 27 additions & 0 deletions src/ErrorHandler/JsonResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Jacobemerick\CommentService\ErrorHandler;

use Exception;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class JsonResponse
{

/**
* @param Request $req
* @param Response $res
* @param Exception $e
* @returns Response
*/
public function __invoke(Request $req, Response $res, Exception $e)
{
$body = json_encode([
'error' => $e->getMessage(),
]);

$res->getBody()->write($body);
return $res;
}
}

0 comments on commit 999c088

Please sign in to comment.