Merge pull request #2381 from overleaf/ho-template-error-handling

Template error handling & blog routes removal

GitOrigin-RevId: 849423e19bbb5291ef25ed9612f49bdc67dae330
This commit is contained in:
Timothée Alby 2019-11-27 18:27:19 +05:30 committed by sharelatex
parent 508556c835
commit 9af55a11f7
3 changed files with 0 additions and 208 deletions

View file

@ -1,96 +0,0 @@
/* eslint-disable
max-len,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let BlogController
const request = require('request')
const settings = require('settings-sharelatex')
const logger = require('logger-sharelatex')
const _ = require('underscore')
const ErrorController = require('../Errors/ErrorController')
module.exports = BlogController = {
getPage(req, res, next) {
const url = req.url != null ? req.url.toLowerCase() : undefined
const blogUrl = `${settings.apis.blog.url}${url}`
const extensionsToProxy = [
'.png',
'.xml',
'.jpeg',
'.jpg',
'.json',
'.zip',
'.eps',
'.gif'
]
const shouldProxy = _.find(
extensionsToProxy,
extension => url.indexOf(extension) !== -1
)
if (shouldProxy) {
return BlogController._directProxy(blogUrl, res)
}
return request.get(blogUrl, function(err, r, data) {
if (
(r != null ? r.statusCode : undefined) === 404 ||
(r != null ? r.statusCode : undefined) === 403
) {
return ErrorController.notFound(req, res, next)
}
if (err != null) {
return res.send(500)
}
data = data.trim()
try {
data = JSON.parse(data)
if (settings.cdn && settings.cdn.web && settings.cdn.web.host) {
if (data != null) {
data.content = __guard__(
data != null ? data.content : undefined,
x1 =>
x1.replace(
/src="(\/[^"]+)"/g,
`src='${settings.cdn.web.host}$1'`
)
)
}
}
} catch (error) {
err = error
logger.err({ err, data }, 'error parsing data from data')
}
return res.render('blog/blog_holder', data)
})
},
getIndexPage(req, res) {
req.url = '/blog/index.html'
return BlogController.getPage(req, res)
},
_directProxy(originUrl, res) {
const upstream = request.get(originUrl)
upstream.on('error', error =>
logger.error({ err: error }, 'blog proxy error')
)
return upstream.pipe(res)
}
}
function __guard__(value, transform) {
return typeof value !== 'undefined' && value !== null
? transform(value)
: undefined
}

View file

@ -32,7 +32,6 @@ const ExportsController = require('./Features/Exports/ExportsController')
const PasswordResetRouter = require('./Features/PasswordReset/PasswordResetRouter') const PasswordResetRouter = require('./Features/PasswordReset/PasswordResetRouter')
const StaticPagesRouter = require('./Features/StaticPages/StaticPagesRouter') const StaticPagesRouter = require('./Features/StaticPages/StaticPagesRouter')
const ChatController = require('./Features/Chat/ChatController') const ChatController = require('./Features/Chat/ChatController')
const BlogController = require('./Features/Blog/BlogController')
const Modules = require('./infrastructure/Modules') const Modules = require('./infrastructure/Modules')
const RateLimiterMiddleware = require('./Features/Security/RateLimiterMiddleware') const RateLimiterMiddleware = require('./Features/Security/RateLimiterMiddleware')
const InactiveProjectController = require('./Features/InactiveData/InactiveProjectController') const InactiveProjectController = require('./Features/InactiveData/InactiveProjectController')
@ -107,11 +106,6 @@ function initialize(webRouter, privateApiRouter, publicApiRouter) {
) )
} }
if (Settings.overleaf == null) {
webRouter.get('/blog', BlogController.getIndexPage)
webRouter.get('/blog/*', BlogController.getPage)
}
webRouter.get('/user/activate', UserPagesController.activateAccountPage) webRouter.get('/user/activate', UserPagesController.activateAccountPage)
AuthenticationController.addEndpointToLoginWhitelist('/user/activate') AuthenticationController.addEndpointToLoginWhitelist('/user/activate')

View file

@ -1,106 +0,0 @@
/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const should = require('chai').should()
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
const path = require('path')
const sinon = require('sinon')
const modulePath = path.join(
__dirname,
'../../../../app/src/Features/Blog/BlogController'
)
const { expect } = require('chai')
describe('BlogController', function() {
beforeEach(function() {
this.settings = {
apis: {
blog: {
url: 'http://blog.sharelatex.env'
}
},
cdn: { web: { host: null } }
}
this.request = { get: sinon.stub() }
this.ErrorController = {}
this.BlogController = SandboxedModule.require(modulePath, {
globals: {
console: console
},
requires: {
'settings-sharelatex': this.settings,
'logger-sharelatex': {
log() {}
},
'../Errors/ErrorController': this.ErrorController,
request: this.request
}
})
this.req = {}
return (this.res = {})
})
describe('getPage', function() {
it('should get the data from the blog api', function(done) {
this.req.url = '/blog/something.html'
const body = { stuff: 'here' }
this.request.get.callsArgWith(1, null, null, JSON.stringify(body))
this.res.render = (view, data) => {
this.request.get.calledWith(
`${this.settings.apis.blog.url}${this.req.url}`
)
view.should.equal('blog/blog_holder')
assert.deepEqual(body, data)
return done()
}
return this.BlogController.getPage(this.req, this.res)
})
it('should send to the error controller if the blog responds 404', function(done) {
this.req.url = '/blog/something.html'
this.request.get.callsArgWith(1, null, { statusCode: 404 })
this.ErrorController.notFound = (req, res) => {
assert.deepEqual(req, this.req)
assert.deepEqual(res, this.res)
return done()
}
return this.BlogController.getPage(this.req, this.res)
})
it('should proxy the image urls', function(done) {
this.BlogController._directProxy = sinon.stub()
this.req.url = '/something.png'
this.BlogController.getPage(this.req, this.res)
this.BlogController._directProxy
.calledWith(`${this.settings.apis.blog.url}${this.req.url}`, this.res)
.should.equal(true)
return done()
})
})
describe('getIndexPage', function() {
it('should change the url and send it to getPage', function(done) {
this.req.url = '/blog'
this.BlogController.getPage = function(req, res) {
req.url.should.equal('/blog/index.html')
return done()
}
return this.BlogController.getIndexPage(this.req, this.res)
})
})
})