Search code examples
scalaakkaakka-http

Akka: Cancel Routing Due To Incorrect Query Parameters


So I have a route structure something like this

pathPrexix("root"){
  concat {
    path("path") {
      get {
        parameters("someId".as[String], 'fixedValue ! "requiredValue") { params =>

        }
      }
    },
    path(Segment) { extractedValue =>
      .....
    }
  }
} 

If the user ends a request to the /root/path endpoint with the incorrect query parameters (either someId missing or fixedValue not equal to value) then the request will be routed further on to the next route, root/Segment. extractedValue would in this case be path which would fail and send the user back error handled by the second route.

The preferred behaviour would be to tell the user that they either missed a query parameters or that the query parameter must be one of the given values. Is there any way to make sure that happen?

If I move the second path above the first, it will capture all requests sent.


Solution

  • You just need to complete with an appropriate error code if the get does not match:

    path("path") {
      concat(
        get {
          parameters("someId".as[String], 'fixedValue ! "requiredValue") { params =>
          }
        },
        complete(StatusCodes.NotFound)
      )
    },
    

    You could put additional information in the reply message, but it would be non-standard and therefore would require the client to be aware of it.