Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created September 19, 2014 09:40
Show Gist options
  • Save ishiduca/0e505391e48747fdcc4b to your computer and use it in GitHub Desktop.
Save ishiduca/0e505391e48747fdcc4b to your computer and use it in GitHub Desktop.
use Domain in 'carom.js app'
var app = Object.create(require('carom.js')).constructor()
function onError (err, req, res) {
console.error(err)
console.error(err.stack)
res.writeHead(500)
res.end(String(err))
}
app.use('favicon')
app.use('doma', onError)
app.use('router', {app: app})
app.use('404')
app.router.GET('/', function (req, res) {
throw new Error('test throw error !!')
})
app.router.GET('/domain', function (req, res) {
var domain = this.d
require('fs').readFile('no_exists.json', 'utf8', domain.intercept(function (data) {
res.writeHead(200,{'content-type': 'application/json; charset=utf-8'})
res.end(data)
}))
})
var port = 3002
app.server.listen(port, function (err) {
if (err) return console.error(err.stack)
console.log('server start to listen on port "%d"', port)
})
module.exports = function (onError) {
if ('function' !== typeof onError)
throw new TypeError('"onError" must be "function"')
var domain = require('domain')
return function errorhandling (req, res, next) {
var context = this
var d = this.d = domain.create()
d.on('error', function (err) {
onError.apply(context, [err, req, res])
})
d.run(next)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment