Skip to content

Commit

Permalink
added default error on sendFile catch if not explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Apr 24, 2018
1 parent c88de6b commit dd147a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ class RESPONSE {
} catch(e) { // TODO: Add second catch?
// Execute callback with caught error
await fn(e)
this.error(e) // Throw error if not done in callback
}

} // end sendFile
Expand Down Expand Up @@ -353,7 +354,7 @@ class RESPONSE {

// Sends the request to the main callback
send(body) {

// Create the response
const response = {
headers: this._headers,
Expand All @@ -370,8 +371,8 @@ class RESPONSE {


// Trigger API error
error(err) {
this.app.catchErrors(err)
error(e) {
this.app.catchErrors(e)
} // end error

} // end Response class
Expand Down
30 changes: 29 additions & 1 deletion test/sendFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ api.get('/sendfile/err', function(req,res) {

api.get('/sendfile/test', function(req,res) {
res.sendFile('test/test.txt' + (req.query.test ? req.query.test : ''), err => {

// Return a promise
return Promise.try(() => {
for(let i = 0; i<40000000; i++) {}
Expand All @@ -66,6 +65,23 @@ api.get('/sendfile/test', function(req,res) {
})
})

api.get('/sendfile/error', function(req,res) {
res.sendFile('test/test.txtx', err => {

// Return a promise
return Promise.try(() => {
for(let i = 0; i<40000000; i++) {}
return true
}).then((x) => {
if (err) {
// log error
return true
}
})

})
})

api.get('/sendfile/buffer', function(req,res) {
res.sendFile(fs.readFileSync('test/test.txt'))
})
Expand Down Expand Up @@ -229,6 +245,18 @@ describe('SendFile Tests:', function() {



it('Text file error w/ callback override (promise - no end)', function() {
let _event = Object.assign({},event,{ path: '/sendfile/error', queryStringParameters: { test: 'x' } })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json','x-error': 'true' }, statusCode: 500, body: result.body, isBase64Encoded: false })
})
}) // end it



it('Buffer Input', function() {
let _event = Object.assign({},event,{ path: '/sendfile/buffer' })

Expand Down

0 comments on commit dd147a3

Please sign in to comment.