Merge pull request #1344 from sharelatex/bg-handle-filestore-errors

handle non-sucess responses from filestore copy

GitOrigin-RevId: f00766066a5a2cf20116aa9c8d876f78fae8953f
This commit is contained in:
Brian Gough 2019-01-15 10:39:50 +00:00 committed by sharelatex
parent da6711dc99
commit 724e2e8fcd
3 changed files with 32 additions and 5 deletions

View file

@ -99,10 +99,17 @@ module.exports = FileStoreHandler =
file_id:oldFile_id
uri: @_buildUrl(newProject_id, newFile_id)
timeout:fiveMinsInMs
request opts, (err)->
request opts, (err, response)->
if err?
logger.err err:err, oldProject_id:oldProject_id, oldFile_id:oldFile_id, newProject_id:newProject_id, newFile_id:newFile_id, "something went wrong telling filestore api to copy file"
callback(err, opts.uri)
callback(err)
else if 200 <= response.statusCode < 300
# successful response
callback(null, opts.uri)
else
err = new Error("non-ok response from filestore for copyFile: #{response.statusCode}")
logger.err {uri: opts.uri, statusCode: response.statusCode}, "error uploading to filestore"
callback(err)
_buildUrl: (project_id, file_id)->
return "#{settings.apis.filestore.url}/project/#{project_id}/file/#{file_id}"

View file

@ -1,4 +1,5 @@
express = require("express")
bodyParser = require "body-parser"
app = express()
module.exports = MockFileStoreApi =
@ -22,6 +23,18 @@ module.exports = MockFileStoreApi =
{ content } = @files[project_id][file_id]
res.send content
# handle file copying
app.put "/project/:project_id/file/:file_id", bodyParser.json(), (req, res, next) =>
{project_id, file_id} = req.params
source = req.body.source
{content} = @files[source.project_id]?[source.file_id]
if !content?
res.sendStatus 500
else
@files[project_id] ?= {}
@files[project_id][file_id] = { content }
res.sendStatus 200
app.listen 3009, (error) ->
throw error if error?
.on "error", (error) ->

View file

@ -210,7 +210,7 @@ describe "FileStoreHandler", ->
@newFile_id = "new file id"
it "should post json", (done)->
@request.callsArgWith(1, null)
@request.callsArgWith(1, null, {statusCode: 200})
@handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, =>
@request.args[0][0].method.should.equal "put"
@ -220,13 +220,13 @@ describe "FileStoreHandler", ->
done()
it "builds the correct url", (done)->
@request.callsArgWith(1, null)
@request.callsArgWith(1, null, {statusCode: 200})
@handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, =>
@handler._buildUrl.calledWith(@newProject_id, @newFile_id).should.equal true
done()
it "returns the url", (done)->
@request.callsArgWith(1, null)
@request.callsArgWith(1, null, {statusCode: 200})
@handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, (err, url) =>
url.should.equal "http://filestore.stubbedBuilder.com"
done()
@ -237,3 +237,10 @@ describe "FileStoreHandler", ->
@handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, (err)=>
err.should.equal error
done()
it "should return an error for a non-success statusCode", (done)->
@request.callsArgWith(1, null, {statusCode: 500})
@handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, (err)=>
err.should.be.an('error')
err.message.should.equal 'non-ok response from filestore for copyFile: 500'
done()