Skip to content

Commit 0714a37

Browse files
committed
All errors now return 404, with a single error msg
1 parent 864e76e commit 0714a37

File tree

6 files changed

+38
-44
lines changed

6 files changed

+38
-44
lines changed

app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +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"}
9+
const error = {error: "No results found"}
1010
const app = express()
1111

1212
const home = require("./routes/v1-home")
@@ -39,7 +39,7 @@ app.use("/v1/parts", parts)
3939

4040
// 404 Error Handler
4141
app.use((req, res) => {
42-
res.status(404).end(JSON.stringify(endpointError, null, 2))
42+
res.status(404).end(JSON.stringify(error, null, 2))
4343
})
4444

4545
// Mongo Connection + Server Start

routes/v1-launches.js

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

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

77
// Get most recent launch
88
v1.get("/latest", (req, res) => {
@@ -23,7 +23,7 @@ v1.get("/", (req, res) => {
2323
global.db.collection("launch").find({"launch_year": `${year}`}, {"_id": 0 }).sort({"flight_number": -1})
2424
.toArray((err, doc) => {
2525
if (doc.length == 0) {
26-
res.end(JSON.stringify(error, null, 2))
26+
res.status(404).end(JSON.stringify(error, null, 2))
2727
}
2828
res.end(JSON.stringify(doc, null, 2))
2929
})
@@ -32,7 +32,7 @@ v1.get("/", (req, res) => {
3232
.sort({"flight_number": 1})
3333
.toArray((err, doc) => {
3434
if (doc.length == 0) {
35-
res.end(JSON.stringify(error, null, 2))
35+
res.status(404).end(JSON.stringify(error, null, 2))
3636
}
3737
res.end(JSON.stringify(doc, null, 2))
3838
})
@@ -41,7 +41,7 @@ v1.get("/", (req, res) => {
4141
.sort({"flight_number": 1})
4242
.toArray((err, doc) => {
4343
if (doc.length == 0) {
44-
res.end(JSON.stringify(error, null, 2))
44+
res.status(404).end(JSON.stringify(error, null, 2))
4545
}
4646
res.end(JSON.stringify(doc, null, 2))
4747
})
@@ -61,7 +61,7 @@ v1.get("/cores/:core", (req, res) => {
6161
.toArray((err, doc) => {
6262
if (err) return console.log(err)
6363
if (doc.length == 0) {
64-
res.end(JSON.stringify(error, null, 2))
64+
res.status(404).end(JSON.stringify(error, null, 2))
6565
}
6666
res.end(JSON.stringify(doc, null, 2))
6767
})
@@ -74,7 +74,7 @@ v1.get("/caps/:cap", (req, res) => {
7474
.toArray((err, doc) => {
7575
if (err) return console.log(err)
7676
if (doc.length == 0) {
77-
res.end(JSON.stringify(error, null, 2))
77+
res.status(404).end(JSON.stringify(error, null, 2))
7878
}
7979
res.end(JSON.stringify(doc, null, 2))
8080
})
@@ -85,9 +85,6 @@ v1.get("/asds", (req, res) => {
8585
global.db.collection("launch").find({"landing_type": "ASDS"},{"_id": 0}).sort({"flight_number": 1})
8686
.toArray((err, doc) => {
8787
if (err) return console.log(err)
88-
if (doc.length == 0) {
89-
res.end(JSON.stringify(error, null, 2))
90-
}
9188
res.end(JSON.stringify(doc, null, 2))
9289
})
9390
})
@@ -97,9 +94,6 @@ v1.get("/rtls", (req, res) => {
9794
global.db.collection("launch").find({"landing_type": "RTLS"},{"_id": 0}).sort({"flight_number": 1})
9895
.toArray((err, doc) => {
9996
if (err) return console.log(err)
100-
if (doc.length == 0) {
101-
res.end(JSON.stringify(error, null, 2))
102-
}
10397
res.end(JSON.stringify(doc, null, 2))
10498
})
10599
})

routes/v1-launchpad.js

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

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

77
// Get all launchpads
88
v1.get("/", (req, res) => {
@@ -16,7 +16,7 @@ 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.end(JSON.stringify(error, null, 2))
19+
res.status(404).end(JSON.stringify(error, null, 2))
2020
}
2121
res.end(JSON.stringify(doc[0], null, 2))
2222
})

routes/v1-parts.js

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

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

77
// Returns all capsule information
88
v1.get("/caps", (req, res) => {
@@ -20,7 +20,7 @@ v1.get("/caps/:cap", (req, res) => {
2020
.toArray((err, doc) => {
2121
if (err) return console.log(err)
2222
if (doc.length == 0) {
23-
res.end(JSON.stringify(error, null, 2))
23+
res.status(404).end(JSON.stringify(error, null, 2))
2424
}
2525
res.end(JSON.stringify(doc[0], null, 2))
2626
})
@@ -42,7 +42,7 @@ v1.get("/cores/:core", (req, res) => {
4242
.toArray((err, doc) => {
4343
if (err) return console.log(err)
4444
if (doc.length == 0) {
45-
res.end(JSON.stringify(error, null, 2))
45+
res.status(404).end(JSON.stringify(error, null, 2))
4646
}
4747
res.end(JSON.stringify(doc[0], null, 2))
4848
})

routes/v1-upcoming.js

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

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

77
// Upcoming launches by date, year, or all
88
v1.get("/", (req, res) => {
@@ -13,7 +13,7 @@ v1.get("/", (req, res) => {
1313
global.db.collection("upcoming").find({"launch_year": `${year}`}, {"_id": 0 }).sort({"flight_number": -1})
1414
.toArray((err, doc) => {
1515
if (doc.length == 0) {
16-
res.end(JSON.stringify(error, null, 2))
16+
res.status(404).end(JSON.stringify(error, null, 2))
1717
}
1818
res.end(JSON.stringify(doc, null, 2))
1919
})
@@ -22,7 +22,7 @@ v1.get("/", (req, res) => {
2222
.sort({"flight_number": 1})
2323
.toArray((err, doc) => {
2424
if (doc.length == 0) {
25-
res.end(JSON.stringify(error, null, 2))
25+
res.status(404).end(JSON.stringify(error, null, 2))
2626
}
2727
res.end(JSON.stringify(doc, null, 2))
2828
})

test/v1-all.test.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ beforeAll((done) => {
1515
test("It should return 404 endpoint error", () => {
1616
return request(app).get("/v2").then(response => {
1717
expect(response.statusCode).toBe(404)
18-
expect(response.text).toContain("No Endpoint Found")
18+
expect(response.text).toContain("No results found")
1919
})
2020
})
2121

@@ -103,8 +103,8 @@ test("It should return LC-39A info", () => {
103103

104104
test("It should return no launchpads found info", () => {
105105
return request(app).get("/v1/launchpads/ksc_lc_40a").then(response => {
106-
expect(response.statusCode).toBe(200)
107-
expect(response.text).toContain("No Matches Found")
106+
expect(response.statusCode).toBe(404)
107+
expect(response.text).toContain("No results found")
108108
})
109109
})
110110

@@ -135,8 +135,8 @@ test("It should return all past launches from LC-4E", () => {
135135

136136
test("It should return no launches from made up launchpad", () => {
137137
return request(app).get("/v1/launches?site=vafb_slc_5e").then(response => {
138-
expect(response.statusCode).toBe(200)
139-
expect(response.text).toContain("No Matches Found")
138+
expect(response.statusCode).toBe(404)
139+
expect(response.text).toContain("No results found")
140140
})
141141
})
142142

@@ -149,8 +149,8 @@ test("It should return all 2012 launches", () => {
149149

150150
test("It should return no 2005 launches", () => {
151151
return request(app).get("/v1/launches?year=2005").then(response => {
152-
expect(response.statusCode).toBe(200)
153-
expect(response.text).toContain("No Matches Found")
152+
expect(response.statusCode).toBe(404)
153+
expect(response.text).toContain("No results found")
154154
})
155155
})
156156

@@ -162,8 +162,8 @@ test("It should return past launches in timeframe", () => {
162162

163163
test("It should return no past launches in timeframe", () => {
164164
return request(app).get("/v1/launches?start=2005-01-20&final=2005-05-25").then(response => {
165-
expect(response.statusCode).toBe(200)
166-
expect(response.text).toContain("No Matches Found")
165+
expect(response.statusCode).toBe(404)
166+
expect(response.text).toContain("No results found")
167167
})
168168
})
169169

@@ -176,8 +176,8 @@ test("It should return all launches with core B1021", () => {
176176

177177
test("It should return no launches with core A1021", () => {
178178
return request(app).get("/v1/launches/cores/A1021").then(response => {
179-
expect(response.statusCode).toBe(200)
180-
expect(response.text).toContain("No Matches Found")
179+
expect(response.statusCode).toBe(404)
180+
expect(response.text).toContain("No results found")
181181
})
182182
})
183183

@@ -190,8 +190,8 @@ test("It should return all launches with cap C106", () => {
190190

191191
test("It should return no launches with cap C403", () => {
192192
return request(app).get("/v1/launches/caps/C403").then(response => {
193-
expect(response.statusCode).toBe(200)
194-
expect(response.text).toContain("No Matches Found")
193+
expect(response.statusCode).toBe(404)
194+
expect(response.text).toContain("No results found")
195195
})
196196
})
197197

@@ -227,21 +227,21 @@ test("It should return all upcoming launches in 2017", () => {
227227

228228
test("It should return no upcoming launches in 2016", () => {
229229
return request(app).get("/v1/launches/upcoming?year=2016").then(response => {
230-
expect(response.statusCode).toBe(200)
231-
expect(response.text).toContain("No Matches Found")
230+
expect(response.statusCode).toBe(404)
231+
expect(response.text).toContain("No results found")
232232
})
233233
})
234234

235235
test("It should return all launches in the timeframe", () => {
236236
return request(app).get("/v1/launches/upcoming?start=2011-01-20&final=2017-05-25").then(response => {
237-
expect(response.statusCode).toBe(200)
237+
expect(response.statusCode).toBe(404)
238238
})
239239
})
240240

241241
test("It should return no launches in the timeframe", () => {
242242
return request(app).get("/v1/launches/upcoming?start=2011-01-20&final=2016-05-25").then(response => {
243-
expect(response.statusCode).toBe(200)
244-
expect(response.text).toContain("No Matches Found")
243+
expect(response.statusCode).toBe(404)
244+
expect(response.text).toContain("No results found")
245245
})
246246
})
247247

@@ -266,8 +266,8 @@ test("It should return all info on C106", () => {
266266

267267
test("It should return no info on C406", () => {
268268
return request(app).get("/v1/parts/caps/C406").then(response => {
269-
expect(response.statusCode).toBe(200)
270-
expect(response.text).toContain("No Matches Found")
269+
expect(response.statusCode).toBe(404)
270+
expect(response.text).toContain("No results found")
271271
})
272272
})
273273

@@ -288,7 +288,7 @@ test("It should return core info on B1021", () => {
288288

289289
test("It should return no core info on A1021", () => {
290290
return request(app).get("/v1/parts/cores/A1021").then(response => {
291-
expect(response.statusCode).toBe(200)
292-
expect(response.text).toContain("No Matches Found")
291+
expect(response.statusCode).toBe(404)
292+
expect(response.text).toContain("No results found")
293293
})
294294
})

0 commit comments

Comments
 (0)