{"size": 1738, "ext": "js", "max_stars_repo_path": "lib/hapi_plugin_builder.js", "max_stars_repo_name": "dm03514/auth0-instrumentation", "max_stars_repo_stars_event_min_datetime": "2016-07-04T19:53:32.000Z", "max_stars_repo_stars_event_max_datetime": "2019-11-06T09:15:37.000Z", "max_issues_repo_path": "lib/hapi_plugin_builder.js", "max_issues_repo_name": "dm03514/auth0-instrumentation", "max_issues_repo_issues_event_min_datetime": "2016-04-06T12:51:15.000Z", "max_issues_repo_issues_event_max_datetime": "2020-03-04T15:07:48.000Z", "max_forks_repo_path": "lib/hapi_plugin_builder.js", "max_forks_repo_name": "dm03514/auth0-instrumentation", "max_forks_repo_forks_event_min_datetime": "2016-04-25T21:02:35.000Z", "max_forks_repo_forks_event_max_datetime": "2020-02-10T19:43:50.000Z"}
{"max_stars_count": 19, "max_issues_count": 92, "max_forks_count": 42, "avg_line_length": 32.7924528302, "max_line_length": 99, "alphanum_fraction": 0.5914844649}
function hapiPluginBuilder(client) {
  function hapiErrorReporterBuilder(options) {
    return function hapiErrorReporter(request, event) {
      // extract err, id depends on the hapi version.
      // for >= hapi17:
      //            - event has a property error
      //            - the id is on `request.info.id`
      // for < hapi17:
      //            - event is the directly the error
      //            - the id is on `request.id`
      const err = event.isBoom ? event : event.error;
      const id = request.id ? request.id : request.info.id;
      client.captureError(err, {
        extra: {
          timestamp: request.info.received,
          id: id,
          method: request.method,
          path: request.path,
          payload: request.pre && request.pre._originalPayload,
          query: request.query,
          remoteAddress: request.info.remoteAddress,
          userAgent: request.raw.req.headers['user-agent']
        },
        tags: options.tags
      });
    }
  }

  var plugin = {
    pkg: require('../package.json'),
    register: function (server, options, next) {
      const hapiVersion = server.version || ""; // '17.0.0'
      const majorHapiVersion = parseInt(hapiVersion.split(".")[0]);

      server.expose('client', client);

      // depending on the hapi version use a different way to listen to the event
      if (majorHapiVersion >= 17) {
        server.events.on({name: 'request', channels: 'error' }, hapiErrorReporterBuilder(options));
      } else {
        server.on('request-error', hapiErrorReporterBuilder(options));
        next();
      }
    }
  };

  plugin.register.attributes = { pkg: require('../package.json') };

  return plugin
}

module.exports = hapiPluginBuilder;
