Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default exception handler #54

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,21 @@ public function execute(Route $route, Request $request): self
}
}
} catch (\Throwable $e) {
if (empty(self::$errors['*'])) { // If no error handler is set then add a default one.
self::$errors['*'][] = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no longer $errors['*'] please check the new implementation of errors.

'callback' => function (\Throwable $error, Response $response) {
$response->setStatusCode(500);
$response->json([
'message' => $error->getMessage(),
'stacktrace' => $error->getTrace()
]);
\fwrite(STDERR, "\033[31mException: " . $error->getMessage() . "\033[0m\n");
\fwrite(STDERR, "Stacktrace: \n" . $error->getTraceAsString() . "\n");
PineappleIOnic marked this conversation as resolved.
Show resolved Hide resolved
},
'resources' => ['error', 'response']
];
}

foreach ($groups as $group) {
PineappleIOnic marked this conversation as resolved.
Show resolved Hide resolved
foreach (self::$errors as $error) { // Group error hooks
/** @var Hook $error */
Expand Down
4 changes: 0 additions & 4 deletions tests/e2e/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ public function call(string $method, string $path = '', array $headers = [], arr

$responseHeaders['status-code'] = $responseStatus;

if ($responseStatus === 500) {
echo 'Server error('.$method.': '.$path.'. Params: '.json_encode($params).'): '.json_encode($responseBody)."\n";
}

return [
'headers' => $responseHeaders,
'body' => $responseBody
Expand Down
12 changes: 12 additions & 0 deletions tests/e2e/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ public function testRedirect()
$response = $this->client->call(Client::METHOD_GET, '/redirect');
$this->assertEquals('Hello World!', $response['body']);
}

public function testException()
{
$response = $this->client->call(Client::METHOD_GET, '/exception');
$this->assertEquals(500, $response['headers']['status-code']);
}

public function testHandledException()
{
$response = $this->client->call(Client::METHOD_GET, '/handledException');
$this->assertEquals('Handled Exception.', $response['body']);
}
}
20 changes: 20 additions & 0 deletions tests/e2e/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@
$response->redirect('/');
});

App::get('/exception')
->inject('response')
->action(function($response) {
/** @var Utopia/Response $response */
throw new Exception('Exception!');
});

App::get('/handledException')
->inject('response')
->action(function($response) {
/** @var Utopia/Response $response */

App::error(function ($error, $response) {
/** @var Utopia/Response $response */
$response->send('Handled Exception.');
}, ['error', 'response']);

throw new Exception('Exception!');
});

$request = new Request();
$response = new Response();

Expand Down