Skip to content

Commit d76d9ba

Browse files
authored
For consistency with next() (#258)
* For consistency and bugs * For consistency and bugs
1 parent f3457ba commit d76d9ba

File tree

12 files changed

+93
-118
lines changed

12 files changed

+93
-118
lines changed

controllers/bulk.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ const bulkCreate = async function (req, res, next) {
2222
if (!Array.isArray(documents)) {
2323
err.message = "The request body must be an array of objects."
2424
err.status = 400
25-
next(utils.createExpressError(err))
26-
return
25+
return next(utils.createExpressError(err))
2726
}
2827
if (documents.length === 0) {
2928
err.message = "No action on an empty array."
3029
err.status = 400
31-
next(utils.createExpressError(err))
32-
return
30+
return next(utils.createExpressError(err))
3331
}
3432
const gatekeep = documents.filter(d=> {
3533
// Each item must be valid JSON, but can't be an array.
@@ -42,12 +40,11 @@ const bulkCreate = async function (req, res, next) {
4240
// Items must not have an @id, and in some cases same for id.
4341
const idcheck = _contextid(d["@context"]) ? (d.id ?? d["@id"]) : d["@id"]
4442
if(idcheck) return d
45-
})
43+
})
4644
if (gatekeep.length > 0) {
4745
err.message = "All objects in the body of a `/bulkCreate` must be JSON and must not contain a declared identifier property."
4846
err.status = 400
49-
next(utils.createExpressError(err))
50-
return
47+
return next(utils.createExpressError(err))
5148
}
5249

5350
// TODO: bulkWrite SLUGS? Maybe assign an id to each document and then use that to create the slug?
@@ -66,6 +63,7 @@ const bulkCreate = async function (req, res, next) {
6663
// unordered bulkWrite() operations have better performance metrics.
6764
let bulkOps = []
6865
const generatorAgent = getAgentClaim(req, next)
66+
if (!generatorAgent) return
6967
for(let d of documents) {
7068
// Do not create empty {}s
7169
if(Object.keys(d).length === 0) continue
@@ -92,7 +90,7 @@ const bulkCreate = async function (req, res, next) {
9290
}
9391
catch (error) {
9492
//MongoServerError from the client has the following properties: index, code, keyPattern, keyValue
95-
next(utils.createExpressError(error))
93+
return next(utils.createExpressError(error))
9694
}
9795
}
9896

@@ -111,14 +109,12 @@ const bulkUpdate = async function (req, res, next) {
111109
if (!Array.isArray(documents)) {
112110
err.message = "The request body must be an array of objects."
113111
err.status = 400
114-
next(utils.createExpressError(err))
115-
return
112+
return next(utils.createExpressError(err))
116113
}
117114
if (documents.length === 0) {
118115
err.message = "No action on an empty array."
119116
err.status = 400
120-
next(utils.createExpressError(err))
121-
return
117+
return next(utils.createExpressError(err))
122118
}
123119
const gatekeep = documents.filter(d => {
124120
// Each item must be valid JSON, but can't be an array.
@@ -136,12 +132,12 @@ const bulkUpdate = async function (req, res, next) {
136132
if (gatekeep.length > 0) {
137133
err.message = "All objects in the body of a `/bulkUpdate` must be JSON and must contain a declared identifier property."
138134
err.status = 400
139-
next(utils.createExpressError(err))
140-
return
135+
return next(utils.createExpressError(err))
141136
}
142137
// unordered bulkWrite() operations have better performance metrics.
143138
let bulkOps = []
144139
const generatorAgent = getAgentClaim(req, next)
140+
if (!generatorAgent) return
145141
for(const objectReceived of documents){
146142
// We know it has an id
147143
const idReceived = objectReceived["@id"] ?? objectReceived.id
@@ -154,8 +150,7 @@ const bulkUpdate = async function (req, res, next) {
154150
try {
155151
originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
156152
} catch (error) {
157-
next(utils.createExpressError(error))
158-
return
153+
return next(utils.createExpressError(error))
159154
}
160155
if (null === originalObject) continue
161156
if (utils.isDeleted(originalObject)) continue
@@ -196,7 +191,7 @@ const bulkUpdate = async function (req, res, next) {
196191
}
197192
catch (error) {
198193
//MongoServerError from the client has the following properties: index, code, keyPattern, keyValue
199-
next(utils.createExpressError(error))
194+
return next(utils.createExpressError(error))
200195
}
201196
}
202197

controllers/crud.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,27 @@ import { _contextid, idNegotiation, generateSlugId, ObjectID, getAgentClaim, par
1515
* */
1616
const create = async function (req, res, next) {
1717
res.set("Content-Type", "application/json; charset=utf-8")
18+
let props = req.body
19+
if (!props || Object.keys(props).length === 0) {
20+
let err = {
21+
message: "Detected empty JSON object. You must provide at least one property in the /create request body JSON.",
22+
status: 400
23+
}
24+
return next(utils.createExpressError(err))
25+
}
1826
let slug
1927
if(req.get("Slug")){
2028
let slug_json = await generateSlugId(req.get("Slug"), next)
2129
if(slug_json.code){
22-
next(utils.createExpressError(slug_json))
23-
return
30+
return next(utils.createExpressError(slug_json))
2431
}
2532
else{
2633
slug = slug_json.slug_id
2734
}
2835
}
2936

3037
let generatorAgent = getAgentClaim(req, next)
38+
if (!generatorAgent) return
3139
let context = req.body["@context"] ? { "@context": req.body["@context"] } : {}
3240
let provided = JSON.parse(JSON.stringify(req.body))
3341
let rerumProp = { "__rerum": utils.configureRerumOptions(generatorAgent, provided, false, false)["__rerum"] }
@@ -54,7 +62,7 @@ const create = async function (req, res, next) {
5462
}
5563
catch (error) {
5664
//MongoServerError from the client has the following properties: index, code, keyPattern, keyValue
57-
next(utils.createExpressError(error))
65+
return next(utils.createExpressError(error))
5866
}
5967
}
6068

@@ -68,22 +76,21 @@ const query = async function (req, res, next) {
6876
let props = req.body
6977
const limit = parseInt(req.query.limit ?? 100)
7078
const skip = parseInt(req.query.skip ?? 0)
71-
if (Object.keys(props).length === 0) {
79+
if (!props || Object.keys(props).length === 0) {
7280
//Hey now, don't ask for everything...this can happen by accident. Don't allow it.
7381
let err = {
7482
message: "Detected empty JSON object. You must provide at least one property in the /query request body JSON.",
7583
status: 400
7684
}
77-
next(utils.createExpressError(err))
78-
return
85+
return next(utils.createExpressError(err))
7986
}
8087
try {
8188
let matches = await db.find(props).limit(limit).skip(skip).toArray()
8289
matches = matches.map(o => idNegotiation(o))
8390
res.set(utils.configureLDHeadersFor(matches))
8491
res.json(matches)
8592
} catch (error) {
86-
next(utils.createExpressError(error))
93+
return next(utils.createExpressError(error))
8794
}
8895
}
8996

@@ -115,9 +122,9 @@ const id = async function (req, res, next) {
115122
"message": `No RERUM object with id '${id}'`,
116123
"status": 404
117124
}
118-
next(utils.createExpressError(err))
125+
return next(utils.createExpressError(err))
119126
} catch (error) {
120-
next(utils.createExpressError(error))
127+
return next(utils.createExpressError(error))
121128
}
122129
}
123130

controllers/delete.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ const deleteObj = async function(req, res, next) {
2626
try {
2727
id = req.params["_id"] ?? parseDocumentID(JSON.parse(JSON.stringify(req.body))["@id"]) ?? parseDocumentID(JSON.parse(JSON.stringify(req.body))["id"])
2828
} catch(error){
29-
next(utils.createExpressError(error))
30-
return
29+
return next(utils.createExpressError(error))
3130
}
3231
let agentRequestingDelete = getAgentClaim(req, next)
32+
if (!agentRequestingDelete) return
3333
let originalObject
3434
try {
3535
originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
3636
} catch (error) {
37-
next(utils.createExpressError(error))
38-
return
37+
return next(utils.createExpressError(error))
3938
}
4039
if (null !== originalObject) {
4140
let safe_original = JSON.parse(JSON.stringify(originalObject))
@@ -58,8 +57,7 @@ const deleteObj = async function(req, res, next) {
5857
})
5958
}
6059
if (err.status) {
61-
next(utils.createExpressError(err))
62-
return
60+
return next(utils.createExpressError(err))
6361
}
6462
let preserveID = safe_original["@id"]
6563
let deletedFlag = {} //The __deleted flag is a JSONObject
@@ -76,15 +74,13 @@ const deleteObj = async function(req, res, next) {
7674
try {
7775
result = await db.replaceOne({ "_id": originalObject["_id"] }, deletedObject)
7876
} catch (error) {
79-
next(utils.createExpressError(error))
80-
return
77+
return next(utils.createExpressError(error))
8178
}
8279
if (result.modifiedCount === 0) {
8380
//result didn't error out, the action was not performed. Sometimes, this is a neutral thing. Sometimes it is indicative of an error.
8481
err.message = "The original object was not replaced with the deleted object in the database."
8582
err.status = 500
86-
next(utils.createExpressError(err))
87-
return
83+
return next(utils.createExpressError(err))
8884
}
8985
//204 to say it is deleted and there is nothing in the body
9086
console.log("Object deleted: " + preserveID)
@@ -94,12 +90,11 @@ const deleteObj = async function(req, res, next) {
9490
//Not sure we can get here, as healHistoryTree might throw and error.
9591
err.message = "The history tree for the object being deleted could not be mended."
9692
err.status = 500
97-
next(utils.createExpressError(err))
98-
return
93+
return next(utils.createExpressError(err))
9994
}
10095
err.message = "No object with this id could be found in RERUM. Cannot delete."
10196
err.status = 404
102-
next(utils.createExpressError(err))
97+
return next(utils.createExpressError(err))
10398
}
10499

105100
/**

controllers/gog.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { _contextid, ObjectID, getAgentClaim, parseDocumentID, idNegotiation } f
2424
const _gog_fragments_from_manuscript = async function (req, res, next) {
2525
res.set("Content-Type", "application/json; charset=utf-8")
2626
const agent = getAgentClaim(req, next)
27+
if (!agent) return
2728
const agentID = agent.split("/").pop()
2829
const manID = req.body["ManuscriptWitness"]
2930
const limit = parseInt(req.query.limit ?? 50)
@@ -44,8 +45,7 @@ const _gog_fragments_from_manuscript = async function (req, res, next) {
4445
})
4546
}
4647
if (err.status) {
47-
next(utils.createExpressError(err))
48-
return
48+
return next(utils.createExpressError(err))
4949
}
5050
try {
5151
let matches = []
@@ -138,7 +138,7 @@ const _gog_fragments_from_manuscript = async function (req, res, next) {
138138
}
139139
catch (error) {
140140
console.error(error)
141-
next(utils.createExpressError(error))
141+
return next(utils.createExpressError(error))
142142
}
143143
}
144144

@@ -156,6 +156,7 @@ const _gog_fragments_from_manuscript = async function (req, res, next) {
156156
const _gog_glosses_from_manuscript = async function (req, res, next) {
157157
res.set("Content-Type", "application/json; charset=utf-8")
158158
const agent = getAgentClaim(req, next)
159+
if (!agent) return
159160
const agentID = agent.split("/").pop()
160161
const manID = req.body["ManuscriptWitness"]
161162
const limit = parseInt(req.query.limit ?? 50)
@@ -176,8 +177,7 @@ const _gog_glosses_from_manuscript = async function (req, res, next) {
176177
})
177178
}
178179
if (err.status) {
179-
next(utils.createExpressError(err))
180-
return
180+
return next(utils.createExpressError(err))
181181
}
182182
try {
183183
let matches = []
@@ -300,7 +300,7 @@ const _gog_glosses_from_manuscript = async function (req, res, next) {
300300
}
301301
catch (error) {
302302
console.error(error)
303-
next(utils.createExpressError(error))
303+
return next(utils.createExpressError(error))
304304
}
305305
}
306306

controllers/history.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@ const since = async function (req, res, next) {
2323
try {
2424
obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
2525
} catch (error) {
26-
next(utils.createExpressError(error))
27-
return
26+
return next(utils.createExpressError(error))
2827
}
2928
if (null === obj) {
3029
let err = {
3130
message: `Cannot produce a history. There is no object in the database with id '${id}'. Check the URL.`,
3231
status: 404
3332
}
34-
next(utils.createExpressError(err))
35-
return
33+
return next(utils.createExpressError(err))
3634
}
3735
let all = await getAllVersions(obj)
3836
.catch(error => {
@@ -60,16 +58,14 @@ const history = async function (req, res, next) {
6058
try {
6159
obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
6260
} catch (error) {
63-
next(utils.createExpressError(error))
64-
return
61+
return next(utils.createExpressError(error))
6562
}
6663
if (null === obj) {
6764
let err = {
6865
message: `Cannot produce a history. There is no object in the database with id '${id}'. Check the URL.`,
6966
status: 404
7067
}
71-
next(utils.createExpressError(err))
72-
return
68+
return next(utils.createExpressError(err))
7369
}
7470
let all = await getAllVersions(obj)
7571
.catch(error => {
@@ -103,9 +99,9 @@ const idHeadRequest = async function (req, res, next) {
10399
"message": `No RERUM object with id '${id}'`,
104100
"status": 404
105101
}
106-
next(utils.createExpressError(err))
102+
return next(utils.createExpressError(err))
107103
} catch (error) {
108-
next(utils.createExpressError(error))
104+
return next(utils.createExpressError(error))
109105
}
110106
}
111107

@@ -128,9 +124,9 @@ const queryHeadRequest = async function (req, res, next) {
128124
"message": `There are no objects in the database matching the query. Check the request body.`,
129125
"status": 404
130126
}
131-
next(utils.createExpressError(err))
127+
return next(utils.createExpressError(err))
132128
} catch (error) {
133-
next(utils.createExpressError(error))
129+
return next(utils.createExpressError(error))
134130
}
135131
}
136132

@@ -145,16 +141,14 @@ const sinceHeadRequest = async function (req, res, next) {
145141
try {
146142
obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
147143
} catch (error) {
148-
next(utils.createExpressError(error))
149-
return
144+
return next(utils.createExpressError(error))
150145
}
151146
if (null === obj) {
152147
let err = {
153148
message: `Cannot produce a history. There is no object in the database with id '${id}'. Check the URL.`,
154149
status: 404
155150
}
156-
next(utils.createExpressError(err))
157-
return
151+
return next(utils.createExpressError(err))
158152
}
159153
let all = await getAllVersions(obj)
160154
.catch(error => {
@@ -183,16 +177,14 @@ const historyHeadRequest = async function (req, res, next) {
183177
try {
184178
obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]})
185179
} catch (error) {
186-
next(utils.createExpressError(error))
187-
return
180+
return next(utils.createExpressError(error))
188181
}
189182
if (null === obj) {
190183
let err = {
191184
message: "Cannot produce a history. There is no object in the database with this id. Check the URL.",
192185
status: 404
193186
}
194-
next(utils.createExpressError(err))
195-
return
187+
return next(utils.createExpressError(err))
196188
}
197189
let all = await getAllVersions(obj)
198190
.catch(error => {

0 commit comments

Comments
 (0)