Skip to content

Commit 667d75e

Browse files
authored
hide error details from serialization when debug is off (#109)
1 parent bc19424 commit 667d75e

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

plugins/exception-render/src/MixerApiExceptionRenderer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public function render(): ResponseInterface
7575
'exception' => (new ReflectionClass($exception))->getShortName(),
7676
'message' => $message,
7777
'url' => h($url),
78-
'error' => $exception,
7978
'code' => $code,
79+
'error' => $exception,
8080
];
8181

8282
if ($this->error instanceof ValidationException) {
@@ -128,6 +128,8 @@ public function getError(): Throwable
128128
private function debugViewVars($exception, array $viewVars): array
129129
{
130130
if (!Configure::read('debug')) {
131+
unset($viewVars['error']);
132+
131133
return $viewVars;
132134
}
133135

plugins/exception-render/tests/TestCase/ControllerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public function test_validation_exception(): void
6060
$message = reset($violation->messages);
6161
$this->assertEquals('_empty', $message->rule);
6262
$this->assertEquals('This field cannot be left empty', $message->message);
63+
64+
$this->assertTrue(isset($object->trace));
65+
$this->assertTrue(isset($object->error));
6366
}
6467

6568
public function test_validation_exception_with_cakephp_debug_off(): void
@@ -77,5 +80,6 @@ public function test_validation_exception_with_cakephp_debug_off(): void
7780
$object = json_decode($body);
7881

7982
$this->assertFalse(isset($object->trace));
83+
$this->assertFalse(isset($object->error));
8084
}
8185
}

plugins/exception-render/tests/test_app/src/Controller/ActorsController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace MixerApi\ExceptionRender\Test\App\Controller;
55

6+
use Cake\Http\Exception\InternalErrorException;
7+
68
class ActorsController extends AppController
79
{
810
/**
@@ -106,4 +108,9 @@ public function delete($id = null)
106108
}
107109
throw new \Exception("Record not deleted");
108110
}
111+
112+
public function exception()
113+
{
114+
throw new InternalErrorException('testing');
115+
}
109116
}

0 commit comments

Comments
 (0)