Rory
Rory

Reputation: 838

How to handle multiple invalid query params in akka http?

I have query params defined in my route like so:

parameters(('modifiedDate.as[String].?, 'active.as[Boolean].?)) { (modifiedDate, active) =>

I have the following rejection handler:

RejectionHandler.newBuilder()
      .handle { case MalformedQueryParamRejection(param, _, _) =>
        param match {
          case "modifiedDate" => ...
          case "active" => ...
        }
      }

The problem is that MalformedQueryParamRejection only matches the first error, so if both query params are passed and both are incorrect:

some-endpoint?modifiedDate=invalid&active=invalid

the client will only be informed of the first error.

Is there any way of using akka http to be informed of all the query parameters that are invalid, something like MalformedQueryParamRejection but that reports all the invalid query params, like MalformedQueryParamsRejection (plural)?

Upvotes: 1

Views: 433

Answers (2)

Rory
Rory

Reputation: 838

I've raised a feature request for this:

https://github.com/akka/akka-http/issues/1490

Upvotes: 0

Jeffrey Chung
Jeffrey Chung

Reputation: 19497

Use handleAll. For example:

RejectionHandler.newBuilder()
  .handleAll[MalformedQueryParamRejection] { paramRejections =>
    // paramRejections is a Seq[MalformedQueryParamRejection]
    val malformedParamNames = paramRejections.map(_.parameterName)
    ...
  }
  ...

Upvotes: 2

Related Questions