I am writing a micro-service in Scala, and I am processing a response from a server, handling errors by throwing different exceptions.
The error processing is basically the same for each error, log a warning message and throw an exception. The only thing that changes is the kind of custom exception I am generating, so I did not want to repeat the same code all over my "case" statements, therefore I decided to use a method, accepting the exception as parameter.
config.httpClient.apply(request).map(response => {
response.getStatusCode() match {
case Status.Ok.code =>
<Do stuff to process the response>
case Status.BadRequest.code =>
errorReporting(response.status.code, response.contentString, BadRequestException)
case Status.Unauthorized.code =>
errorReporting(response.status.code, response.contentString, UnauthorizedException)
case _ =>
errorReporting(response.status.code, response.contentString, GeneralException)
}
})
The exceptions are defined as case classes:
case class GeneralException(private val message: String,
private val cause: Throwable = None.orNull) extends Exception (message, cause)
case class BadRequestException(private val message: String,
private val cause: Throwable = None.orNull) extends Exception(message, cause)
case class UnauthorizedException(private val message: String,
private val cause: Throwable = None.orNull) extends Exception(message, cause)
And the errorReporting method here:
def errorReporting(errorCode: Int, errorMessage: String, ex: (String, Throwable) => Throwable) = {
val message = s"Auth server response code: ${errorCode}, " + errorMessage
warn(message)
throw ex(message, None.orNull)
}
I defined my exceptions with a default value for "cause". The problem is that I do not find a way to tell "errorReporting" that Throwable is an optional parameter, therefore I am forced to throw the exception with throw ex(message, None.orNull)
which I do not find very elegant.
This entry Invocation of methods with default parameters in scala higher-order function tells me it is not possible to pass default parameters to high order functions.
Is there an alternative to solve this in a better way?