<commit_before>import { NextFunction, Request, Response, Router } from "express";
import { AuthValidationMiddleware } from "./../../../logic/middleware/index";
import { ApiError, DbError, Recipe, ValidationError } from "./../../../models/index";

export class AuthValidationHandler {

  constructor (private _authValidationMiddleware: AuthValidationMiddleware) {}

  public checkRequest = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.checkRequest(req)
      .then(() => next())
      .catch((e: any) => res.status(e.statusCode).json(e.forResponse()));
  }

  public extractBody = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.extractRequestBody(req)
      .then(() => next())
      .catch((e: any) => res.status(e.statusCode).json(e.forResponse()));
  }

  public checkToken = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.checkValidToken(req)
      .then(() => next())
      .catch((e: any) => res.status(e.statusCode).json(e.forResponse()));
  }

  public checkUser = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.checkValidUser(req)
      .then(() => next())
      .catch((e: any) => res.status(e.statusCode).json(e.forResponse()));
  }

  public restricted = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.checkUserRoleForRestriction(req, true)
      .then(() => next())
      .catch((e: any) => res.status(e.statusCode).json(e.forResponse()));
  }

  public nonRestricted = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.checkUserRoleForRestriction(req, false)
      .then(() => next())
      .catch((e: any) => res.status(e.statusCode).json(e.forResponse()));
  }
}<commit_msg>#34 - add _sendErrorResponse() function<commit_after>import { NextFunction, Request, Response, Router } from "express";
import { AuthValidationMiddleware } from "./../../../logic/middleware/index";
import { ApiError, DbError, Recipe, ValidationError } from "./../../../models/index";

export class AuthValidationHandler {

  constructor (private _authValidationMiddleware: AuthValidationMiddleware) {}

  public checkRequest = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.checkRequest(req)
      .then(() => next())
      .catch((e: any) => this._sendErrorResponse(e, res));
  }

  public extractBody = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.extractRequestBody(req)
      .then(() => next())
      .catch((e: any) => this._sendErrorResponse(e, res));
  }

  public checkToken = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.checkValidToken(req)
      .then(() => next())
      .catch((e: any) => this._sendErrorResponse(e, res));
  }

  public checkUser = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.checkValidUser(req)
      .then(() => next())
      .catch((e: any) => this._sendErrorResponse(e, res));
  }

  public restricted = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.checkUserRoleForRestriction(req, true)
      .then(() => next())
      .catch((e: any) => this._sendErrorResponse(e, res));
  }

  public nonRestricted = (req: Request, res: Response, next: NextFunction): void => {
    this._authValidationMiddleware.checkUserRoleForRestriction(req, false)
      .then(() => next())
      .catch((e: any) => this._sendErrorResponse(e, res));
  }

  private _sendErrorResponse = (e: any, res: Response) => {
    res.status(e.statusCode).json(e.forResponse());
  }
}
{'fraction_non_alphanumeric': 0.16898087503274822, 'fraction_numerical': 0.0005239717055279015, 'mean_word_length': 4.117962466487936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6959820', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1118, 'n_words': 250}