middleware/mw.error.handling.js
function handlePreResponseError(server) {
        server.ext('onPreResponse', function (request, reply) {
            var response = request.response;
            var originalResponse = response;
            var msg;

            // No error, keep going with the reply as normal
            if (!response.isBoom) { reply.continue(); return; }

            // we need to convert to AppError if it is not already
            if (!(response instanceof AppError)) {
                msg = response.message || (response + '');

                // if legit 404 be sure to use that code (happens with not found in /dist on local)
                if (msg.indexOf('404:') >= 0 ||
                    (response.output && response.output.payload &&
                    response.output.payload.statusCode === 404)) {

                    response = new AppError({
                        code: 'not_found',
                        msg: 'The resource ' + request.url.path + ' was not found'
                    });
                }
                else {
                    msg = response.message || (response + '');
                    if (response.data) { msg += ' : ' + response.data; }

                    log.info(msg);

                    response = new AppError({
                        code: response.code || 'api_error',
                        msg: msg,
                        err: response
                    });
                }
            }

            var code = response.code || 'unknown_error';
            var err = errorDecoder[code] || errorDecoder['unknown_error'];
            var url = request.path;

            if (err.httpErrorCode === 404) {
                trackingService.pageNotFound({
                    caller: request.caller,
                    url: routeHelper.getBaseUrl(request.app.name) + url
                });
            }
            else {
                log.error(request.method + ' ' + url + ' (' + err.friendlyMessage + ')',
                    { err: originalResponse }   );
            }

            //TODO: this hack sucks, but needed quick way to display error page; do better!
            if (pancakes.getContainer() === 'webserver') {

                // hack fix for issue when error on trust site
                if (request.app && request.app.name === 'trust') {
                    reply().redirect(routeHelper.getBaseUrl('contact') + '?notify=loginFailure');
                }
                // else show error pages
                else {
                    var errUrl = err.httpErrorCode === 404 ? '/error404' : '/error500';
                    pancakes.processRoute({ request: request, reply: reply, urlOverride: errUrl, returnCode: err.httpErrorCode });
                }
            }
            else {
                reply(Boom.create(err.httpErrorCode, err.friendlyMessage));
            }
        });
    }
function handlePreResponseError(server) {
        server.ext('onPreResponse', function (request, reply) {
            var response = request.response;
            var originalResponse = response;
            var msg;

            // No error, keep going with the reply as normal
            if (!response.isBoom) { reply.continue(); return; }

            // we need to convert to AppError if it is not already
            if (!(response instanceof AppError)) {
                msg = response.message || (response + '');

                // if legit 404 be sure to use that code (happens with not found in /dist on local)
                if (msg.indexOf('404:') >= 0 ||
                    (response.output && response.output.payload &&
                    response.output.payload.statusCode === 404)) {

                    response = new AppError({
                        code: 'not_found',
                        msg: 'The resource ' + request.url.path + ' was not found'
                    });
                }
                else {
                    msg = response.message || (response + '');
                    if (response.data) { msg += ' : ' + response.data; }

                    log.info(msg);

                    response = new AppError({
                        code: response.code || 'api_error',
                        msg: msg,
                        err: response
                    });
                }
            }

            var code = response.code || 'unknown_error';
            var err = errorDecoder[code] || errorDecoder['unknown_error'];
            var url = request.path;

            if (err.httpErrorCode === 404) {
                trackingService.pageNotFound({
                    caller: request.caller,
                    url: routeHelper.getBaseUrl(request.app.name) + url
                });
            }
            else {
                log.error(request.method + ' ' + url + ' (' + err.friendlyMessage + ')',
                    { err: originalResponse }   );
            }

            //TODO: this hack sucks, but needed quick way to display error page; do better!
            if (pancakes.getContainer() === 'webserver') {

                // hack fix for issue when error on trust site
                if (request.app && request.app.name === 'trust') {
                    reply().redirect(routeHelper.getBaseUrl('contact') + '?notify=loginFailure');
                }
                // else show error pages
                else {
                    var errUrl = err.httpErrorCode === 404 ? '/error404' : '/error500';
                    pancakes.processRoute({ request: request, reply: reply, urlOverride: errUrl, returnCode: err.httpErrorCode });
                }
            }
            else {
                reply(Boom.create(err.httpErrorCode, err.friendlyMessage));
            }
        });
    }
function handlePreResponseError ( server ) { server . ext ( 'onPreResponse' , function ( request , reply ) { var response = request . response ; var originalResponse = response ; var msg ; // No error, keep going with the reply as normal if ( ! response . isBoom ) { reply . continue ( ) ; return ; } // we need to convert to AppError if it is not already if ( ! ( response instanceof AppError ) ) { msg = response . message || ( response + '' ) ; // if legit 404 be sure to use that code (happens with not found in /dist on local) if ( msg . indexOf ( '404:' ) >= 0 || ( response . output && response . output . payload && response . output . payload . statusCode === 404 ) ) { response = new AppError ( { code : 'not_found' , msg : 'The resource ' + request . url . path + ' was not found' } ) ; } else { msg = response . message || ( response + '' ) ; if ( response . data ) { msg += ' : ' + response . data ; } log . info ( msg ) ; response = new AppError ( { code : response . code || 'api_error' , msg : msg , err : response } ) ; } } var code = response . code || 'unknown_error' ; var err = errorDecoder [ code ] || errorDecoder [ 'unknown_error' ] ; var url = request . path ; if ( err . httpErrorCode === 404 ) { trackingService . pageNotFound ( { caller : request . caller , url : routeHelper . getBaseUrl ( request . app . name ) + url } ) ; } else { log . error ( request . method + ' ' + url + ' (' + err . friendlyMessage + ')' , { err : originalResponse } ) ; } //TODO: this hack sucks, but needed quick way to display error page; do better! if ( pancakes . getContainer ( ) === 'webserver' ) { // hack fix for issue when error on trust site if ( request . app && request . app . name === 'trust' ) { reply ( ) . redirect ( routeHelper . getBaseUrl ( 'contact' ) + '?notify=loginFailure' ) ; } // else show error pages else { var errUrl = err . httpErrorCode === 404 ? '/error404' : '/error500' ; pancakes . processRoute ( { request : request , reply : reply , urlOverride : errUrl , returnCode : err . httpErrorCode } ) ; } } else { reply ( Boom . create ( err . httpErrorCode , err . friendlyMessage ) ) ; } } ) ; }
Set up the pre-response error handler
@param server
Set up the pre - response error handler