Skip to content

Commit 305ce4f

Browse files
committed
Better error handling, uncaught error handling and JSON response generation.
1 parent 13bc428 commit 305ce4f

File tree

10 files changed

+109
-83
lines changed

10 files changed

+109
-83
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const v1 = express.Router()
77
v1.get("/", (req, res) => {
88
global.db.collection("home").find({},{"_id": 0 }).toArray((err, doc) => {
99
if (err) return console.log(err)
10-
res.end(JSON.stringify(doc[0], null, 2))
10+
res.json(doc[0])
1111
})
1212
})
1313

routes/v1-info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const v1 = express.Router()
77
v1.get("/", (req, res) => {
88
global.db.collection("info").find({},{"_id": 0 }).toArray((err, doc) => {
99
if (err) return console.log(err)
10-
res.end(JSON.stringify(doc[0], null, 2))
10+
res.json(doc[0])
1111
})
1212
})
1313

routes/v1-launches.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ v1.get("/latest", (req, res) => {
99
global.db.collection("launch").find({},{"_id": 0 }).sort({"flight_number": -1}).limit(1)
1010
.toArray((err, doc) => {
1111
if (err) return console.log(err)
12-
res.end(JSON.stringify(doc, null, 2))
12+
res.json(doc)
1313
})
1414
})
1515

@@ -22,34 +22,26 @@ v1.get("/", (req, res) => {
2222
if (year) {
2323
global.db.collection("launch").find({"launch_year": `${year}`}, {"_id": 0 }).sort({"flight_number": -1})
2424
.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))
25+
res.json(doc)
2926
})
3027
} else if (start && final) {
3128
global.db.collection("launch").find({ "launch_date_utc": {"$gte": `${start}T00:00:00Z`, "$lte": `${final}T00:00:00Z`}}, {"_id": 0 })
3229
.sort({"flight_number": 1})
3330
.toArray((err, doc) => {
3431
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) => {
5143
if (err) return console.log(err)
52-
res.end(JSON.stringify(doc, null, 2))
44+
res.json(doc)
5345
})
5446
}
5547
})
@@ -60,10 +52,7 @@ v1.get("/cores/:core", (req, res) => {
6052
global.db.collection("launch").find({"core_serial": `${core}`},{"_id": 0}).sort({"core_serial": 1})
6153
.toArray((err, doc) => {
6254
if (err) return console.log(err)
63-
if (doc.length == 0) {
64-
res.status(200).end(JSON.stringify(error, null, 2))
65-
}
66-
res.end(JSON.stringify(doc, null, 2))
55+
res.json(doc)
6756
})
6857
})
6958

@@ -73,10 +62,7 @@ v1.get("/caps/:cap", (req, res) => {
7362
global.db.collection("launch").find({"cap_serial": `${cap}`},{"_id": 0}).sort({"capsule_serial": 1})
7463
.toArray((err, doc) => {
7564
if (err) return console.log(err)
76-
if (doc.length == 0) {
77-
res.status(200).end(JSON.stringify(error, null, 2))
78-
}
79-
res.end(JSON.stringify(doc, null, 2))
65+
res.json(doc)
8066
})
8167
})
8268

@@ -85,7 +71,7 @@ v1.get("/asds", (req, res) => {
8571
global.db.collection("launch").find({"landing_type": "ASDS"},{"_id": 0}).sort({"flight_number": 1})
8672
.toArray((err, doc) => {
8773
if (err) return console.log(err)
88-
res.end(JSON.stringify(doc, null, 2))
74+
res.json(doc)
8975
})
9076
})
9177

@@ -94,7 +80,7 @@ v1.get("/rtls", (req, res) => {
9480
global.db.collection("launch").find({"landing_type": "RTLS"},{"_id": 0}).sort({"flight_number": 1})
9581
.toArray((err, doc) => {
9682
if (err) return console.log(err)
97-
res.end(JSON.stringify(doc, null, 2))
83+
res.json(doc)
9884
})
9985
})
10086

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: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,48 @@ 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) return next(err)
12+
res.json(doc)
1313
})
1414
})
1515

