{"size": 667, "ext": "js", "max_stars_repo_path": "lib/middlewares/exception_middleware.js", "max_stars_repo_name": "a-labo/akoa", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/middlewares/exception_middleware.js", "max_issues_repo_name": "a-labo/akoa", "max_issues_repo_issues_event_min_datetime": "2020-05-10T06:29:17.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T21:54:08.000Z", "max_forks_repo_path": "lib/middlewares/exception_middleware.js", "max_forks_repo_name": "a-labo/akoa", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 20.84375, "max_line_length": 59, "alphanum_fraction": 0.6206896552}
/**
 * Define an exception middleware
 * @function exceptionMiddleware
 * @param {Object} [options] - Optional settings
 * @returns {function}
 */
'use strict'

/** @lends exceptionMiddleware */
function exceptionMiddleware (options = {}) {
  const {
    logger = console.error,
    status = 500,
    prefix = null
  } = options
  return async function exceptionMiddleware (ctx, next) {
    const skip = prefix && (ctx.path.indexOf(prefix) !== 0)
    if (skip) {
      await next()
      return
    }
    try {
      await next()
    } catch (err) {
      logger('Uncaught exception:', err)
      ctx.status = status
    }
  }
}

module.exports = exceptionMiddleware
