Skip to content

Commit 6b13d51

Browse files
committed
Improved error msg handling
1 parent 0442e61 commit 6b13d51

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

contacts/db/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ const { Pool } = require('pg');
55
const pgconn = new Pool({
66
connectionString: DB_CONFIG,
77
ssl: false,
8-
})
8+
});
99

1010
module.exports = { pgconn }

contacts/routes/index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ const { pgconn } = require('../db/config')
66
router.get('/', function(req, res) {
77
pgconn.query('SELECT * FROM contacts', function(err,results) {
88
if (err) {
9-
throw err
9+
console.log(err);
10+
res.render('index', { error: 'Database connection failure! '+err.stack, contacts: null, title: 'Contact List' });
11+
}
12+
else {
13+
let contacts = results.rows;
14+
console.log(contacts);
15+
res.render('index', { error: null, contacts: contacts, title: 'Contact List' });
1016
}
11-
let contacts = results.rows;
12-
console.log(contacts);
13-
res.render('index', { contacts: contacts, title: 'Contact List' });
1417
})
1518
});
1619

contacts/views/index.pug

+21-18
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@ block content
66
h1= title
77
hr
88
br
9-
table.table.table-striped.table-bordered.table-hover
10-
if contacts
11-
thead
12-
tr
13-
th ID
14-
th First Name
15-
th Last Name
16-
th EMail
17-
tbody
18-
for contact in contacts
9+
if error
10+
.alert.alert-danger #{error}
11+
else
12+
table.table.table-striped.table-bordered.table-hover
13+
if contacts
14+
thead
1915
tr
20-
td= contact.id
21-
td= contact.firstname
22-
td= contact.lastname
23-
td= contact.email
24-
else
25-
tr
26-
td
27-
| No contacts found
16+
th ID
17+
th First Name
18+
th Last Name
19+
th EMail
20+
tbody
21+
for contact in contacts
22+
tr
23+
td= contact.id
24+
td= contact.firstname
25+
td= contact.lastname
26+
td= contact.email
27+
else
28+
tr
29+
td
30+
| No contacts found

0 commit comments

Comments
 (0)