Skip to content

Commit 2ac7597

Browse files
authored
Merge pull request #51 from Srokap/better_error_handling
Better error handling
2 parents 13bc428 + c85f386 commit 2ac7597

File tree

10 files changed

+162
-100
lines changed

10 files changed

+162
-100
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,6 @@ Icon
8484
Network Trash Folder
8585
Temporary Items
8686
.apdisk
87+
88+
# JetBrains IDE project files
89+
/.idea

app.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const compression = require("compression")
66
const helmet = require("helmet")
77
const config = require("./config.json")
88
const MongoClient = require("mongodb")
9-
const error = {error: "No results found"}
109
const app = express()
1110

1211
const home = require("./routes/v1-home")
@@ -39,12 +38,27 @@ app.use("/v1/parts", parts)
3938

4039
// 404 Error Handler
4140
app.use((req, res) => {
42-
res.status(404).end(JSON.stringify(error, null, 2))
41+
res.status(404)
42+
res.json({
43+
error: "No results found"
44+
})
45+
})
46+
47+
// generic error handler - must have 4 parameters
48+
// eslint-disable-next-line no-unused-vars
49+
app.use((err, req, res, next) => {
50+
res.status(500)
51+
res.json({
52+
error: "Internal Server Error"
53+
})
4354
})
4455

