function handleRequestError(err, req, res, next) {
    // In order to make errors visible, we have to set CORS for em.
    let intentObj;
    if (req.intent) {
      intentObj = req.intent;
      delete req.intent;
    }
    setCors.call(this, req, res, req.method);
    let reqErr,
      reqData,
      statusCode = 400;
    if (err instanceof SyntaxError) {  // any other error, we bubble up.
      reqErr = thorin.error(PARSE_ERROR_CODE, 'Invalid payload.', err);
      statusCode = 400;
      reqData = {
        error: reqErr.toJSON()
      };
    } else if (err instanceof Error && err.name.indexOf('Thorin') === 0) {
      reqErr = err;
      statusCode = err.statusCode || 400;
      reqData = {
        error: reqErr.toJSON()
      };
    } else if (typeof err === 'object' && err.type) {
      if (err.type === 'entity.too.large') {
        reqErr = thorin.error(PARSE_ERROR_CODE, 'Payload too large', err.statusCode);
      } else {
        reqErr = thorin.error(err.error);
      }
      reqData = {
        error: reqErr.toJSON()
      };
      statusCode = reqErr && reqErr.statusCode || 400;
    } else {
      switch (err.status) {
        case 415: // encoding unsupported.
          reqErr = thorin.error(PARSE_ERROR_CODE, 'Encoding unsupported', err);
          break;
        case 400: // aborted
          reqErr = thorin.error(PARSE_ERROR_CODE, 'Request aborted', err);
          break;
        case 413: // payload large
          reqErr = thorin.error(PARSE_ERROR_CODE, 'Payload too large', err);
          break;
        default:
          reqErr = thorin.error(err);
          statusCode = 500;
      }
      reqData = {
        error: reqErr.toJSON()
      };
    }
    try {
      res.status(statusCode);
    } catch (e) {
    }
    let logErr;
    // TODO: include the HTML 404 not found pages.
    if (req._hasDebug !== false) {
      let logMsg = '[ENDED',
        logLevel = 'trace';
      if (req.uniqueId) {
        logMsg += ' ' + req.uniqueId;
      }
      logMsg += '] -';
      if (req.action) logMsg += ' ' + req.action;
      logMsg += " (" + req.method.toUpperCase() + ' ' + req.originalUrl.substr(0, 64) + ') ';
      logMsg += '= ' + statusCode + ' ';
      if (statusCode === 404) {
        if (reqErr.code !== 'TRANSPORT.NOT_FOUND') {
          logMsg += '[' + reqErr.code + '] ';
        }
        logMsg += reqErr.message;
      } else if (statusCode < 500) {
        logMsg += '[' + reqErr.code + '] ' + reqErr.message;
      } else {
        logMsg += '[' + reqErr.code + ']';
        logLevel = 'warn';
        logErr = reqErr;
      }
      if (req.startAt) {
        let took = Date.now() - req.startAt;
        logMsg += " (" + took + "ms)";
      }
      logger(logLevel, logMsg, logErr);
    }
    try {
      if (this[config].debug && logErr.source) {
        logger('warn', logErr.source.stack);
      }
    } catch (e) {
    }
    // Check if we have a buffer in our rawData
    if (err.rawData instanceof Buffer) {
      try {
        res.set({
          'Content-Type': 'application/octet-stream'
        });
      } catch (e) {
      }
      try {
        res.end(err.rawData);
      } catch (e) {
        console.error('Thorin.transport.http: failed to send error buffer to response.');
        console.debug(e);
        try {
          res.end();
        } catch (e) {
        }
      }
      return;
    }
    if (typeof err.rawData === 'string') {
      try {
        res.type('html');
      } catch (e) {
      }
      try {
        res.end(err.rawData);
      } catch (e) {
        console.error('Thorin.transport.http: failed to send error string to response.');
        console.debug(e);
        try {
          res.end();
        } catch (e) {
        }
      }
      return;
    }
    try {
      if (req.exposeType === false && reqData.type) {
        delete reqData.type;
      }
      try {
        reqData = this.parseIntentJson(reqData, null, req, intentObj);
      } catch (e) {
      }
      res.header('content-type', 'application/json; charset=utf-8');
      reqData = JSON.stringify(reqData);
      res.end(reqData);
    } catch (e) {
      console.error('Thorin.transport.http: failed to finalize request with error: ', reqErr);
      console.error(e);
      try {
        res.end(reqErr.message);
      } catch (e) {
        try {
          res.end();
        } catch (e) {
        }
      }
    }
  }
