I have following spray route
post {
parameters(('adId.as[String], 'propertyType.as[TypNe])).as(Import){imp =>
complete {
adImporterService ! imp
StatusCodes.Accepted
}
}
}
Custom deserializer for conversion to Java enum of TypNe:
implicit val propertyTypeDeserializer = new Deserializer[String,TypNe]{
def apply(s:String):Deserialized[TypNe]={
Try(TypNe.valueOf(s.toUpperCase)) match {
case Success(tn) => Right(tn)
case Failure(ex) => Left(MalformedContent(s"Wrong property type. Accepted values: ${TypNe.values}", Some(ex)))
}
}
}
and Import case class is defined as follows:
case class Import(adId: String, propertyType:TypNe){
require(adId!= null && !adId.isEmpty && propertyType!=null,"adId parameter cannot be null or empty and propertyType has to be defined")
}
The desired and expected behaviour is that IllegarArgumentException thrown is bind to response code 400 - BadRequest. But I am getting response code 500 - Internal Server Error.
I am using a Default RejectionHandler
class MainService(route: Route) extends HttpServiceActor {
implicit val system = context.system
import spray.routing.RejectionHandler.Default
override def receive: Receive = runRoute(route)
}
with default ExceptionHandler - which converts NonFatal exception to InternalServer error.
What is the best practice to achieve a desired behaviour?
Creating a custom ExceptionHandler seems to me as a not correct way to go. What I would probably need to achive a conversion of IllegalArgumentException to MalformedQueryParamRejection within a parameters directive.
Can anyone give a hint how to achieve it?
Thx
UPDATE: Tried different approach but didn't work as expected either.
post {
parameters(('adId.as[String], 'propertyType.as[String])) { (id,pT) =>
validate(Try(TypNe.valueOf(pT)).isSuccess,s"propertyType $pT is not a valid ${TypNe.values()}")
validate(id!= null && !id.trim.isEmpty,"adId parameter cannot be null or empty ")
complete {
adImporterService ! Import(id, TypNe.valueOf(pT))
StatusCodes.Accepted
}
}
}