lib/interfaces/hapi.js
function makeHapiPlugin(client, config) {

  /**
   * The register function serves to attach the hapiErrorHandler to specific
   * points in the hapi request-response lifecycle. Namely: it attaches to the
   * 'request-error' event in Hapi which is emitted when a plugin or receiver
   * throws an error while executing and the 'onPreResponse' event to intercept
   * error code carrying requests before they are sent back to the client so
   * that the errors can be logged to the Error Reporting API.
   * @function hapiRegisterFunction
   * @param {Hapi.Server} server - A Hapi server instance
   * @param {Object} options - The server configuration options object
   * @param {Function} next - The Hapi callback to move execution to the next
   *  plugin
   * @returns {Undefined} - returns the execution of the next callback
   */
  function hapiRegisterFunction(server, options, next) {
    if (isObject(server)) {
      if (isFunction(server.on)) {
        server.on('request-error', function(req, err) {
          var em = hapiErrorHandler(req, err, config);

          client.sendError(em);
        });
      }

      if (isFunction(server.ext)) {
        server.ext('onPreResponse', function(request, reply) {
          var em = null;

          if (isObject(request) && isObject(request.response) &&
              request.response.isBoom) {
            em = hapiErrorHandler(request, new Error(request.response.message),
                                  config);

            client.sendError(em);
          }

          if (isObject(reply) && isFunction(reply.continue)) {
            reply.continue();
          }
        });
      }
    }

    if (isFunction(next)) {
      return next();
    }
  }

  var hapiPlugin = {register : hapiRegisterFunction};
  var version = (isPlainObject(config) && config.getVersion()) ?
    config.getVersion() : '0.0.0';

  hapiPlugin.register.attributes = {
    name : '@google/cloud-errors',
    version : version
  };

  return hapiPlugin;
}
function makeHapiPlugin(client, config) {

  /**
   * The register function serves to attach the hapiErrorHandler to specific
   * points in the hapi request-response lifecycle. Namely: it attaches to the
   * 'request-error' event in Hapi which is emitted when a plugin or receiver
   * throws an error while executing and the 'onPreResponse' event to intercept
   * error code carrying requests before they are sent back to the client so
   * that the errors can be logged to the Error Reporting API.
   * @function hapiRegisterFunction
   * @param {Hapi.Server} server - A Hapi server instance
   * @param {Object} options - The server configuration options object
   * @param {Function} next - The Hapi callback to move execution to the next
   *  plugin
   * @returns {Undefined} - returns the execution of the next callback
   */
  function hapiRegisterFunction(server, options, next) {
    if (isObject(server)) {
      if (isFunction(server.on)) {
        server.on('request-error', function(req, err) {
          var em = hapiErrorHandler(req, err, config);

          client.sendError(em);
        });
      }

      if (isFunction(server.ext)) {
        server.ext('onPreResponse', function(request, reply) {
          var em = null;

          if (isObject(request) && isObject(request.response) &&
              request.response.isBoom) {
            em = hapiErrorHandler(request, new Error(request.response.message),
                                  config);

            client.sendError(em);
          }

          if (isObject(reply) && isFunction(reply.continue)) {
            reply.continue();
          }
        });
      }
    }

    if (isFunction(next)) {
      return next();
    }
  }

  var hapiPlugin = {register : hapiRegisterFunction};
  var version = (isPlainObject(config) && config.getVersion()) ?
    config.getVersion() : '0.0.0';

  hapiPlugin.register.attributes = {
    name : '@google/cloud-errors',
    version : version
  };

  return hapiPlugin;
}
function makeHapiPlugin ( client , config ) { /**
   * The register function serves to attach the hapiErrorHandler to specific
   * points in the hapi request-response lifecycle. Namely: it attaches to the
   * 'request-error' event in Hapi which is emitted when a plugin or receiver
   * throws an error while executing and the 'onPreResponse' event to intercept
   * error code carrying requests before they are sent back to the client so
   * that the errors can be logged to the Error Reporting API.
   * @function hapiRegisterFunction
   * @param {Hapi.Server} server - A Hapi server instance
   * @param {Object} options - The server configuration options object
   * @param {Function} next - The Hapi callback to move execution to the next
   *  plugin
   * @returns {Undefined} - returns the execution of the next callback
   */ function hapiRegisterFunction ( server , options , next ) { if ( isObject ( server ) ) { if ( isFunction ( server . on ) ) { server . on ( 'request-error' , function ( req , err ) { var em = hapiErrorHandler ( req , err , config ) ; client . sendError ( em ) ; } ) ; } if ( isFunction ( server . ext ) ) { server . ext ( 'onPreResponse' , function ( request , reply ) { var em = null ; if ( isObject ( request ) && isObject ( request . response ) && request . response . isBoom ) { em = hapiErrorHandler ( request , new Error ( request . response . message ) , config ) ; client . sendError ( em ) ; } if ( isObject ( reply ) && isFunction ( reply . continue ) ) { reply . continue ( ) ; } } ) ; } } if ( isFunction ( next ) ) { return next ( ) ; } } var hapiPlugin = { register : hapiRegisterFunction } ; var version = ( isPlainObject ( config ) && config . getVersion ( ) ) ? config . getVersion ( ) : '0.0.0' ; hapiPlugin . register . attributes = { name : '@google/cloud-errors' , version : version } ; return hapiPlugin ; }
Creates a Hapi plugin object which can be used to handle errors in Hapi.
@param {AuthClient} client - an inited auth client instance
@param {NormalizedConfigurationVariables} config - the environmental
configuration
@returns {Object} - the actual Hapi plugin
Creates a Hapi plugin object which can be used to handle errors in Hapi .