Move the handleError function into the LinkedFilesController

This commit is contained in:
Shane Kilkelly 2018-06-22 11:14:04 +01:00
parent ebe828aa62
commit 096d3f28a1
2 changed files with 52 additions and 42 deletions

View file

@ -4,8 +4,19 @@ ProjectLocator = require '../Project/ProjectLocator'
Settings = require 'settings-sharelatex'
logger = require 'logger-sharelatex'
_ = require 'underscore'
LinkedFilesErrors = require './LinkedFilesErrors'
LinkedFilesHandler = require './LinkedFilesHandler'
{
UrlFetchFailedError,
InvalidUrlError,
OutputFileFetchFailedError,
AccessDeniedError,
BadEntityTypeError,
BadDataError,
ProjectNotFoundError,
V1ProjectNotFoundError,
SourceFileNotFoundError,
} = require './LinkedFilesErrors'
module.exports = LinkedFilesController = {
@ -41,7 +52,7 @@ module.exports = LinkedFilesController = {
parent_folder_id,
user_id,
(err, newFileId) ->
return LinkedFilesErrors.handleError(err, req, res, next) if err?
return LinkedFilesController.handleError(err, req, res, next) if err?
res.json(new_file_id: newFileId)
refreshLinkedFile: (req, res, next) ->
@ -68,7 +79,44 @@ module.exports = LinkedFilesController = {
parent_folder_id,
user_id,
(err, newFileId) ->
return LinkedFilesErrors.handleError(err, req, res, next) if err?
return LinkedFilesController.handleError(err, req, res, next) if err?
res.json(new_file_id: newFileId)
handleError: (error, req, res, next) ->
if error instanceof BadDataError
res.status(400).send("The submitted data is not valid")
else if error instanceof AccessDeniedError
res.status(403).send("You do not have access to this project")
else if error instanceof BadDataError
res.status(400).send("The submitted data is not valid")
else if error instanceof BadEntityTypeError
res.status(400).send("The file is the wrong type")
else if error instanceof SourceFileNotFoundError
res.status(404).send("Source file not found")
else if error instanceof ProjectNotFoundError
res.status(404).send("Project not found")
else if error instanceof V1ProjectNotFoundError
res.status(409).send("Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file")
else if error instanceof OutputFileFetchFailedError
res.status(404).send("Could not get output file")
else if error instanceof UrlFetchFailedError
res.status(422).send(
"Your URL could not be reached (#{error.statusCode} status code). Please check it and try again."
)
else if error instanceof InvalidUrlError
res.status(422).send(
"Your URL is not valid. Please check it and try again."
)
else
next(error)
}

View file

@ -81,42 +81,4 @@ module.exports = {
ProjectNotFoundError,
V1ProjectNotFoundError,
SourceFileNotFoundError,
handleError: (error, req, res, next) ->
if error instanceof BadDataError
res.status(400).send("The submitted data is not valid")
else if error instanceof AccessDeniedError
res.status(403).send("You do not have access to this project")
else if error instanceof BadDataError
res.status(400).send("The submitted data is not valid")
else if error instanceof BadEntityTypeError
res.status(400).send("The file is the wrong type")
else if error instanceof SourceFileNotFoundError
res.status(404).send("Source file not found")
else if error instanceof ProjectNotFoundError
res.status(404).send("Project not found")
else if error instanceof V1ProjectNotFoundError
res.status(409).send("Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file")
else if error instanceof OutputFileFetchFailedError
res.status(404).send("Could not get output file")
else if error instanceof UrlFetchFailedError
res.status(422).send(
"Your URL could not be reached (#{error.statusCode} status code). Please check it and try again."
)
else if error instanceof InvalidUrlError
res.status(422).send(
"Your URL is not valid. Please check it and try again."
)
else
next(error)
}