/**
 * Module dependencies
 */

var qs = require('qs')

/**
 * Error Response
 */

function error (err, req, res, next) {
  // 302 Redirect
  if (err.statusCode === 302 && err.redirect_uri) {
    var responseMode = '?' || '#'
    var error = { error: err.error, error_description: err.error_description }
    var uri = err.redirect_uri + responseMode + qs.stringify(error)

    res.redirect(uri)

  // 400 Bad Request
  } else if (err.statusCode === 400) {
    res.set({
      'Cache-Control': 'no-store',
      'Pragma': 'no-cache'
    })

    res.status(400).json({
      error: err.error || err.message,
      error_description: err.error_description,
      error_uri: err.error_uri
    })

  // 401 Unauthorized
  } else if (err.statusCode === 401) {
    // rfc 6750 Bearer Token
    // http://tools.ietf.org/html/rfc6750#section-3
    res.set({
      'WWW-Authenticate': 'Bearer ' +
        'realm="' + err.realm + '", ' +
        'error="' + err.error + '", ' +
        'error_description="' + err.error_description + '"'
    })

    res.status(401).send('Unauthorized')

  // 403 Forbidden
  } else if (err.statusCode === 403) {
    res.status(403).send('Forbidden')

  // 500 Internal Server Error
  } else {
    var statusCode = err.statusCode || 500
    var message = (err.statusCode && err.message) || 'Internal Server Error'

    res.status(statusCode).send(message)
  }

}

/**
 * Exports
 */

module.exports = error

@@ -121,105 +121,444 @@
 %7B%0A  
-// 302 Redirect%0A  if (err.statusCode === 302 && err.redirect_uri) %7B%0A    var responseMode = '?' %7C%7C
+var message%0A  var errorName = err.error %7C%7C err.message %7C%7C err.name%0A  var errorDescription = err.error_description %7C%7C err.description %7C%7C err.message%0A  // 302 Redirect%0A  if (err.statusCode === 302 && err.redirect_uri) %7B%0A    var params = req.connectParams %7C%7C req.query%0A    var responseMode = (params.response_mode && params.response_mode.trim()) %7C%7C%0A      (params.response_type === 'code' %7C%7C%0A        params.response_type === 'none') ? '?' :
  '#'
@@ -583,25 +583,25 @@
  error: 
-err.
 error
+Name
 , error_
@@ -612,32 +612,27 @@
 ription: err
-.error_d
+orD
 escription %7D
@@ -911,36 +911,21 @@
 ror: err
-.error %7C%7C err.messag
+orNam
 e,%0A     
@@ -943,32 +943,27 @@
 ription: err
-.error_d
+orD
 escription,%0A
@@ -1258,22 +1258,22 @@
 %22' + err
-.error
+orName
  + '%22, '
@@ -1306,32 +1306,27 @@
 ion=%22' + err
-.error_d
+orD
 escription +
@@ -1376,17 +1376,44 @@
 thorized
-'
+%3Cbr%3E%3Cbr%3E' + errorDescription
 )%0A%0A  // 
@@ -1461,24 +1461,153 @@
  === 403) %7B%0A
+    message = 'Forbidden'%0A%0A    if (req.app.get('env') === 'development') %7B%0A      message += '%3Cbr%3E%3Cbr%3E' + errorDescription%0A    %7D%0A%0A
     res.stat
@@ -1619,27 +1619,23 @@
 3).send(
-'Forbidden'
+message
 )%0A%0A  // 
@@ -1718,20 +1718,16 @@
 500%0A    
-var 
 message 
@@ -1788,16 +1788,118 @@
 Error'%0A%0A
+    if (req.app.get('env') === 'development') %7B%0A      message += '%3Cbr%3E%3Cbr%3E' + errorDescription%0A    %7D%0A%0A
     res.

Improve error-handling and display