Skip to content

Commit

Permalink
refactor run callback
Browse files Browse the repository at this point in the history
add Server.error
fix bug in run callback (accepts not defined)
fix bug in stream.body event (part is undefined)
  • Loading branch information
Aaron Heckmann authored and tj committed Mar 9, 2010
1 parent 680c07a commit 0a225c1
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions lib/express/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,11 @@ Server = Class({
request.setBodyEncoding('binary')
var pendingFiles = 0
function callback(e, result) {
if (!e)
if (!pendingFiles) return self.route(request, response)
if (!e) {
if (!pendingFiles) self.route(request, response)
}
else
// TODO: refactore -- same code used in #route(); make util
if (request.accepts('html') && set('show exceptions'))
request.halt(500, require('express/pages/show-exceptions').render(request, e))
else
request.halt(500)
if (set('throw exceptions'))
throw error
else self.route(request, response)
self.error(e, request, response)
}
if (request.headers['content-type'] &&
request.headers['content-type'].indexOf('multipart/form-data') !== -1) {
Expand All @@ -245,9 +239,9 @@ Server = Class({
.addListener('partEnd', function(part) {
if (!part.name) return
if (part.fileStream)
part.fileStream.close(function(){
part.fileStream.close(function(){
--pendingFiles
callback(null)
callback()
}),
utils.mergeParam(part.name, { filename: part.filename, tempfile: part.tempfile }, request.params.post)
else
Expand Down Expand Up @@ -282,15 +276,29 @@ Server = Class({
request.halt(200, body)
}
catch (e) {
if (e instanceof ExpressError)
throw e
if (request.accepts('html') && set('show exceptions'))
request.halt(500, require('express/pages/show-exceptions').render(request, e))
else
request.halt(500)
if (set('throw exceptions'))
throw e
this.error(e, request)
}
},

/**
* Handle errors
* @param {object} e
* @param {object} request
* @param {object} response
* @api private
*/
error: function (e, request, response) {
if (e instanceof ExpressError)
throw e
if (!(request instanceof Request))
request = new Request(request, response),
request.trigger('request')
if (request.accepts('html') && set('show exceptions'))
request.halt(500, require('express/pages/show-exceptions').render(request, e))
else
request.halt(500)
if (set('throw exceptions'))
throw e
}
})

Expand Down

0 comments on commit 0a225c1

Please sign in to comment.