4556
// Mongo Connection + Server Start
4657
MongoClient.connect(config.url, (err, database) => {
47-
if (err) return console.log(err)
58+
if (err) {
59+
console.log(err)
60+
process.exit(1)
61+
}
4862
global.db = database
4963

5064
app.set("port", (process.env.PORT || 5000))

routes/v1-home.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const express = require("express")
44
const v1 = express.Router()
55

66
// Returns API Info
7-
v1.get("/", (req, res) => {
7+
v1.get("/", (req, res, next) => {
88
global.db.collection("home").find({},{"_id": 0 }).toArray((err, doc) => {
9-
if (err) return console.log(err)
10-
res.end(JSON.stringify(doc[0], null, 2))
9+
if (err) {
10+
return next(err)
11+
}
12+
res.json(doc[0])
1113
})
1214
})
1315

routes/v1-info.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const express = require("express")
44
const v1 = express.Router()
55

66
// Returns company info
7-
v1.get("/", (req, res) => {
7+
v1.get("/", (req, res, next) => {
88
global.db.collection("info").find({},{"_id": 0 }).toArray((err, doc) => {
9-
if (err) return console.log(err)
10-
res.end(JSON.stringify(doc[0], null, 2))
9+
if (err) {
10+
return next(err)
11+
}
12+
res.json(doc[0])
1113
})
1214
})
1315

routes/v1-launches.js

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,99 +2,95 @@
22

33
const express = require("express")
44
const v1 = express.Router()
5-
const error = {error: "No results found"}
65

76
// Get most recent launch
8-
v1.get("/latest", (req, res) => {
7+
v1.get("/latest", (req, res, next) => {
98
global.db.collection("launch").find({},{"_id": 0 }).sort({"flight_number": -1}).limit(1)
109
.toArray((err, doc) => {
11-
if (err) return console.log(err)
12-
res.end(JSON.stringify(doc, null, 2))
10+
if (err) {
11+
return next(err)
12+
}
13+
res.json(doc)
1314
})
1415
})
1516

1617
// All launches by date, year, or default to all launches
17-
v1.get("/", (req, res) => {
18+
v1.get("/", (req, res, next) => {
1819
const year = req.query.year
1920
const start = req.query.start
2021
const final = req.query.final
2122
const site = req.query.site
2223
if (year) {
2324
global.db.collection("launch").find({"launch_year": `${year}`}, {"_id": 0 }).sort({"flight_number": -1})
2425
.toArray((err, doc) => {
25-
if (doc.length == 0) {
26-
res.status(200).end(JSON.stringify(error, null, 2))
27-
}
28-
res.end(JSON.stringify(doc, null, 2))
26+
res.json(doc)
2927
})
3028
} else if (start && final) {
3129
global.db.collection("launch").find({ "launch_date_utc": {"$gte": `${start}T00:00:00Z`, "$lte": `${final}T00:00:00Z`}}, {"_id": 0 })
3230
.sort({"flight_number": 1})
3331
.toArray((err, doc) => {
34-
if (doc.length == 0) {
35-
res.status(200).end(JSON.stringify(error, null, 2))
36-
}
37-
res.end(JSON.stringify(doc, null, 2))
32+
res.json(doc)
3833
})
3934
} else if (site) {
4035
global.db.collection("launch").find({ "launch_site.site_id": `${site}`}, {"_id": 0 })
4136
.sort({"flight_number": 1})
4237
.toArray((err, doc) => {
43-
if (doc.length == 0) {
44-
res.status(200).end(JSON.stringify(error, null, 2))
45-
}
46-
res.end(JSON.stringify(doc, null, 2))
38+
res.json(doc)
4739
})
4840
} else {
4941
global.db.collection("launch").find({},{"_id": 0 }).sort({"flight_number": 1})
5042
.toArray((err, doc) => {
51-
if (err) return console.log(err)
52-
res.end(JSON.stringify(doc, null, 2))
43+
if (err) {
44+
return next(err)
45+
}
46+
res.json(doc)
5347
})
5448
}
5549
})
5650

5751
// Returns launches by core serial #
58-
v1.get("/cores/:core", (req, res) => {
52+
v1.get("/cores/:core", (req, res, next) => {
5953
const core = req.params.core
6054
global.db.collection("launch").find({"core_serial": `${core}`},{"_id": 0}).sort({"core_serial": 1})
6155
.toArray((err, doc) => {
62-
if (err) return console.log(err)
63-
if (doc.length == 0) {
64-
res.status(200).end(JSON.stringify(error, null, 2))
56+
if (err) {
57+
return next(err)
6558
}
66-
res.end(JSON.stringify(doc, null, 2))
59+
res.json(doc)
6760
})
6861
})
6962

7063
// Returns launches by capsule serial #
71-
v1.get("/caps/:cap", (req, res) => {
64+
v1.get("/caps/:cap", (req, res, next) => {
7265
const cap = req.params.cap
7366
global.db.collection("launch").find({"cap_serial": `${cap}`},{"_id": 0}).sort({"capsule_serial": 1})
7467
.toArray((err, doc) => {
75-
if (err) return console.log(err)
76-
if (doc.length == 0) {
77-
res.status(200).end(JSON.stringify(error, null, 2))
68+
if (err) {
69+
return next(err)
7870
}
79-
res.end(JSON.stringify(doc, null, 2))
71+
res.json(doc)
8072
})
8173
})
8274

8375
// Returns all ASDS launches
84-
v1.get("/asds", (req, res) => {
76+
v1.get("/asds", (req, res, next) => {
8577
global.db.collection("launch").find({"landing_type": "ASDS"},{"_id": 0}).sort({"flight_number": 1})
8678
.toArray((err, doc) => {
87-
if (err) return console.log(err)
88-
res.end(JSON.stringify(doc, null, 2))
79+
if (err) {
80+
return next(err)
81+
}
82+
res.json(doc)
8983
})
9084
})
9185

9286
// Returns all RTLS launches
93-
v1.get("/rtls", (req, res) => {
87+
v1.get("/rtls", (req, res, next) => {
9488
global.db.collection("launch").find({"landing_type": "RTLS"},{"_id": 0}).sort({"flight_number": 1})
9589
.toArray((err, doc) => {
96-
if (err) return console.log(err)
97-
res.end(JSON.stringify(doc, null, 2))
90+
if (err) {
91+
return next(err)
92+
}
93+
res.json(doc)
9894
})
9995
})
10096

routes/v1-launchpad.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const error = {error: "No results found"}
77
// Get all launchpads
88
v1.get("/", (req, res) => {
99
global.db.collection("launchpad").find({},{"_id": 0 }).toArray((err, doc) => {
10-
res.end(JSON.stringify(doc, null, 2))
10+
res.json(doc)
1111
})
1212
})
1313

@@ -16,9 +16,10 @@ v1.get("/:pad", (req, res) => {
1616
const id = req.params.pad
1717
global.db.collection("launchpad").find({"id": `${id}`}, {"_id": 0 }).toArray((err, doc) => {
1818
if (doc.length == 0) {
19-
res.status(200).end(JSON.stringify(error, null, 2))
19+
res.status(404)
20+
return res.json(error)
2021
}
21-
res.end(JSON.stringify(doc[0], null, 2))
22+
res.json(doc[0])
2223
})
2324
})
2425

routes/v1-parts.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,56 @@ const v1 = express.Router()
55
const error = {error: "No results found"}
66

77
// Returns all capsule information
8-
v1.get("/caps", (req, res) => {
8+
v1.get("/caps", (req, res, next) => {
99
global.db.collection("capsule").find({},{"_id": 0}).sort({"capsule_serial": 1})
1010
.toArray((err, doc) => {
11-
if (err) return console.log(err)
12-
res.end(JSON.stringify(doc, null, 2))
11+
if (err) {
12+
return next(err)
13+
}
14+
res.json(doc)
1315
})
1416
})
1517

1618
// Returns capsule info by serial #
17-
v1.get("/caps/:cap", (req, res) => {
19+
v1.get("/caps/:cap", (req, res, next) => {
1820
const cap = req.params.cap
1921
global.db.collection("capsule").find({"capsule_serial": `${cap}`},{"_id": 0}).sort({"capsule_serial": 1})
2022
.toArray((err, doc) => {
21-
if (err) return console.log(err)
23+
if (err) {
24+
return next(err)
25+
}
2226
if (doc.length == 0) {
23-
res.status(200).end(JSON.stringify(error, null, 2))
27+
res.status(404)
28+
return res.json(error)
2429
}
25-
res.end(JSON.stringify(doc[0], null, 2))
30+
res.json(doc[0])
2631
})
2732
})
2833

2934
// Returns all core information
30-
v1.get("/cores", (req, res) => {
35+
v1.get("/cores", (req, res, next) => {
3136
global.db.collection("core").find({},{"_id": 0}).sort({"core_serial": 1})
3237
.toArray((err, doc) => {
33-
if (err) return console.log(err)
34-
res.end(JSON.stringify(doc, null, 2))
38+
if (err) {
39+
return next(err)
40+
}
41+
res.json(doc)
3542
})
3643
})
3744

3845
// Returns core info by serial #
39-
v1.get("/cores/:core", (req, res) => {
46+
v1.get("/cores/:core", (req, res, next) => {
4047
const core = req.params.core
4148
global.db.collection("core").find({"core_serial": `${core}`},{"_id": 0}).sort({"core_serial": 1})
4249
.toArray((err, doc) => {
43-
if (err) return console.log(err)
50+
if (err) {
51+
return next(err)
52+
}
4453
if (doc.length == 0) {
45-
res.status(200).end(JSON.stringify(error, null, 2))
54+
res.status(404)
55+
return res.json(error)
4656
}
47-
res.end(JSON.stringify(doc[0], null, 2))
57+
res.json(doc[0])
4858
})
4959
})
5060

routes/v1-upcoming.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,30 @@
22

33
const express = require("express")
44
const v1 = express.Router()
5-
const error = {error: "No results found"}
65

76
// Upcoming launches by date, year, or all
8-
v1.get("/", (req, res) => {
7+
v1.get("/", (req, res, next) => {
98
const year = req.query.year
109
const start = req.query.start
1110
const final = req.query.final
1211
if (year) {
1312
global.db.collection("upcoming").find({"launch_year": `${year}`}, {"_id": 0 }).sort({"flight_number": -1})
1413
.toArray((err, doc) => {
15-
if (doc.length == 0) {
16-
res.status(200).end(JSON.stringify(error, null, 2))
17-
}
18-
res.end(JSON.stringify(doc, null, 2))
14+
res.json(doc)
1915
})
2016
} else if (start && final) {
2117
global.db.collection("upcoming").find({ "launch_date_utc": {"$gte": `${start}T00:00:00Z`, "$lte": `${final}T00:00:00Z`}}, {"_id": 0 })
2218
.sort({"flight_number": 1})
2319
.toArray((err, doc) => {
24-
if (doc.length == 0) {
25-
res.status(200).end(JSON.stringify(error, null, 2))
26-
}
27-
res.end(JSON.stringify(doc, null, 2))
20+
res.json(doc)
2821
})
2922
} else {
3023
global.db.collection("upcoming").find({},{"_id": 0 }).sort({"flight_number": 1})
3124
.toArray((err, doc) => {
32-
if (err) return console.log(err)
33-
res.end(JSON.stringify(doc, null, 2))
25+
if (err) {
26+
return next(err)
27+
}
28+
res.json(doc)
3429
})
3530
}
3631
})

0 commit comments

Comments
 (0)