All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alexitc.playsonify.play.PublicErrorRenderer.scala Maven / Gradle / Ivy

Go to download

An opinionated library to help you build JSON APIs in a practical way using Play Framework

The newest version!
package com.alexitc.playsonify.play

import _root_.play.api.libs.json.{JsValue, Json}
import com.alexitc.playsonify.models._

class PublicErrorRenderer {

  import PublicErrorRenderer._

  /**
   * Converts a [[PublicError]] to a [[JsValue]].
   */
  def renderPublicError(publicError: PublicError): JsValue = publicError match {
    case e: GenericPublicError =>
      val obj = Json.obj(
        "type" -> GenericErrorType,
        "message" -> e.message
      )
      Json.toJson(obj)

    case e: FieldValidationError =>
      val obj = Json.obj(
        "type" -> FieldValidationErrorType,
        "field" -> e.field,
        "message" -> e.message
      )
      Json.toJson(obj)

    case e: HeaderValidationError =>
      val obj = Json.obj(
        "type" -> HeaderValidationErrorType,
        "header" -> e.header,
        "message" -> e.message
      )
      Json.toJson(obj)

    case e: InternalError =>
      val obj = Json.obj(
        "type" -> ServerErrorType,
        "errorId" -> e.id.string,
        "message" -> e.message
      )
      Json.toJson(obj)
  }
}

object PublicErrorRenderer {
  val GenericErrorType = "generic-error"
  val FieldValidationErrorType = "field-validation-error"
  val HeaderValidationErrorType = "header-validation-error"
  val ServerErrorType = "server-error"
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy