-
Notifications
You must be signed in to change notification settings - Fork 18
/
DSPApiDirectives.scala
33 lines (26 loc) · 1.28 KB
/
DSPApiDirectives.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* Copyright © 2021 - 2023 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.http.directives
import akka.http.scaladsl.server
import akka.http.scaladsl.server.Directives.handleExceptions
import akka.http.scaladsl.server.Directives.handleRejections
import akka.http.scaladsl.server.ExceptionHandler
import akka.http.scaladsl.server.RejectionHandler
import ch.megard.akka.http.cors.scaladsl.CorsDirectives
import org.knora.webapi.config.AppConfig
import org.knora.webapi.http.handler.KnoraExceptionHandler
/**
* DSP-API HTTP directives, used by wrapping around a routes, to influence
* rejections and exception handling
*/
object DSPApiDirectives {
// Our rejection handler. Here we are using the default one from the CORS lib
def rejectionHandler: RejectionHandler = CorsDirectives.corsRejectionHandler.withFallback(RejectionHandler.default)
// Our exception handler
def exceptionHandler(appConfig: AppConfig): ExceptionHandler = KnoraExceptionHandler(appConfig)
// Combining the two handlers for convenience
def handleErrors(appConfig: AppConfig): server.Directive[Unit] =
handleRejections(rejectionHandler) & handleExceptions(exceptionHandler(appConfig))
}