'use strict'

const Env = use('Env')
const Ouch = use('youch')
const Http = exports = module.exports = {}

/**
 * handle errors occured during a Http request.
 *
 * @param  {Object} error
 * @param  {Object} request
 * @param  {Object} response
 */
Http.handleError = function * (error, request, response) {
  /**
   * DEVELOPMENT REPORTER
   */
  if (Env.get('NODE_ENV') === 'development') {
    const ouch = new Ouch().pushHandler(
      new Ouch.handlers.PrettyPageHandler('blue', null, 'sublime')
    )
    ouch.handleException(error, request.request, response.response, (output) => {
      console.error(error.stack)
    })
    return
  }

  /**
   * PRODUCTION REPORTER
   */
  const status = error.status || 500
  console.error(error.stack)
  yield response.status(status).sendView('errors/index', {error})
}

/**
 * listener for Http.start event, emitted after
 * starting http server.
 */
Http.onStart = function () {
}

@@ -36,17 +36,18 @@
 )%0Aconst 
-O
+Yo
 uch = us
@@ -302,16 +302,54 @@
 onse) %7B%0A
+  const status = error.status %7C%7C 500%0A%0A
   /**%0A  
@@ -435,16 +435,17 @@
   const 
+y
 ouch = n
@@ -451,221 +451,254 @@
 new 
-O
+Yo
 uch(
-).pushHandler(%0A      new Ouch.handlers.PrettyPageHandler('blue', null, 'sublime')%0A    )%0A    ouch.handleException(error, request.request, response.response, (output) =%3E %7B%0A      console.error(error.stack)%0A    %7D
+error, request.request)%0A    const type = request.accepts('json', 'html')%0A    const formatMethod = type === 'json' ? 'toJSON' : 'toHTML'%0A    const formattedErrors = yield youch%5BformatMethod%5D()%0A    response.status(status).send(formattedErrors
 )%0A  
@@ -746,51 +746,14 @@
 TER%0A
+
    */%0A
-  const status = error.status %7C%7C 500%0A
   co

use newer youch for error display