@@ -4,7 +4,16 @@ import java.util.concurrent.TimeoutException
44import akka .actor .typed .ActorSystem
55import akka .http .scaladsl .model .headers .RawHeader
66import akka .http .scaladsl .model .{HttpResponse , StatusCodes }
7- import akka .http .scaladsl .server .{Directives , ExceptionHandler , Route }
7+ import akka .http .scaladsl .server .{
8+ AuthorizationFailedRejection ,
9+ Directives ,
10+ ExceptionHandler ,
11+ MethodRejection ,
12+ MissingCookieRejection ,
13+ RejectionHandler ,
14+ Route ,
15+ ValidationRejection
16+ }
817import app .softnetwork .api .server .config .ServerSettings
918import org .json4s .Formats
1019import app .softnetwork .serialization ._
@@ -22,6 +31,32 @@ trait ApiRoutes extends Directives with GrpcServices with DefaultComplete {
2231
2332 def log : Logger
2433
34+ val rejectionHandler : RejectionHandler =
35+ RejectionHandler
36+ .newBuilder()
37+ .handle { case MissingCookieRejection (cookieName) =>
38+ complete(HttpResponse (StatusCodes .BadRequest , entity = s " $cookieName cookie required " ))
39+ }
40+ .handle { case AuthorizationFailedRejection =>
41+ complete(StatusCodes .Forbidden )
42+ }
43+ .handle { case ValidationRejection (msg, _) =>
44+ complete(HttpResponse (StatusCodes .InternalServerError , entity = msg))
45+ }
46+ .handleAll[MethodRejection ] { methodRejections =>
47+ val names = methodRejections.map(_.supported.name)
48+ complete(
49+ HttpResponse (
50+ StatusCodes .MethodNotAllowed ,
51+ entity = s " Supported methods: ${names mkString " or " }! "
52+ )
53+ )
54+ }
55+ .handleNotFound {
56+ complete(HttpResponse (StatusCodes .NotFound , entity = " Not found" ))
57+ }
58+ .result()
59+
2560 val timeoutExceptionHandler : ExceptionHandler =
2661 ExceptionHandler { case e : TimeoutException =>
2762 extractUri { uri =>
@@ -35,23 +70,25 @@ trait ApiRoutes extends Directives with GrpcServices with DefaultComplete {
3570
3671 final def mainRoutes : ActorSystem [_] => Route = system => {
3772 val routes = concat((HealthCheckService :: apiRoutes(system)).map(_.route): _* )
38- handleExceptions(timeoutExceptionHandler) {
39- logRequestResult(" RestAll" ) {
40- pathPrefix(config.ServerSettings .RootPath ) {
41- Try (
42- respondWithHeaders(RawHeader (" Api-Version" , applicationVersion)) {
43- routes
44- }
45- ) match {
46- case Success (s) => s
47- case Failure (f) =>
48- log.error(f.getMessage, f.getCause)
49- complete(
50- HttpResponse (
51- StatusCodes .InternalServerError ,
52- entity = f.getMessage
73+ handleRejections(rejectionHandler) {
74+ handleExceptions(timeoutExceptionHandler) {
75+ logRequestResult(" RestAll" ) {
76+ pathPrefix(config.ServerSettings .RootPath ) {
77+ Try (
78+ respondWithHeaders(RawHeader (" Api-Version" , applicationVersion)) {
79+ routes
80+ }
81+ ) match {
82+ case Success (s) => s
83+ case Failure (f) =>
84+ log.error(f.getMessage, f.getCause)
85+ complete(
86+ HttpResponse (
87+ StatusCodes .InternalServerError ,
88+ entity = f.getMessage
89+ )
5390 )
54- )
91+ }
5592 }
5693 }
5794 }
0 commit comments