From 34bb1d4dbe76e8719590e84482078d94f892f522 Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 19 Jun 2023 21:44:16 +0800 Subject: [PATCH] [KYUUBI #4977] Log error message when REST API invocation error occurs ### _Why are the changes needed?_ Currently, there is no error message on Kyuubi server's log when REST API invocation error occurs, and since we are building REST API, all response media types should be JSON. ### _How was this patch tested?_ - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [x] [Run test](https://kyuubi.readthedocs.io/en/master/contributing/code/testing.html#running-tests) locally before make a pull request Closes #4977 from pan3793/error-log. Closes #4977 00c2b0c0f [Cheng Pan] Update kyuubi-server/src/main/scala/org/apache/kyuubi/server/trino/api/api.scala 5fe00474f [Cheng Pan] trino f988329f8 [Cheng Pan] nit 74f9ed762 [Cheng Pan] log error message for REST api invocation Authored-by: Cheng Pan Signed-off-by: Cheng Pan --- .../main/scala/org/apache/kyuubi/server/api/api.scala | 10 ++++++---- .../scala/org/apache/kyuubi/server/trino/api/api.scala | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/api.scala b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/api.scala index deadcf9ab..b56131542 100644 --- a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/api.scala +++ b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/api.scala @@ -25,6 +25,7 @@ import javax.ws.rs.ext.{ExceptionMapper, Provider} import org.eclipse.jetty.server.handler.ContextHandler +import org.apache.kyuubi.Logging import org.apache.kyuubi.server.KyuubiRestFrontendService private[api] trait ApiRequestContext { @@ -39,18 +40,19 @@ private[api] trait ApiRequestContext { } @Provider -class RestExceptionMapper extends ExceptionMapper[Exception] { +class RestExceptionMapper extends ExceptionMapper[Exception] with Logging { override def toResponse(exception: Exception): Response = { + warn("Error occurs on accessing REST API.", exception) exception match { case e: WebApplicationException => Response.status(e.getResponse.getStatus) - .`type`(e.getResponse.getMediaType) - .entity(e.getMessage) + .`type`(MediaType.APPLICATION_JSON) + .entity(Map("message" -> e.getMessage)) .build() case e => Response.status(Response.Status.INTERNAL_SERVER_ERROR) .`type`(MediaType.APPLICATION_JSON) - .entity(e.getMessage) + .entity(Map("message" -> e.getMessage)) .build() } } diff --git a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/trino/api/api.scala b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/trino/api/api.scala index 76f8a1ca3..50887dabd 100644 --- a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/trino/api/api.scala +++ b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/trino/api/api.scala @@ -25,6 +25,7 @@ import javax.ws.rs.ext.{ExceptionMapper, Provider} import org.eclipse.jetty.server.handler.ContextHandler +import org.apache.kyuubi.Logging import org.apache.kyuubi.server.KyuubiTrinoFrontendService private[api] trait ApiRequestContext { @@ -39,18 +40,19 @@ private[api] trait ApiRequestContext { } @Provider -class RestExceptionMapper extends ExceptionMapper[Exception] { +class RestExceptionMapper extends ExceptionMapper[Exception] with Logging { override def toResponse(exception: Exception): Response = { + warn("Error occurs on accessing Trino API.", exception) exception match { case e: WebApplicationException => Response.status(e.getResponse.getStatus) - .`type`(e.getResponse.getMediaType) - .entity(e.getMessage) + .`type`(MediaType.APPLICATION_JSON) + .entity(Map("message" -> e.getMessage)) .build() case e => Response.status(Response.Status.INTERNAL_SERVER_ERROR) .`type`(MediaType.APPLICATION_JSON) - .entity(e.getMessage) + .entity(Map("message" -> e.getMessage)) .build() } }