0

I have created custom errors in my app:

sealed trait DataError extends Product with Serializable
final case class DataNotFound(ex: String) extends DataError
final case class DataStorageError(ex: String) extends DataError

And I have created simple decoder for json responses:

DataNotFound(code, message) => HttpResponse(StatusCodes.NotFound, entity = HttpEntity(ContentTypes.`application/json`, s"""{"code":"$code", "message":"No data found: $message"}"""))
DataStorageError(message) => HttpResponse(StatusCodes.InternalServerError, entity = HttpEntity(ContentTypes.`application/json`, s"$message"))

But as you see, this is a poor solution in my opinion. I would like to use json4s formats to parse error messages into jsons automatically, instead of writing manual strings.

I tried to do something like:

implicit val format = jsonFormat2(DataNotFound)

But it did not work in this case.

What is the best way to do it to get same results? Is it possible or maybe my solution is fine?

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
Developus
  • 1,400
  • 2
  • 14
  • 50

0 Answers0