diff --git a/controllers/bulk.js b/controllers/bulk.js index 5e019a27..c2bd5759 100644 --- a/controllers/bulk.js +++ b/controllers/bulk.js @@ -22,14 +22,12 @@ const bulkCreate = async function (req, res, next) { if (!Array.isArray(documents)) { err.message = "The request body must be an array of objects." err.status = 400 - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } if (documents.length === 0) { err.message = "No action on an empty array." err.status = 400 - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } const gatekeep = documents.filter(d=> { // Each item must be valid JSON, but can't be an array. @@ -42,12 +40,11 @@ const bulkCreate = async function (req, res, next) { // Items must not have an @id, and in some cases same for id. const idcheck = _contextid(d["@context"]) ? (d.id ?? d["@id"]) : d["@id"] if(idcheck) return d - }) + }) if (gatekeep.length > 0) { err.message = "All objects in the body of a `/bulkCreate` must be JSON and must not contain a declared identifier property." err.status = 400 - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } // 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) { // unordered bulkWrite() operations have better performance metrics. let bulkOps = [] const generatorAgent = getAgentClaim(req, next) + if (!generatorAgent) return for(let d of documents) { // Do not create empty {}s if(Object.keys(d).length === 0) continue @@ -92,7 +90,7 @@ const bulkCreate = async function (req, res, next) { } catch (error) { //MongoServerError from the client has the following properties: index, code, keyPattern, keyValue - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } @@ -111,14 +109,12 @@ const bulkUpdate = async function (req, res, next) { if (!Array.isArray(documents)) { err.message = "The request body must be an array of objects." err.status = 400 - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } if (documents.length === 0) { err.message = "No action on an empty array." err.status = 400 - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } const gatekeep = documents.filter(d => { // Each item must be valid JSON, but can't be an array. @@ -136,12 +132,12 @@ const bulkUpdate = async function (req, res, next) { if (gatekeep.length > 0) { err.message = "All objects in the body of a `/bulkUpdate` must be JSON and must contain a declared identifier property." err.status = 400 - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } // unordered bulkWrite() operations have better performance metrics. let bulkOps = [] const generatorAgent = getAgentClaim(req, next) + if (!generatorAgent) return for(const objectReceived of documents){ // We know it has an id const idReceived = objectReceived["@id"] ?? objectReceived.id @@ -154,8 +150,7 @@ const bulkUpdate = async function (req, res, next) { try { originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null === originalObject) continue if (utils.isDeleted(originalObject)) continue @@ -196,7 +191,7 @@ const bulkUpdate = async function (req, res, next) { } catch (error) { //MongoServerError from the client has the following properties: index, code, keyPattern, keyValue - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } diff --git a/controllers/crud.js b/controllers/crud.js index 178f455c..f89032fb 100644 --- a/controllers/crud.js +++ b/controllers/crud.js @@ -15,12 +15,19 @@ import { _contextid, idNegotiation, generateSlugId, ObjectID, getAgentClaim, par * */ const create = async function (req, res, next) { res.set("Content-Type", "application/json; charset=utf-8") + let props = req.body + if (!props || Object.keys(props).length === 0) { + let err = { + message: "Detected empty JSON object. You must provide at least one property in the /create request body JSON.", + status: 400 + } + return next(utils.createExpressError(err)) + } let slug if(req.get("Slug")){ let slug_json = await generateSlugId(req.get("Slug"), next) if(slug_json.code){ - next(utils.createExpressError(slug_json)) - return + return next(utils.createExpressError(slug_json)) } else{ slug = slug_json.slug_id @@ -28,6 +35,7 @@ const create = async function (req, res, next) { } let generatorAgent = getAgentClaim(req, next) + if (!generatorAgent) return let context = req.body["@context"] ? { "@context": req.body["@context"] } : {} let provided = JSON.parse(JSON.stringify(req.body)) let rerumProp = { "__rerum": utils.configureRerumOptions(generatorAgent, provided, false, false)["__rerum"] } @@ -54,7 +62,7 @@ const create = async function (req, res, next) { } catch (error) { //MongoServerError from the client has the following properties: index, code, keyPattern, keyValue - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } @@ -68,14 +76,13 @@ const query = async function (req, res, next) { let props = req.body const limit = parseInt(req.query.limit ?? 100) const skip = parseInt(req.query.skip ?? 0) - if (Object.keys(props).length === 0) { + if (!props || Object.keys(props).length === 0) { //Hey now, don't ask for everything...this can happen by accident. Don't allow it. let err = { message: "Detected empty JSON object. You must provide at least one property in the /query request body JSON.", status: 400 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } try { let matches = await db.find(props).limit(limit).skip(skip).toArray() @@ -83,7 +90,7 @@ const query = async function (req, res, next) { res.set(utils.configureLDHeadersFor(matches)) res.json(matches) } catch (error) { - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } @@ -115,9 +122,9 @@ const id = async function (req, res, next) { "message": `No RERUM object with id '${id}'`, "status": 404 } - next(utils.createExpressError(err)) + return next(utils.createExpressError(err)) } catch (error) { - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } diff --git a/controllers/delete.js b/controllers/delete.js index 8002f1cf..f6f38fe3 100644 --- a/controllers/delete.js +++ b/controllers/delete.js @@ -26,16 +26,15 @@ const deleteObj = async function(req, res, next) { try { id = req.params["_id"] ?? parseDocumentID(JSON.parse(JSON.stringify(req.body))["@id"]) ?? parseDocumentID(JSON.parse(JSON.stringify(req.body))["id"]) } catch(error){ - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } let agentRequestingDelete = getAgentClaim(req, next) + if (!agentRequestingDelete) return let originalObject try { originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null !== originalObject) { let safe_original = JSON.parse(JSON.stringify(originalObject)) @@ -58,8 +57,7 @@ const deleteObj = async function(req, res, next) { }) } if (err.status) { - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } let preserveID = safe_original["@id"] let deletedFlag = {} //The __deleted flag is a JSONObject @@ -76,15 +74,13 @@ const deleteObj = async function(req, res, next) { try { result = await db.replaceOne({ "_id": originalObject["_id"] }, deletedObject) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (result.modifiedCount === 0) { //result didn't error out, the action was not performed. Sometimes, this is a neutral thing. Sometimes it is indicative of an error. err.message = "The original object was not replaced with the deleted object in the database." err.status = 500 - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } //204 to say it is deleted and there is nothing in the body console.log("Object deleted: " + preserveID) @@ -94,12 +90,11 @@ const deleteObj = async function(req, res, next) { //Not sure we can get here, as healHistoryTree might throw and error. err.message = "The history tree for the object being deleted could not be mended." err.status = 500 - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } err.message = "No object with this id could be found in RERUM. Cannot delete." err.status = 404 - next(utils.createExpressError(err)) + return next(utils.createExpressError(err)) } /** diff --git a/controllers/gog.js b/controllers/gog.js index 7bc72d17..7b22584c 100644 --- a/controllers/gog.js +++ b/controllers/gog.js @@ -24,6 +24,7 @@ import { _contextid, ObjectID, getAgentClaim, parseDocumentID, idNegotiation } f const _gog_fragments_from_manuscript = async function (req, res, next) { res.set("Content-Type", "application/json; charset=utf-8") const agent = getAgentClaim(req, next) + if (!agent) return const agentID = agent.split("/").pop() const manID = req.body["ManuscriptWitness"] const limit = parseInt(req.query.limit ?? 50) @@ -44,8 +45,7 @@ const _gog_fragments_from_manuscript = async function (req, res, next) { }) } if (err.status) { - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } try { let matches = [] @@ -138,7 +138,7 @@ const _gog_fragments_from_manuscript = async function (req, res, next) { } catch (error) { console.error(error) - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } @@ -156,6 +156,7 @@ const _gog_fragments_from_manuscript = async function (req, res, next) { const _gog_glosses_from_manuscript = async function (req, res, next) { res.set("Content-Type", "application/json; charset=utf-8") const agent = getAgentClaim(req, next) + if (!agent) return const agentID = agent.split("/").pop() const manID = req.body["ManuscriptWitness"] const limit = parseInt(req.query.limit ?? 50) @@ -176,8 +177,7 @@ const _gog_glosses_from_manuscript = async function (req, res, next) { }) } if (err.status) { - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } try { let matches = [] @@ -300,7 +300,7 @@ const _gog_glosses_from_manuscript = async function (req, res, next) { } catch (error) { console.error(error) - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } diff --git a/controllers/history.js b/controllers/history.js index 651568d8..e04e9cf4 100644 --- a/controllers/history.js +++ b/controllers/history.js @@ -23,16 +23,14 @@ const since = async function (req, res, next) { try { obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null === obj) { let err = { message: `Cannot produce a history. There is no object in the database with id '${id}'. Check the URL.`, status: 404 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } let all = await getAllVersions(obj) .catch(error => { @@ -60,16 +58,14 @@ const history = async function (req, res, next) { try { obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null === obj) { let err = { message: `Cannot produce a history. There is no object in the database with id '${id}'. Check the URL.`, status: 404 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } let all = await getAllVersions(obj) .catch(error => { @@ -103,9 +99,9 @@ const idHeadRequest = async function (req, res, next) { "message": `No RERUM object with id '${id}'`, "status": 404 } - next(utils.createExpressError(err)) + return next(utils.createExpressError(err)) } catch (error) { - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } @@ -128,9 +124,9 @@ const queryHeadRequest = async function (req, res, next) { "message": `There are no objects in the database matching the query. Check the request body.`, "status": 404 } - next(utils.createExpressError(err)) + return next(utils.createExpressError(err)) } catch (error) { - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } @@ -145,16 +141,14 @@ const sinceHeadRequest = async function (req, res, next) { try { obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null === obj) { let err = { message: `Cannot produce a history. There is no object in the database with id '${id}'. Check the URL.`, status: 404 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } let all = await getAllVersions(obj) .catch(error => { @@ -183,16 +177,14 @@ const historyHeadRequest = async function (req, res, next) { try { obj = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null === obj) { let err = { message: "Cannot produce a history. There is no object in the database with this id. Check the URL.", status: 404 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } let all = await getAllVersions(obj) .catch(error => { diff --git a/controllers/overwrite.js b/controllers/overwrite.js index e496e670..8a1e5a17 100644 --- a/controllers/overwrite.js +++ b/controllers/overwrite.js @@ -21,6 +21,7 @@ const overwrite = async function (req, res, next) { res.set("Content-Type", "application/json; charset=utf-8") let objectReceived = JSON.parse(JSON.stringify(req.body)) let agentRequestingOverwrite = getAgentClaim(req, next) + if (!agentRequestingOverwrite) return const receivedID = objectReceived["@id"] ?? objectReceived.id if (receivedID) { let id = parseDocumentID(receivedID) @@ -28,8 +29,7 @@ const overwrite = async function (req, res, next) { try { originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null === originalObject) { err = Object.assign(err, { @@ -84,8 +84,7 @@ const overwrite = async function (req, res, next) { try { result = await db.replaceOne({ "_id": id }, newObject) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (result.modifiedCount == 0) { //result didn't error out, the action was not performed. Sometimes, this is a neutral thing. Sometimes it is indicative of an error. @@ -108,7 +107,7 @@ const overwrite = async function (req, res, next) { status: 400 }) } - next(utils.createExpressError(err)) + return next(utils.createExpressError(err)) } export { overwrite } diff --git a/controllers/patchSet.js b/controllers/patchSet.js index 1d1f0932..b3459a7d 100644 --- a/controllers/patchSet.js +++ b/controllers/patchSet.js @@ -25,6 +25,7 @@ const patchSet = async function (req, res, next) { let originalContext let patchedObject = {} let generatorAgent = getAgentClaim(req, next) + if (!generatorAgent) return const receivedID = objectReceived["@id"] ?? objectReceived.id if (receivedID) { let id = parseDocumentID(receivedID) @@ -32,8 +33,7 @@ const patchSet = async function (req, res, next) { try { originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null === originalObject) { //This object is not in RERUM, they want to import it. Do that automatically. @@ -106,8 +106,7 @@ const patchSet = async function (req, res, next) { } catch (error) { //WriteError or WriteConcernError - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } } } @@ -118,7 +117,7 @@ const patchSet = async function (req, res, next) { status: 400 }) } - next(utils.createExpressError(err)) + return next(utils.createExpressError(err)) } export { patchSet } diff --git a/controllers/patchUnset.js b/controllers/patchUnset.js index 5d8f05f3..669b19d8 100644 --- a/controllers/patchUnset.js +++ b/controllers/patchUnset.js @@ -24,6 +24,7 @@ const patchUnset = async function (req, res, next) { let objectReceived = JSON.parse(JSON.stringify(req.body)) let patchedObject = {} let generatorAgent = getAgentClaim(req, next) + if (!generatorAgent) return const receivedID = objectReceived["@id"] ?? objectReceived.id if (receivedID) { let id = parseDocumentID(receivedID) @@ -31,8 +32,7 @@ const patchUnset = async function (req, res, next) { try { originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null === originalObject) { //This object is not in RERUM, they want to import it. Do that automatically. @@ -110,8 +110,7 @@ const patchUnset = async function (req, res, next) { } catch (error) { //WriteError or WriteConcernError - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } } } @@ -122,7 +121,7 @@ const patchUnset = async function (req, res, next) { status: 400 }) } - next(utils.createExpressError(err)) + return next(utils.createExpressError(err)) } export { patchUnset } diff --git a/controllers/patchUpdate.js b/controllers/patchUpdate.js index 942c61f9..5e7c9c27 100644 --- a/controllers/patchUpdate.js +++ b/controllers/patchUpdate.js @@ -23,6 +23,7 @@ const patchUpdate = async function (req, res, next) { let objectReceived = JSON.parse(JSON.stringify(req.body)) let patchedObject = {} let generatorAgent = getAgentClaim(req, next) + if (!generatorAgent) return const receivedID = objectReceived["@id"] ?? objectReceived.id if (receivedID) { let id = parseDocumentID(receivedID) @@ -30,8 +31,7 @@ const patchUpdate = async function (req, res, next) { try { originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null === originalObject) { //This object is not in RERUM, they want to import it. Do that automatically. @@ -109,8 +109,7 @@ const patchUpdate = async function (req, res, next) { } catch (error) { //WriteError or WriteConcernError - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } } } @@ -121,7 +120,7 @@ const patchUpdate = async function (req, res, next) { status: 400 }) } - next(utils.createExpressError(err)) + return next(utils.createExpressError(err)) } export { patchUpdate } diff --git a/controllers/putUpdate.js b/controllers/putUpdate.js index 3778dcf8..04d7f436 100644 --- a/controllers/putUpdate.js +++ b/controllers/putUpdate.js @@ -24,6 +24,7 @@ const putUpdate = async function (req, res, next) { res.set("Content-Type", "application/json; charset=utf-8") let objectReceived = JSON.parse(JSON.stringify(req.body)) let generatorAgent = getAgentClaim(req, next) + if (!generatorAgent) return const idReceived = objectReceived["@id"] ?? objectReceived.id if (idReceived) { if(!idReceived.includes(process.env.RERUM_ID_PREFIX)){ @@ -35,8 +36,7 @@ const putUpdate = async function (req, res, next) { try { originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (null === originalObject) { //This object is not found. @@ -82,8 +82,7 @@ const putUpdate = async function (req, res, next) { } catch (error) { //WriteError or WriteConcernError - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } } } @@ -94,7 +93,7 @@ const putUpdate = async function (req, res, next) { status: 400 }) } - next(utils.createExpressError(err)) + return next(utils.createExpressError(err)) } /** @@ -110,6 +109,7 @@ async function _import(req, res, next) { res.set("Content-Type", "application/json; charset=utf-8") let objectReceived = JSON.parse(JSON.stringify(req.body)) let generatorAgent = getAgentClaim(req, next) + if (!generatorAgent) return const id = ObjectID() let context = objectReceived["@context"] ? { "@context": objectReceived["@context"] } : {} let rerumProp = { "__rerum": utils.configureRerumOptions(generatorAgent, objectReceived, false, true)["__rerum"] } @@ -132,7 +132,7 @@ async function _import(req, res, next) { } catch (error) { //MongoServerError from the client has the following properties: index, code, keyPattern, keyValue - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } diff --git a/controllers/release.js b/controllers/release.js index 66fe534e..58b7c5d4 100644 --- a/controllers/release.js +++ b/controllers/release.js @@ -22,6 +22,7 @@ import { _contextid, ObjectID, getAgentClaim, parseDocumentID, idNegotiation, ge */ const release = async function (req, res, next) { let agentRequestingRelease = getAgentClaim(req, next) + if (!agentRequestingRelease) return let id = req.params["_id"] let slug let err = {"message":""} @@ -29,8 +30,7 @@ const release = async function (req, res, next) { if(req.get("Slug")){ let slug_json = await generateSlugId(req.get("Slug"), next) if(slug_json.code){ - next(utils.createExpressError(slug_json)) - return + return next(utils.createExpressError(slug_json)) } else{ slug = slug_json.slug_id @@ -42,8 +42,7 @@ const release = async function (req, res, next) { originalObject = await db.findOne({"$or":[{"_id": id}, {"__rerum.slug": id}]}) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } let safe_original = JSON.parse(JSON.stringify(originalObject)) let previousReleasedID = safe_original.__rerum.releases.previous @@ -68,8 +67,7 @@ const release = async function (req, res, next) { }) } if (err.status) { - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } if (null !== originalObject){ safe_original["__rerum"].isReleased = new Date(Date.now()).toISOString().replace("Z", "") @@ -102,8 +100,7 @@ const release = async function (req, res, next) { result = await db.replaceOne({ "_id": id }, releasedObject) } catch (error) { - next(utils.createExpressError(error)) - return + return next(utils.createExpressError(error)) } if (result.modifiedCount == 0) { //result didn't error out, the action was not performed. Sometimes, this is a neutral thing. Sometimes it is indicative of an error. @@ -124,8 +121,7 @@ const release = async function (req, res, next) { message: "You must provide the id of an object to release. Use /release/id-here or release?_id=id-here.", status: 400 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } } diff --git a/controllers/search.js b/controllers/search.js index 071a1f58..f90d935c 100644 --- a/controllers/search.js +++ b/controllers/search.js @@ -269,8 +269,7 @@ const searchAsWords = async function (req, res, next) { message: "You did not provide text to search for in the search request.", status: 400 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } const limit = parseInt(req.query.limit ?? 100) const skip = parseInt(req.query.skip ?? 0) @@ -287,7 +286,7 @@ const searchAsWords = async function (req, res, next) { res.json(results) } catch (error) { console.error(error) - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } @@ -357,8 +356,7 @@ const searchAsPhrase = async function (req, res, next) { message: "You did not provide text to search for in the search request.", status: 400 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } const limit = parseInt(req.query.limit ?? 100) const skip = parseInt(req.query.skip ?? 0) @@ -375,7 +373,7 @@ const searchAsPhrase = async function (req, res, next) { res.json(results) } catch (error) { console.error(error) - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } @@ -437,8 +435,7 @@ const searchFuzzily = async function (req, res, next) { message: "You did not provide text to search for in the search request.", status: 400 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } const limit = parseInt(req.query.limit ?? 100) const skip = parseInt(req.query.skip ?? 0) @@ -455,7 +452,7 @@ const searchFuzzily = async function (req, res, next) { res.json(results) } catch (error) { console.error(error) - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } @@ -525,8 +522,7 @@ const searchWildly = async function (req, res, next) { message: "You did not provide text to search for in the search request.", status: 400 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } // Require wildcards in the search text if (!searchText.includes('*') && !searchText.includes('?')) { @@ -534,8 +530,7 @@ const searchWildly = async function (req, res, next) { message: "Wildcards must be used in wildcard search. Use '*' to match any characters or '?' to match a single character.", status: 400 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } const limit = parseInt(req.query.limit ?? 100) const skip = parseInt(req.query.skip ?? 0) @@ -552,7 +547,7 @@ const searchWildly = async function (req, res, next) { res.json(results) } catch (error) { console.error(error) - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } } @@ -629,8 +624,7 @@ const searchAlikes = async function (req, res, next) { message: "You must provide a JSON document in the request body to find similar documents.", status: 400 } - next(utils.createExpressError(err)) - return + return next(utils.createExpressError(err)) } const limit = parseInt(req.query.limit ?? 100) const skip = parseInt(req.query.skip ?? 0) @@ -686,7 +680,7 @@ const searchAlikes = async function (req, res, next) { res.json(results) } catch (error) { console.error(error) - next(utils.createExpressError(error)) + return next(utils.createExpressError(error)) } }