Skip to content

Commit

Permalink
Merge pull request #31 from open-runtimes/chore-logging-structure
Browse files Browse the repository at this point in the history
Chores: Update logging structure
  • Loading branch information
christyjacob4 authored Sep 12, 2024
2 parents 7e7dd83 + a32b66c commit 1c281f3
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ function logError(Throwable $error, string $action, ?Logger $logger, Route $rout
$route = $utopia->match($request);
logError($error, "httpError", $logger, $route);

$version = (string) Http::getEnv('OPR_PROXY_VERSION') ?: 'UNKNOWN';
$message = $error->getMessage();
$file = $error->getFile();
$line = $error->getLine();
$trace = $error->getTrace();

switch ($error->getCode()) {
case 400: // Error allowed publicly
case 401: // Error allowed publicly
Expand All @@ -500,20 +506,24 @@ function logError(Throwable $error, string $action, ?Logger $logger, Route $rout
$code = 500; // All other errors get the generic 500 server error status code
}

$output = [
'message' => $error->getMessage(),
'code' => $error->getCode(),
'file' => $error->getFile(),
'line' => $error->getLine(),
'trace' => $error->getTrace(),
'version' => Http::getEnv('OPR_PROXY_VERSION', 'UNKNOWN')
$output = ((Http::isDevelopment())) ? [
'message' => $message,
'code' => $code,
'file' => $file,
'line' => $line,
'trace' => \json_encode($trace, JSON_UNESCAPED_UNICODE) === false ? [] : $trace, // check for failing encode
'version' => $version
] : [
'message' => $message,
'code' => $code,
'version' => $version
];

$response
->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
->addHeader('Expires', '0')
->addHeader('Pragma', 'no-cache')
->setStatusCode(\intval($code));
->setStatusCode($code);

$response->json($output);
});
Expand Down

0 comments on commit 1c281f3

Please sign in to comment.