From 53fa15d3db60c6a5fc631dd0478b4142b45450d7 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Mon, 30 Jun 2014 17:21:19 +0100 Subject: [PATCH] added basic error handling around templates rendering --- .../Templates/TemplatesWebController.coffee | 25 +++++++++++++++---- .../TemplatesWebControllerTests.coffee | 4 ++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/services/web/app/coffee/Features/Templates/TemplatesWebController.coffee b/services/web/app/coffee/Features/Templates/TemplatesWebController.coffee index 7686186ffb..330d5fe9ca 100644 --- a/services/web/app/coffee/Features/Templates/TemplatesWebController.coffee +++ b/services/web/app/coffee/Features/Templates/TemplatesWebController.coffee @@ -7,13 +7,19 @@ module.exports = TemplatesWebController = renderTemplatesIndexPage: (req, res)-> logger.log "rendering index page of templates" TemplatesWebController._getDataFromTemplatesApi "/user/#{req.params.user_id}", (err, data)-> + if err? + logger.err err:err, "something went wrong in renderTemplatesIndexPage" + return res.send 500 data.title = "LaTeX Templates" res.render "templates/index", data renerTemplateInTag: (req, res)-> - logger.log "rendering latex template page" {user_id, tag_name, template_name} = req.params + logger.log user_id:user_id, tag_name:tag_name, template_name:template_name, "rendering latex template page" TemplatesWebController._getDataFromTemplatesApi "/user/#{user_id}/tag/#{tag_name}/template/#{template_name}", (err, data)-> + if err? + logger.err err:err, user_id:user_id, tag_name:tag_name, template_name:template_name, "something went wrong in renerTemplateInTag" + return res.send 500 data.title = data.template.name res.render "templates/template", data @@ -37,28 +43,37 @@ module.exports = TemplatesWebController = logger.error err: error, "templates proxy API error" res.send 500 - _renderCanonicalPage: _renderCanonicalPage = (req, res)-> + _renderCanonicalPage: (req, res)-> {user_id, template_id} = req.params logger.log user_id:user_id, template_id:template_id, "rendering template page" TemplatesWebController._getDataFromTemplatesApi "/user/#{user_id}/template/#{template_id}", (err, data)-> + if err? + logger.err err:err, user_id:user_id, template_id:template_id, "something went wrong in _renderCanonicalPage" + return res.send 500 data.tag = null res.render "templates/template", data - _renderAllTemplatesPage: _renderAllTemplatesPage = (req, res)-> + _renderAllTemplatesPage: (req, res)-> {user_id} = req.params logger.log user_id:user_id, "rendering all templates page" TemplatesWebController._getDataFromTemplatesApi "/user/#{user_id}/all", (err, data)-> + if err? + logger.err err:err, user_id:user_id, "something went wrong in _renderCanonicalPage" + return res.send 500 data.title = "All Templates" res.render "templates/tag", data - _renderTagPage: _renderTagPage = (req, res)-> + _renderTagPage: (req, res)-> {user_id, tag_name} = req.params logger.log user_id:user_id, tag_name:tag_name, "rendinging tag page for templates" TemplatesWebController._getDataFromTemplatesApi "/user/#{user_id}/tag/#{tag_name}", (err, data)-> + if err? + logger.err err:err, user_id:user_id, tag_name:tag_name, "something went wrong in _renderCanonicalPage" + return res.send 500 data.title = data.tag.name res.render "templates/tag", data - _getDataFromTemplatesApi: _getDataFromTemplatesApi = (path, callback)-> + _getDataFromTemplatesApi: (path, callback)-> opts = url: "#{settings.apis.templates_api.url}#{path}" json:true diff --git a/services/web/test/UnitTests/coffee/Templates/TemplatesWebControllerTests.coffee b/services/web/test/UnitTests/coffee/Templates/TemplatesWebControllerTests.coffee index de0434f04a..411ac437d7 100644 --- a/services/web/test/UnitTests/coffee/Templates/TemplatesWebControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Templates/TemplatesWebControllerTests.coffee @@ -16,7 +16,9 @@ describe "TemplatesWebController", -> url:"templates.sharelatex.env" @TemplatesWebController = SandboxedModule.require modulePath, requires: "settings-sharelatex":@settings - "logger-sharelatex": log:-> + "logger-sharelatex": + log:-> + err:-> @stubbedApiData = template:{_id:"12312321", name:"bob"} tag: {name:"tag name"}