<commit_before>package pl.touk.nussknacker.ui.api

import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.model.{HttpResponse, StatusCode, StatusCodes}
import akka.http.scaladsl.server.ExceptionHandler
import argonaut.EncodeJson
import com.typesafe.scalalogging.LazyLogging
import pl.touk.nussknacker.ui.process.deployment.ProcessIsBeingDeployed
import pl.touk.nussknacker.restmodel.validation.ValidationResults.ValidationResult
import pl.touk.nussknacker.ui.{BadRequestError, EspError, FatalError, NotFoundError}
import pl.touk.http.argonaut.Argonaut62Support
import pl.touk.nussknacker.ui.validation.FatalValidationError

import scala.language.implicitConversions

object EspErrorToHttp extends LazyLogging with Argonaut62Support {

  import pl.touk.nussknacker.ui.codec.UiCodecs.validationResultEncode

  def espErrorToHttp(error: EspError) : HttpResponse = {
    val statusCode = error match {
      case e: NotFoundError => StatusCodes.NotFound
      case e: FatalError => StatusCodes.InternalServerError
      case e: BadRequestError => StatusCodes.BadRequest
      case e: ProcessIsBeingDeployed => StatusCodes.Conflict
      case e: FatalValidationError => StatusCodes.BadRequest
      //unknown?
      case _ =>
        logger.error(s"Unknown EspError: ${error.getMessage}", error)
        StatusCodes.InternalServerError
    }
    HttpResponse(status = statusCode, entity = error.getMessage)
  }

  def errorToHttp : PartialFunction[Throwable, HttpResponse] = {
    case e:EspError => espErrorToHttp(e)
    case ex =>
      logger.error(s"Unknown error: ${ex.getMessage}", ex)
      HttpResponse(status = StatusCodes.InternalServerError, entity = ex.getMessage)
  }

  def espErrorHandler: ExceptionHandler = {
    import akka.http.scaladsl.server.Directives._
    ExceptionHandler {
      case e: EspError => complete(errorToHttp(e))
    }<commit_msg>Log unhandled API errors<commit_after>package pl.touk.nussknacker.ui.api

import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.model.{HttpResponse, StatusCode, StatusCodes}
import akka.http.scaladsl.server.ExceptionHandler
import argonaut.EncodeJson
import com.typesafe.scalalogging.LazyLogging
import pl.touk.nussknacker.ui.process.deployment.ProcessIsBeingDeployed
import pl.touk.nussknacker.restmodel.validation.ValidationResults.ValidationResult
import pl.touk.nussknacker.ui.{BadRequestError, EspError, FatalError, NotFoundError}
import pl.touk.http.argonaut.Argonaut62Support
import pl.touk.nussknacker.ui.validation.FatalValidationError

import scala.language.implicitConversions
import scala.util.control.NonFatal

object EspErrorToHttp extends LazyLogging with Argonaut62Support {

  import pl.touk.nussknacker.ui.codec.UiCodecs.validationResultEncode

  def espErrorToHttp(error: EspError) : HttpResponse = {
    val statusCode = error match {
      case e: NotFoundError => StatusCodes.NotFound
      case e: FatalError => StatusCodes.InternalServerError
      case e: BadRequestError => StatusCodes.BadRequest
      case e: ProcessIsBeingDeployed => StatusCodes.Conflict
      case e: FatalValidationError => StatusCodes.BadRequest
      //unknown?
      case _ =>
        logger.error(s"Unknown EspError: ${error.getMessage}", error)
        StatusCodes.InternalServerError
    }
    HttpResponse(status = statusCode, entity = error.getMessage)
  }

  def errorToHttp : PartialFunction[Throwable, HttpResponse] = {
    case e:EspError => espErrorToHttp(e)
    case ex =>
      logger.error(s"Unknown error: ${ex.getMessage}", ex)
      HttpResponse(status = StatusCodes.InternalServerError, entity = ex.getMessage)
  }

  def espErrorHandler: ExceptionHandler = {
    import akka.http.scaladsl.server.Directives._
    ExceptionHandler {
      case NonFatal(e) => complete(errorToHttp(e))
    }
{'fraction_non_alphanumeric': 0.08868022952529994, 'fraction_numerical': 0.0020865936358894104, 'mean_word_length': 4.706845238095238, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '6441834', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1162, 'n_words': 210}