module.exports = errors

var STATUS_CODES = require("http").STATUS_CODES
, url = require("url")

function errors (er, req, res) {
  if (!req || !res) return errors.bind(null, er)

  var code, message, u

  u = url.parse(req.url)
  delete u.auth
  u = url.format(u)

  if (typeof er === "number") {
    code = er
    message = STATUS_CODES[code]
  } else if (typeof er === "string") {
    message = er
    code = 500
  } else if (er instanceof Error) {
    code = er.statusCode || 500
    message = er.message
  } else if (er) {
    code = er.statusCode || 500
    message = er.message || STATUS_CODES[code]
  }

  console.error("Error %s", req.url, req.headers, code, message, er)

  // serve in either html or json
  var neg = req.negotiator
  , avail = ['text/html', 'application/json', 'text/plain']
  , mt = neg.preferredMediaType(avail) || 'text/plain'

  res.statusCode = code
  res.setHeader('content-type', mt)
  var msg
  switch (mt) {
    case 'text/html':
      // XXX templates
      msg = new Buffer("<html><body>" + code + " " + message 
                      + '\n' + u)
      break

    case 'application/json':
      msg = new Buffer(JSON.stringify(
        { ok:false, code:code, message: message, url: u}))
      break

    case 'text/plain':
      msg = new Buffer(code + " " + message + "\n" + u)
  }
  res.setHeader('content-length', msg.length)
  res.end(msg)
}

@@ -605,16 +605,76 @@
 e%5D%0A  %7D%0A%0A
+  if (er && er.stack) %7B%0A    message += '%5Cn' + er.stack%0A  %7D%0A%0A
   consol
@@ -1079,16 +1079,21 @@
 l%3E%3Cbody%3E
+%3Cpre%3E
 %22 + code

Print stacks, for now at least.