function handleRequestError(err, req, res, next) {
    
    let intentObj;
    if (req.intent) {
      intentObj = req.intent;
      delete req.intent;
    }
    setCors.call(this, req, res, req.method);
    let reqErr,
      reqData,
      statusCode = 400;
    if (err instanceof SyntaxError) {  
      reqErr = thorin.error(PARSE_ERROR_CODE, 'Invalid payload.', err);
      statusCode = 400;
      reqData = {
        error: reqErr.toJSON()
      };
    } else if (err instanceof Error && err.name.indexOf('Thorin') === 0) {
      reqErr = err;
      statusCode = err.statusCode || 400;
      reqData = {
        error: reqErr.toJSON()
      };
    } else if (typeof err === 'object' && err.type) {
      if (err.type === 'entity.too.large') {
        reqErr = thorin.error(PARSE_ERROR_CODE, 'Payload too large', err.statusCode);
      } else {
        reqErr = thorin.error(err.error);
      }
      reqData = {
        error: reqErr.toJSON()
      };
      statusCode = reqErr && reqErr.statusCode || 400;
    } else {
      switch (err.status) {
        case 415: 
          reqErr = thorin.error(PARSE_ERROR_CODE, 'Encoding unsupported', err);
          break;
        case 400: 
          reqErr = thorin.error(PARSE_ERROR_CODE, 'Request aborted', err);
          break;
        case 413: 
          reqErr = thorin.error(PARSE_ERROR_CODE, 'Payload too large', err);
          break;
        default:
          reqErr = thorin.error(err);
          statusCode = 500;
      }
      reqData = {
        error: reqErr.toJSON()
      };
    }
    try {
      res.status(statusCode);
    } catch (e) {
    }
    let logErr;
    
    if (req._hasDebug !== false) {
      let logMsg = '[ENDED',
        logLevel = 'trace';
      if (req.uniqueId) {
        logMsg += ' ' + req.uniqueId;
      }
      logMsg += '] -';
      if (req.action) logMsg += ' ' + req.action;
      logMsg += " (" + req.method.toUpperCase() + ' ' + req.originalUrl.substr(0, 64) + ') ';
      logMsg += '= ' + statusCode + ' ';
      if (statusCode === 404) {
        if (reqErr.code !== 'TRANSPORT.NOT_FOUND') {
          logMsg += '[' + reqErr.code + '] ';
        }
        logMsg += reqErr.message;
      } else if (statusCode < 500) {
        logMsg += '[' + reqErr.code + '] ' + reqErr.message;
      } else {
        logMsg += '[' + reqErr.code + ']';
        logLevel = 'warn';
        logErr = reqErr;
      }
      if (req.startAt) {
        let took = Date.now() - req.startAt;
        logMsg += " (" + took + "ms)";
      }
      logger(logLevel, logMsg, logErr);
    }
    try {
      if (this[config].debug && logErr.source) {
        logger('warn', logErr.source.stack);
      }
    } catch (e) {
    }
    
    if (err.rawData instanceof Buffer) {
      try {
        res.set({
          'Content-Type': 'application/octet-stream'
        });
      } catch (e) {
      }
      try {
        res.end(err.rawData);
      } catch (e) {
        console.error('Thorin.transport.http: failed to send error buffer to response.');
        console.debug(e);
        try {
          res.end();
        } catch (e) {
        }
      }
      return;
    }
    if (typeof err.rawData === 'string') {
      try {
        res.type('html');
      } catch (e) {
      }
      try {
        res.end(err.rawData);
      } catch (e) {
        console.error('Thorin.transport.http: failed to send error string to response.');
        console.debug(e);
        try {
          res.end();
        } catch (e) {
        }
      }
      return;
    }
    try {
      if (req.exposeType === false && reqData.type) {
        delete reqData.type;
      }
      try {
        reqData = this.parseIntentJson(reqData, null, req, intentObj);
      } catch (e) {
      }
      res.header('content-type', 'application/json; charset=utf-8');
      reqData = JSON.stringify(reqData);
      res.end(reqData);
    } catch (e) {
      console.error('Thorin.transport.http: failed to finalize request with error: ', reqErr);
      console.error(e);
      try {
        res.end(reqErr.message);
      } catch (e) {
        try {
          res.end();
        } catch (e) {
        }
      }
    }
  }
