Skip to content

Commit e466127

Browse files
authored
Merge pull request #4276 from paulbalandan/verbose-db-error-output
Use verbose DB output for errors
2 parents a5c0800 + 6214b4f commit e466127

File tree

4 files changed

+32
-41
lines changed

4 files changed

+32
-41
lines changed

app/Views/errors/html/error_404.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
<p>
7676
<?php if (! empty($message) && $message !== '(null)') : ?>
77-
<?= esc($message) ?>
77+
<?= esc(nl2br($message)) ?>
7878
<?php else : ?>
7979
Sorry! Cannot seem to find the page you were looking for.
8080
<?php endif ?>

app/Views/errors/html/error_exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<div class="container">
2222
<h1><?= esc($title), esc($exception->getCode() ? ' #' . $exception->getCode() : '') ?></h1>
2323
<p>
24-
<?= esc($exception->getMessage()) ?>
24+
<?= esc(nl2br($exception->getMessage())) ?>
2525
<a href="https://www.duckduckgo.com/?q=<?= urlencode($title . ' ' . preg_replace('#\'.*\'|".*"#Us', '', $exception->getMessage())) ?>"
2626
rel="noreferrer" target="_blank">search &rarr;</a>
2727
</p>

system/Database/BaseConnection.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ public function initialize()
342342
//--------------------------------------------------------------------
343343

344344
$this->connectTime = microtime(true);
345+
$connectionErrors = [];
345346

346347
try
347348
{
@@ -350,6 +351,7 @@ public function initialize()
350351
}
351352
catch (Throwable $e)
352353
{
354+
$connectionErrors[] = sprintf('Main connection [%s]: %s', $this->DBDriver, $e->getMessage());
353355
log_message('error', 'Error connecting to the database: ' . $e->getMessage());
354356
}
355357

@@ -360,7 +362,7 @@ public function initialize()
360362
if (! empty($this->failover) && is_array($this->failover))
361363
{
362364
// Go over all the failovers
363-
foreach ($this->failover as $failover)
365+
foreach ($this->failover as $index => $failover)
364366
{
365367
// Replace the current settings with those of the failover
366368
foreach ($failover as $key => $val)
@@ -378,6 +380,7 @@ public function initialize()
378380
}
379381
catch (Throwable $e)
380382
{
383+
$connectionErrors[] = sprintf('Failover #%d [%s]: %s', ++$index, $this->DBDriver, $e->getMessage());
381384
log_message('error', 'Error connecting to the database: ' . $e->getMessage());
382385
}
383386

@@ -392,7 +395,11 @@ public function initialize()
392395
// We still don't have a connection?
393396
if (! $this->connID)
394397
{
395-
throw new DatabaseException('Unable to connect to the database.');
398+
throw new DatabaseException(sprintf(
399+
'Unable to connect to the database.%s%s',
400+
PHP_EOL,
401+
implode(PHP_EOL, $connectionErrors)
402+
));
396403
}
397404
}
398405

tests/system/Database/BaseConnectionTest.php

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
<?php namespace CodeIgniter\Database;
1+
<?php
22

3+
namespace CodeIgniter\Database;
4+
5+
use CodeIgniter\Database\Exceptions\DatabaseException;
6+
use CodeIgniter\Test\CIUnitTestCase;
37
use CodeIgniter\Test\Mock\MockConnection;
8+
use Throwable;
49

5-
class BaseConnectionTest extends \CodeIgniter\Test\CIUnitTestCase
10+
class BaseConnectionTest extends CIUnitTestCase
611
{
712
protected $options = [
813
'DSN' => '',
@@ -42,8 +47,6 @@ class BaseConnectionTest extends \CodeIgniter\Test\CIUnitTestCase
4247
'failover' => [],
4348
];
4449

45-
//--------------------------------------------------------------------
46-
4750
public function testSavesConfigOptions()
4851
{
4952
$db = new MockConnection($this->options);
@@ -64,94 +67,75 @@ public function testSavesConfigOptions()
6467
$this->assertSame([], $db->failover);
6568
}
6669

67-
//--------------------------------------------------------------------
68-
6970
public function testConnectionThrowExceptionWhenCannotConnect()
7071
{
71-
$db = new MockConnection($this->options);
72-
73-
$this->expectException('\CodeIgniter\Database\Exceptions\DatabaseException');
74-
$this->expectExceptionMessage('Unable to connect to the database.');
75-
76-
$db->shouldReturn('connect', false)
77-
->initialize();
72+
try
73+
{
74+
$db = new MockConnection($this->options);
75+
$db->shouldReturn('connect', false)->initialize();
76+
}
77+
catch (Throwable $e)
78+
{
79+
$this->assertInstanceOf(DatabaseException::class, $e);
80+
$this->assertStringContainsString('Unable to connect to the database.', $e->getMessage());
81+
}
7882
}
7983

80-
//--------------------------------------------------------------------
81-
8284
public function testCanConnectAndStoreConnection()
8385
{
8486
$db = new MockConnection($this->options);
85-
86-
$db->shouldReturn('connect', 123)
87-
->initialize();
87+
$db->shouldReturn('connect', 123)->initialize();
8888

8989
$this->assertSame(123, $db->getConnection());
9090
}
9191

92-
//--------------------------------------------------------------------
93-
9492
/**
95-
* @throws \CodeIgniter\Database\Exceptions\DatabaseException
96-
* @group single
93+
* @group single
9794
*/
9895
public function testCanConnectToFailoverWhenNoConnectionAvailable()
9996
{
10097
$options = $this->options;
10198
$options['failover'] = [$this->failoverOptions];
10299

103100
$db = new MockConnection($options);
104-
105-
$db->shouldReturn('connect', [false, 345])
106-
->initialize();
101+
$db->shouldReturn('connect', [false, 345])->initialize();
107102

108103
$this->assertSame(345, $db->getConnection());
109104
$this->assertSame('failover', $db->username);
110105
}
111106

112-
//--------------------------------------------------------------------
113-
114107
public function testStoresConnectionTimings()
115108
{
116109
$start = microtime(true);
117110

118111
$db = new MockConnection($this->options);
119-
120112
$db->initialize();
121113

122114
$this->assertGreaterThan($start, $db->getConnectStart());
123115
$this->assertGreaterThan(0.0, $db->getConnectDuration());
124116
}
125117

126-
//--------------------------------------------------------------------
127-
128118
public function testMagicIssetTrue()
129119
{
130120
$db = new MockConnection($this->options);
131121

132122
$this->assertTrue(isset($db->charset));
133123
}
134124

135-
//--------------------------------------------------------------------
136-
137125
public function testMagicIssetFalse()
138126
{
139127
$db = new MockConnection($this->options);
140128

141129
$this->assertFalse(isset($db->foobar));
142130
}
143131

144-
//--------------------------------------------------------------------
145-
146132
public function testMagicGet()
147133
{
148134
$db = new MockConnection($this->options);
149135

150-
$this->assertEquals('utf8', $db->charset);
136+
$this->assertSame('utf8', $db->charset);
151137
}
152138

153-
//--------------------------------------------------------------------
154-
155139
public function testMagicGetMissing()
156140
{
157141
$db = new MockConnection($this->options);

0 commit comments

Comments
 (0)