Skip to content

Commit 1eb76c4

Browse files
committed
Log more information for PDOExceptions while also keeping passwords out.
1 parent 4b9f025 commit 1eb76c4

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1111
* Fixes database naming scheme using `d###_` rather than `s###_` when creating server databases.
1212
* Fix exception thrown when attempting to update an existing database host.
1313

14+
### Changed
15+
* Adjusted exception handler behavior to log more stack information for PDO exceptions while not exposing credentials.
16+
1417
## v0.7.0 (Derelict Dermodactylus)
1518
### Fixed
1619
* `[rc.2]` — Fixes bad API behavior on `/user` routes.

app/Exceptions/Handler.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ class Handler extends ExceptionHandler
3232
ValidationException::class,
3333
];
3434

35+
/**
36+
* A list of exceptions that should be logged with cleaned stack
37+
* traces to avoid exposing credentials or other sensitive information.
38+
*
39+
* @var array
40+
*/
41+
protected $cleanStacks = [
42+
PDOException::class,
43+
];
44+
3545
/**
3646
* A list of the inputs that are never flashed for validation exceptions.
3747
*
@@ -73,7 +83,40 @@ public function report(Exception $exception)
7383
throw $exception;
7484
}
7585

76-
return $logger->error($exception instanceof PDOException ? $exception->getMessage() : $exception);
86+
foreach ($this->cleanStacks as $class) {
87+
if ($exception instanceof $class) {
88+
$exception = $this->generateCleanedExceptionStack($exception);
89+
break;
90+
}
91+
}
92+
93+
return $logger->error($exception);
94+
}
95+
96+
private function generateCleanedExceptionStack(Exception $exception)
97+
{
98+
$cleanedStack = '';
99+
foreach ($exception->getTrace() as $index => $item) {
100+
$cleanedStack .= sprintf(
101+
"#%d %s(%d): %s%s%s\n",
102+
$index,
103+
array_get($item, 'file'),
104+
array_get($item, 'line'),
105+
array_get($item, 'class'),
106+
array_get($item, 'type'),
107+
array_get($item, 'function')
108+
);
109+
}
110+
111+
$message = sprintf(
112+
'%s: %s in %s:%d',
113+
class_basename($exception),
114+
$exception->getMessage(),
115+
$exception->getFile(),
116+
$exception->getLine()
117+
);
118+
119+
return $message . "\nStack trace:\n" . trim($cleanedStack);
77120
}
78121

79122
/**

0 commit comments

Comments
 (0)