function handleRequestError ( err , req , res , next ) { // In order to make errors visible, we have to set CORS for em. let intentObj ; if ( req . intent ) { intentObj = req . intent ; delete req . intent ; } setCors . call ( this , req , res , req . method ) ; let reqErr , reqData , statusCode = 400 ; if ( err instanceof SyntaxError ) { // any other error, we bubble up. reqErr = thorin . error ( PARSE_ERROR_CODE , 'Invalid payload.' , err ) ; statusCode = 400 ; reqData = { error : reqErr . toJSON ( ) } ; } else if ( err instanceof Error && err . name . indexOf ( 'Thorin' ) === 0 ) { reqErr = err ; statusCode = err . statusCode || 400 ; reqData = { error : reqErr . toJSON ( ) } ; } else if ( typeof err === 'object' && err . type ) { if ( err . type === 'entity.too.large' ) { reqErr = thorin . error ( PARSE_ERROR_CODE , 'Payload too large' , err . statusCode ) ; } else { reqErr = thorin . error ( err . error ) ; } reqData = { error : reqErr . toJSON ( ) } ; statusCode = reqErr && reqErr . statusCode || 400 ; } else { switch ( err . status ) { case 415 : // encoding unsupported. reqErr = thorin . error ( PARSE_ERROR_CODE , 'Encoding unsupported' , err ) ; break ; case 400 : // aborted reqErr = thorin . error ( PARSE_ERROR_CODE , 'Request aborted' , err ) ; break ; case 413 : // payload large reqErr = thorin . error ( PARSE_ERROR_CODE , 'Payload too large' , err ) ; break ; default : reqErr = thorin . error ( err ) ; statusCode = 500 ; } reqData = { error : reqErr . toJSON ( ) } ; } try { res . status ( statusCode ) ; } catch ( e ) { } let logErr ; // TODO: include the HTML 404 not found pages. if ( req . _hasDebug !== false ) { let logMsg = '[ENDED' , logLevel = 'trace' ; if ( req . uniqueId ) { logMsg += ' ' + req . uniqueId ; } logMsg += '] -' ; if ( req . action ) logMsg += ' ' + req . action ; logMsg += " (" + req . method . toUpperCase ( ) + ' ' + req . originalUrl . substr ( 0 , 64 ) + ') ' ; logMsg += '= ' + statusCode + ' ' ; if ( statusCode === 404 ) { if ( reqErr . code !== 'TRANSPORT.NOT_FOUND' ) { logMsg += '[' + reqErr . code + '] ' ; } logMsg += reqErr . message ; } else if ( statusCode < 500 ) { logMsg += '[' + reqErr . code + '] ' + reqErr . message ; } else { logMsg += '[' + reqErr . code + ']' ; logLevel = 'warn' ; logErr = reqErr ; } if ( req . startAt ) { let took = Date . now ( ) - req . startAt ; logMsg += " (" + took + "ms)" ; } logger ( logLevel , logMsg , logErr ) ; } try { if ( this [ config ] . debug && logErr . source ) { logger ( 'warn' , logErr . source . stack ) ; } } catch ( e ) { } // Check if we have a buffer in our rawData if ( err . rawData instanceof Buffer ) { try { res . set ( { 'Content-Type' : 'application/octet-stream' } ) ; } catch ( e ) { } try { res . end ( err . rawData ) ; } catch ( e ) { console . error ( 'Thorin.transport.http: failed to send error buffer to response.' ) ; console . debug ( e ) ; try { res . end ( ) ; } catch ( e ) { } } return ; } if ( typeof err . rawData === 'string' ) { try { res . type ( 'html' ) ; } catch ( e ) { } try { res . end ( err . rawData ) ; } catch ( e ) { console . error ( 'Thorin.transport.http: failed to send error string to response.' ) ; console . debug ( e ) ; try { res . end ( ) ; } catch ( e ) { } } return ; } try { if ( req . exposeType === false && reqData . type ) { delete reqData . type ; } try { reqData = this . parseIntentJson ( reqData , null , req , intentObj ) ; } catch ( e ) { } res . header ( 'content-type' , 'application/json; charset=utf-8' ) ; reqData = JSON . stringify ( reqData ) ; res . end ( reqData ) ; } catch ( e ) { console . error ( 'Thorin.transport.http: failed to finalize request with error: ' , reqErr ) ; console . error ( e ) ; try { res . end ( reqErr . message ) ; } catch ( e ) { try { res . end ( ) ; } catch ( e ) { } } } }
/* Handle any kind of error.
/ * Handle any kind of error .