Dávid
10/15/2020, 8:46 AMfun Route.authorizedBy(roles: List<String>, callback: Route.() -> Route): Route {
val routeWithRoles = this.createChild(object : RouteSelector(RouteSelectorEvaluation.qualityConstant) {
override fun toString(): String {
return "(roles: $roles)"
}
override fun evaluate(context: RoutingResolveContext, segmentIndex: Int): RouteSelectorEvaluation =
RouteSelectorEvaluation.Constant
})
routeWithRoles.intercept(ApplicationCallPipeline.Call) {
if (!hasAccess(roles, call.user)) {
call.respond(HttpStatusCode.Unauthorized, "Not sufficient authority") //todo I am not sure if I should respond or throw errror
finish() //todo i am not sure if I should call finish here
}
}
callback(routeWithRoles)
return routeWithRoles
}
1. In my code I am uncertain if I should throw an exception from the interceptor or is it ok to call the respond method?
2. Also is it necessary to call the finish() there?
3. Should I call proceed() in the else branch or is that not necessary?
In general I think it is unclear when to use the proceed and finish methods and what is an intended way to break out of an interceptor with errors.Rustam Siniukov
10/15/2020, 9:10 AMDávid
10/15/2020, 9:25 AMfinish and proceed methods in the pipelines so I also learn from this?Rustam Siniukov
10/15/2020, 9:38 AMproceed is needed when you want to do something at the end of pipeline execution. For example Logging feature calls it to log success or failure of the request.
finish is needed when you already have response for the request and don’t need to execute rest of the pipeline. For example you got response from cache and don’t need to make an actual requestDávid
10/15/2020, 9:39 AMDávid
10/15/2020, 9:39 AM