/
ExceptionHandlerZ.scala
58 lines (46 loc) · 1.77 KB
/
ExceptionHandlerZ.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Copyright © 2021 - 2022 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.handler
import spray.json._
import zhttp.http.Http
import zhttp.http.Response
import dsp.errors.RequestRejectedException
import org.knora.webapi.config.AppConfig
import org.knora.webapi.http.status.ApiStatusCodesZ
/**
* Migrated from [[org.knora.webapi.http.handler.KnoraExceptionHandler]]
*/
object ExceptionHandlerZ {
private val GENERIC_INTERNAL_SERVER_ERROR_MESSAGE =
"The request could not be completed because of an internal server error."
def exceptionToJsonHttpResponseZ(ex: Throwable, appConfig: AppConfig): Http[Any, Nothing, Any, Response] = {
// Get the HTTP status code that corresponds to the exception.
val httpStatus = ApiStatusCodesZ.fromExceptionZ(ex)
// Generate an HTTP response containing the error message ...
val responseFields: Map[String, JsValue] = Map(
"error" -> JsString(makeClientErrorMessage(ex, appConfig))
)
val json = JsObject(responseFields).compactPrint
// ... and the HTTP status code.
Http.response(Response.json(json).setStatus(httpStatus))
}
/**
* Given an exception, returns an error message suitable for clients.
*
* @param ex the exception.
* @param appConfig the application's configuration.
* @return an error message suitable for clients.
*/
private def makeClientErrorMessage(ex: Throwable, appConfig: AppConfig): String =
ex match {
case rre: RequestRejectedException => rre.toString
case other =>
if (appConfig.showInternalErrors) {
other.toString
} else {
GENERIC_INTERNAL_SERVER_ERROR_MESSAGE
}
}
}