<commit_before>package io.flow.play.util

import io.flow.common.v0.models.json._
import play.api.http.HttpErrorHandler
import play.api.libs.json.Json
import play.api.Logger
import play.api.mvc.RequestHeader
import play.api.mvc.Results.{InternalServerError, Status}
import scala.concurrent.Future

/**
  * Custom error handler that always returns application/json
  * 
  * Server errors are logged w/ a unique error number that is presented
  * in the message back to the client. This allows us to quickly cross
  * reference an error to a specific point in the log.
  */
class ErrorHandler extends HttpErrorHandler {

  private[this] val idGenerator = IdGenerator("err")

  def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    statusCode match {
      case 401 | 404 => Future.successful(Status(statusCode))
      case other => {
        message.trim match {
          case "" => Future.successful(Status(statusCode))
          case msg => Future.successful(Status(statusCode)(Json.toJson(Validation.clientError(msg))))
        }
      }
    }
  }

  override def onServerError(request: RequestHeader, exception: Throwable) = {
    val errorId = idGenerator.randomId()
    Logger.error(s"Error[$errorId] ${request.method} ${request.path}", exception)

    val msg = FlowEnvironment.Current match {
      case FlowEnvironment.Development | FlowEnvironment.Workstation => s"A server error has occurred (#$errorId). Additional info for development environment: $exception"
      case FlowEnvironment.Production => s"A server error has occurred (#$errorId)"
    }

    Future.successful(InternalServerError(Json.toJson(Validation.serverError(msg))))
  }
}<commit_msg>Use FlowError in error log msg to make sumo search easier (#90)<commit_after>package io.flow.play.util

import io.flow.common.v0.models.json._
import play.api.http.HttpErrorHandler
import play.api.libs.json.Json
import play.api.Logger
import play.api.mvc.RequestHeader
import play.api.mvc.Results.{InternalServerError, Status}
import scala.concurrent.Future

/**
  * Custom error handler that always returns application/json
  * 
  * Server errors are logged w/ a unique error number that is presented
  * in the message back to the client. This allows us to quickly cross
  * reference an error to a specific point in the log.
  */
class ErrorHandler extends HttpErrorHandler {

  private[this] val idGenerator = IdGenerator("err")

  def onClientError(request: RequestHeader, statusCode: Int, message: String) = {
    statusCode match {
      case 401 | 404 => Future.successful(Status(statusCode))
      case other => {
        message.trim match {
          case "" => Future.successful(Status(statusCode))
          case msg => Future.successful(Status(statusCode)(Json.toJson(Validation.clientError(msg))))
        }
      }
    }
  }

  override def onServerError(request: RequestHeader, exception: Throwable) = {
    val errorId = idGenerator.randomId()
    Logger.error(s"FlowError [$errorId] ${request.method} ${request.path}", exception)

    val msg = FlowEnvironment.Current match {
      case FlowEnvironment.Development | FlowEnvironment.Workstation => s"A server error has occurred (#$errorId). Additional info for development environment: $exception"
      case FlowEnvironment.Production => s"A server error has occurred (#$errorId)"
    }

    Future.successful(InternalServerError(Json.toJson(Validation.serverError(msg))))
  }
}
{'fraction_non_alphanumeric': 0.10194597734533836, 'fraction_numerical': 0.004647110078419983, 'mean_word_length': 3.984081041968162, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1200622', 'n_tokens_mistral': 964, 'n_tokens_neox': 928, 'n_words': 272}