1616
// Returns capsule info by serial #
17-
v1.get("/caps/:cap", (req, res) => {
17+
v1.get("/caps/:cap", (req, res, next) => {
1818
const cap = req.params.cap
1919
global.db.collection("capsule").find({"capsule_serial": `${cap}`},{"_id": 0}).sort({"capsule_serial": 1})
2020
.toArray((err, doc) => {
21-
if (err) return console.log(err)
21+
if (err) return next(err)
2222
if (doc.length == 0) {
23-
res.status(200).end(JSON.stringify(error, null, 2))
23+
res.status(404)
24+
return res.json(error)
2425
}
25-
res.end(JSON.stringify(doc[0], null, 2))
26+
res.json(doc[0])
2627
})
2728
})
2829

2930
// Returns all core information
30-
v1.get("/cores", (req, res) => {
31+
v1.get("/cores", (req, res, next) => {
3132
global.db.collection("core").find({},{"_id": 0}).sort({"core_serial": 1})
3233
.toArray((err, doc) => {
33-
if (err) return console.log(err)
34-
res.end(JSON.stringify(doc, null, 2))
34+
if (err) return next(err)
35+
res.json(doc)
3536
})
3637
})
3738

3839
// Returns core info by serial #
39-
v1.get("/cores/:core", (req, res) => {
40+
v1.get("/cores/:core", (req, res, next) => {
4041
const core = req.params.core
4142
global.db.collection("core").find({"core_serial": `${core}`},{"_id": 0}).sort({"core_serial": 1})
4243
.toArray((err, doc) => {
43-
if (err) return console.log(err)
44+
if (err) return next(err)
4445
if (doc.length == 0) {
45-
res.status(200).end(JSON.stringify(error, null, 2))
46+
res.status(404)
47+
return res.json(error)
4648
}
47-
res.end(JSON.stringify(doc[0], null, 2))
49+
res.json(doc[0])
4850
})
4951
})
5052

routes/v1-upcoming.js

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

77
// Upcoming launches by date, year, or all
8-
v1.get("/", (req, res) => {
8+
v1.get("/", (req, res, next) => {
99
const year = req.query.year
1010
const start = req.query.start
1111
const final = req.query.final
1212
if (year) {
1313
global.db.collection("upcoming").find({"launch_year": `${year}`}, {"_id": 0 }).sort({"flight_number": -1})
1414
.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))
15+
res.json(doc)
1916
})
2017
} else if (start && final) {
2118
global.db.collection("upcoming").find({ "launch_date_utc": {"$gte": `${start}T00:00:00Z`, "$lte": `${final}T00:00:00Z`}}, {"_id": 0 })
2219
.sort({"flight_number": 1})
2320
.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))
21+
res.json(doc)
2822
})
2923
} else {
3024
global.db.collection("upcoming").find({},{"_id": 0 }).sort({"flight_number": 1})
3125
.toArray((err, doc) => {
32-
if (err) return console.log(err)
33-
res.end(JSON.stringify(doc, null, 2))
26+
if (err) return next(err)
27+
res.json(doc)
3428
})
3529
}
3630
})

routes/v1-vehicles.js

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

66
// Returns all vehicle info
7-
v1.get("/", (req, res) => {
7+
v1.get("/", (req, res, next) => {
88
global.db.collection("vehicle").find({},{"_id": 0 }).toArray((err, doc) => {
9-
if (err) return console.log(err)
10-
res.end(JSON.stringify(doc, null, 2))
9+
if (err) return next(err)
10+
res.json(doc)
1111
})
1212
})
1313

1414
// Returns Falcon 1 info
15-
v1.get("/falcon1", (req, res) => {
15+
v1.get("/falcon1", (req, res, next) => {
1616
global.db.collection("vehicle").find({"id": "falcon1"},{"_id": 0 }).toArray((err, doc) => {
17-
if (err) return console.log(err)
18-
res.end(JSON.stringify(doc[0], null, 2))
17+
if (err) return next(err)
18+
res.json(doc[0])
1919
})
2020
})
2121

2222
// Returns Falcon 9 info
23-
v1.get("/falcon9", (req, res) => {
23+
v1.get("/falcon9", (req, res, next) => {
2424
global.db.collection("vehicle").find({"id": "falcon9"},{"_id": 0 }).toArray((err, doc) => {
25-
if (err) return console.log(err)
26-
res.end(JSON.stringify(doc[0], null, 2))
25+
if (err) return next(err)
26+
res.json(doc[0])
2727
})
2828
})
2929

3030
// Returns Falcon Heavy info
31-
v1.get("/falconheavy", (req, res) => {
31+
v1.get("/falconheavy", (req, res, next) => {
3232
global.db.collection("vehicle").find({"id": "falcon_heavy"},{"_id": 0 }).toArray((err, doc) => {
33-
if (err) return console.log(err)
34-
res.end(JSON.stringify(doc[0], null, 2))
33+
if (err) return next(err)
34+
res.json(doc[0])
3535
})
3636
})
3737

3838
// Returns Dragon info
39-
v1.get("/dragon", (req, res) => {
39+
v1.get("/dragon", (req, res, next) => {
4040
global.db.collection("vehicle").find({"id": "dragon"},{"_id": 0 }).toArray((err, doc) => {
41-
if (err) return console.log(err)
42-
res.end(JSON.stringify(doc[0], null, 2))
41+
if (err) return next(err)
42+
res.json(doc[0])
4343
})
4444
})
4545

0 commit comments

Comments
 (0)