aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarvin Borner2018-08-28 19:49:38 +0200
committerMarvin Borner2018-08-28 19:49:38 +0200
commitd2e2b8499fd46ce128c7886cd219d9311511a18a (patch)
tree39001033ad155a6fd3006d723e1af8c2f3391c17
parent781438fc4cd54c885e52af22e1dc0ccaaace015e (diff)
Better exceptions with disabled debug mode
-rw-r--r--config/optimus.heimdal.php5
-rw-r--r--infrastructure/Exceptions/Formatters/ExceptionFormatter.php30
2 files changed, 33 insertions, 2 deletions
diff --git a/config/optimus.heimdal.php b/config/optimus.heimdal.php
index 504dad3..553efbb 100644
--- a/config/optimus.heimdal.php
+++ b/config/optimus.heimdal.php
@@ -1,7 +1,8 @@
<?php
-use Symfony\Component\HttpKernel\Exception as SymfonyException;
+use Infrastructure\Exceptions\Formatters as LocalFormatters;
use Optimus\Heimdal\Formatters;
+use Symfony\Component\HttpKernel\Exception as SymfonyException;
return [
'add_cors_headers' => false,
@@ -10,7 +11,7 @@ return [
'formatters' => [
SymfonyException\UnprocessableEntityHttpException::class => Formatters\UnprocessableEntityHttpExceptionFormatter::class,
SymfonyException\HttpException::class => Formatters\HttpExceptionFormatter::class,
- Exception::class => Formatters\ExceptionFormatter::class,
+ Exception::class => LocalFormatters\ExceptionFormatter::class,
],
'response_factory' => \Optimus\Heimdal\ResponseFactory::class,
diff --git a/infrastructure/Exceptions/Formatters/ExceptionFormatter.php b/infrastructure/Exceptions/Formatters/ExceptionFormatter.php
new file mode 100644
index 0000000..920ea69
--- /dev/null
+++ b/infrastructure/Exceptions/Formatters/ExceptionFormatter.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Infrastructure\Exceptions\Formatters;
+
+use Exception;
+use Illuminate\Http\JsonResponse;
+use Optimus\Heimdal\Formatters\BaseFormatter;
+
+class ExceptionFormatter extends BaseFormatter
+{
+ public function format(JsonResponse $response, Exception $e, array $reporterResponses)
+ {
+ $response->setStatusCode(500);
+ $data = $response->getData(true);
+
+ if ($this->debug) {
+ $data = array_merge($data, [
+ 'code' => $e->getCode(),
+ 'message' => $e->getMessage(),
+ 'exception' => (string)$e,
+ 'line' => $e->getLine(),
+ 'file' => $e->getFile()
+ ]);
+ } else {
+ $data['message'] = $e->getMessage();
+ }
+
+ $response->setData($data);
+ }
+}