Search code examples
scalahttp-errorlagom

how to Throw a customize http error in Scala Lagom


I know how to throw a bad request in Lagom by using

throw BadRequest("Bad Request")

but this will return with an http error code of 400. how do I return with a http error code of 409 (Conflict) for example?


Solution

  • You need to create your custom exception that must be inherited from TransportException:

    val ConflictErrorCode: TransportErrorCode = TransportErrorCode(409, -1003, "Conflict")
    
    final class Conflict(message: String)
        extends TransportException(ConflictErrorCode, new ExceptionMessage(ConflictErrorCode.description, message))
    

    Then in your code, you need to write:

    throw new Conflict("Some conflict message")