From ee1b32eee162003986fc460992a610f7b74c54e7 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Mon, 21 May 2018 10:12:41 +0100 Subject: [PATCH] Check for case where the source file is not found --- .../LinkedFiles/ProjectFileAgent.coffee | 18 ++++++++++++++++-- .../Features/LinkedFiles/UrlAgent.coffee | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/services/web/app/coffee/Features/LinkedFiles/ProjectFileAgent.coffee b/services/web/app/coffee/Features/LinkedFiles/ProjectFileAgent.coffee index f71889fd85..3e434d5361 100644 --- a/services/web/app/coffee/Features/LinkedFiles/ProjectFileAgent.coffee +++ b/services/web/app/coffee/Features/LinkedFiles/ProjectFileAgent.coffee @@ -32,6 +32,14 @@ BadDataError = (message) -> BadDataError.prototype.__proto__ = Error.prototype +FileNotFoundError = (message) -> + error = new Error(message) + error.name = 'BadData' + error.__proto__ = FileNotFoundError.prototype + return error +FileNotFoundError.prototype.__proto__ = Error.prototype + + module.exports = ProjectFileAgent = sanitizeData: (data) -> @@ -58,7 +66,11 @@ module.exports = ProjectFileAgent = project_id: source_project_id, path: source_entity_path }, (err, entity, type) -> - return callback(err) if err? # also applies when file not found + # return callback(err) if err? # also applies when file not found + if err? + if err.toString().match(/^not found.*/) + err = new FileNotFoundError() + return callback(err) ProjectFileAgent._writeEntityToDisk source_project_id, entity._id, type, callback _writeEntityToDisk: (project_id, entity_id, type, callback=(err, location)->) -> @@ -80,7 +92,9 @@ module.exports = ProjectFileAgent = else if error instanceof BadDataError res.status(400).send("The submitted data is not valid") else if error instanceof BadEntityTypeError - res.status(404).send("The file is the wrong type") + res.status(400).send("The file is the wrong type") + else if error instanceof FileNotFoundError + res.status(404).send("File not found") else next(error) next() diff --git a/services/web/app/coffee/Features/LinkedFiles/UrlAgent.coffee b/services/web/app/coffee/Features/LinkedFiles/UrlAgent.coffee index ad96aa628f..567a1b4c39 100644 --- a/services/web/app/coffee/Features/LinkedFiles/UrlAgent.coffee +++ b/services/web/app/coffee/Features/LinkedFiles/UrlAgent.coffee @@ -65,4 +65,4 @@ module.exports = UrlAgent = { if !Settings.apis?.linkedUrlProxy?.url? throw new Error('no linked url proxy configured') return "#{Settings.apis.linkedUrlProxy.url}?url=#{encodeURIComponent(url)}" -} \ No newline at end of file +}