Skip to content

Commit dd92643

Browse files
committed
revised error msgs to return json objects instead of strings
1 parent 1873ef3 commit dd92643

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const compression = require("compression")
66
const helmet = require("helmet")
77
const config = require("./config.json")
88
const MongoClient = require("mongodb")
9+
const endpointError = {error: "No Endpoint Found"}
910
const app = express()
1011

1112
const home = require("./routes/v1-home")
@@ -37,7 +38,7 @@ app.use((req, res, next) => {
3738

3839
// 404 Error Handler
3940
app.use((req, res) => {
40-
res.status(404).json("No Endpoint Found")
41+
res.status(404).end(JSON.stringify(endpointError, null, 2))
4142
})
4243

4344
MongoClient.connect(config.url, (err, database) => {

routes/v1-launches.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const express = require("express")
44
const v1 = express.Router()
5+
const error = {error: "No Matches Found"}
56

67
// Get most recent launch
78
v1.get("/latest", (req, res) => {
@@ -22,7 +23,7 @@ v1.get("/", (req, res) => {
2223
global.db.collection("launch").find({"launch_year": `${year}`}, {"_id": 0 }).sort({"flight_number": -1})
2324
.toArray((err, doc) => {
2425
if (doc.length == 0) {
25-
res.end("No Matches Found")
26+
res.end(JSON.stringify(error, null, 2))
2627
}
2728
res.end(JSON.stringify(doc, null, 2))
2829
})
@@ -31,7 +32,7 @@ v1.get("/", (req, res) => {
3132
.sort({"flight_number": 1})
3233
.toArray((err, doc) => {
3334
if (doc.length == 0) {
34-
res.end("No Matches Found")
35+
res.end(JSON.stringify(error, null, 2))
3536
}
3637
res.end(JSON.stringify(doc, null, 2))
3738
})
@@ -40,7 +41,7 @@ v1.get("/", (req, res) => {
4041
.sort({"flight_number": 1})
4142
.toArray((err, doc) => {
4243
if (doc.length == 0) {
43-
res.end("No Matches Found")
44+
res.end(JSON.stringify(error, null, 2))
4445
}
4546
res.end(JSON.stringify(doc, null, 2))
4647
})
@@ -60,7 +61,7 @@ v1.get("/cores/:core", (req, res) => {
6061
.toArray((err, doc) => {
6162
if (err) return console.log(err)
6263
if (doc.length == 0) {
63-
res.end("No Matches Found")
64+
res.end(JSON.stringify(error, null, 2))
6465
}
6566
res.end(JSON.stringify(doc, null, 2))
6667
})
@@ -73,7 +74,7 @@ v1.get("/caps/:cap", (req, res) => {
7374
.toArray((err, doc) => {
7475
if (err) return console.log(err)
7576
if (doc.length == 0) {
76-
res.end("No Matches Found")
77+
res.end(JSON.stringify(error, null, 2))
7778
}
7879
res.end(JSON.stringify(doc, null, 2))
7980
})

routes/v1-launchpad.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const express = require("express")
44
const v1 = express.Router()
5+
const error = {error: "No Matches Found"}
56

67
// Get all launchpads
78
v1.get("/", (req, res) => {
@@ -15,7 +16,7 @@ v1.get("/:pad", (req, res) => {
1516
const id = req.params.pad
1617
global.db.collection("launchpad").find({"id": `${id}`}, {"_id": 0 }).toArray((err, doc) => {
1718
if (doc.length == 0) {
18-
res.end("No Matches Found")
19+
res.end(JSON.stringify(error, null, 2))
1920
}
2021
res.end(JSON.stringify(doc[0], null, 2))
2122
})

routes/v1-parts.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const express = require("express")
44
const v1 = express.Router()
5+
const error = {error: "No Matches Found"}
56

67
// Returns all capsule information
78
v1.get("/caps", (req, res) => {
@@ -19,7 +20,7 @@ v1.get("/caps/:cap", (req, res) => {
1920
.toArray((err, doc) => {
2021
if (err) return console.log(err)
2122
if (doc.length == 0) {
22-
res.end("No Matches Found")
23+
res.end(JSON.stringify(error, null, 2))
2324
}
2425
res.end(JSON.stringify(doc[0], null, 2))
2526
})
@@ -41,7 +42,7 @@ v1.get("/cores/:core", (req, res) => {
4142
.toArray((err, doc) => {
4243
if (err) return console.log(err)
4344
if (doc.length == 0) {
44-
res.end("No Matches Found")
45+
res.end(JSON.stringify(error, null, 2))
4546
}
4647
res.end(JSON.stringify(doc[0], null, 2))
4748
})

routes/v1-upcoming.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const express = require("express")
44
const v1 = express.Router()
5+
const error = {error: "No Matches Found"}
56

67
// Upcoming launches by date, year, or all
78
v1.get("/", (req, res) => {
@@ -12,7 +13,7 @@ v1.get("/", (req, res) => {
1213
global.db.collection("upcoming").find({"launch_year": `${year}`}, {"_id": 0 }).sort({"flight_number": -1})
1314
.toArray((err, doc) => {
1415
if (doc.length == 0) {
15-
res.end("No Matches Found")
16+
res.end(JSON.stringify(error, null, 2))
1617
}
1718
res.end(JSON.stringify(doc, null, 2))
1819
})
@@ -21,7 +22,7 @@ v1.get("/", (req, res) => {
2122
.sort({"flight_number": 1})
2223
.toArray((err, doc) => {
2324
if (doc.length == 0) {
24-
res.end("No Matches Found")
25+
res.end(JSON.stringify(error, null, 2))
2526
}
2627
res.end(JSON.stringify(doc, null, 2))
2728
})

0 commit comments

Comments
 (0)