nl_tokens: Initialize an error handler for the HTTP router
pl_tokens: function initErrorHandler ( { ENV = { } , DEBUG_NODE_ENVS = DEFAULT_DEBUG_NODE_ENVS , STRINGIFYERS = DEFAULT_STRINGIFYERS , } ) { return Promise . resolve ( errorHandler ) ; /**
     * Handle an HTTP transaction error and
     * map it to a serializable response
     * @param  {String}  transactionId
     * A raw NodeJS HTTP incoming message
     * @param  {Object} responseSpec
     * The response specification
     * @param  {HTTPError} err
     * The encountered error
     * @return {Promise}
     * A promise resolving when the operation
     *  completes
     */ function errorHandler ( transactionId , responseSpec , err ) { return Promise . resolve ( ) . then ( ( ) => { const response = { } ; response . status = err . httpCode || 500 ; response . headers = Object . assign ( { } , err . headers || { } , { // Avoid caching errors 'cache-control' : 'private' , // Fallback to the default stringifyer to always be // able to display errors 'content-type' : responseSpec && responseSpec . contentTypes [ 0 ] && STRINGIFYERS [ responseSpec . contentTypes [ 0 ] ] ? responseSpec . contentTypes [ 0 ] : Object . keys ( STRINGIFYERS ) [ 0 ] , } ) ; response . body = { error : { code : err . code || 'E_UNEXPECTED' , // Enjoy nerdy stuff: // https://en.wikipedia.org/wiki/Guru_Meditation guruMeditation : transactionId , } , } ; if ( ENV && DEBUG_NODE_ENVS . includes ( ENV . NODE_ENV ) ) { response . body . error . stack = err . <mask> ; response . body . error . params = err . params ; } return response ; } ) ; } }