Ákos Vandra-Meyer
Ákos Vandra-Meyer

Reputation: 2175

Spray routes - early reject?

My routing is similar to this:

pathPrefix("api") {
  path("login") {
    entity(as[LoginRequest]) { login =>
      complete { ... }
    }
  } ~
  pathPrefix("persons") {
    pathEnd {
      get { ctx => ctx.complete(model.getPersons) }
    } ~
    path("new") {
      post {
        entity(as[Person]) { person =>
          complete { model.addPerson(person).map { _ => StatusCodes.Accepted }
        }
      }
    } ~
    path(IntNumber) { id =>
      delete {
        complete {
          model.deletePerson(id).map { _ => StatusCodes.Accepted }
        }
      }
    }
  } ~
  path("app" / Rest) { path =>
    get {
      getFromResource("app/%s" format path)
    }
  }
}

My problem is that the error codes are off.

When querying a POST with an invalid document to /api/persons/new I would expect to get an InvalidEntity response code, but I get a 405: Method not allowed, supported: GET.

Same with /api/login.

If the entities are correct, the correct route runs.

If I issue a DELETE to /api/persons/invalidnumber, I get 405, instead of 404

If I run a GET for these routes, I get a 404. Maybe the last route is trying to run -- getFromResource?

Is there a way to force an "early return" from the routes? Such as entity(as[LoginRequest]) { ... } ~ failWithPreviousRejection

Upvotes: 2

Views: 71

Answers (0)

Related Questions