Copilot commented on code in PR #12784:
URL: https://github.com/apache/gravitino/pull/12784#discussion_r3913028861


##########
server/src/main/java/org/apache/gravitino/server/web/mapper/ParamExceptionMapper.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.server.web.mapper;
+
+import javax.annotation.Nullable;
+import javax.annotation.Priority;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import org.apache.gravitino.server.web.Utils;
+import org.glassfish.jersey.server.ParamException;
+
+/**
+ * ParamExceptionMapper returns a structured JSON error body when Jersey fails 
to convert a request
+ * parameter (e.g. a typed {@code @PathParam}) into its declared Java type, 
instead of letting the
+ * servlet container fall back to Jetty's default HTML error page.
+ *
+ * <p>{@link ParamException} is thrown by Jersey itself, before any resource 
method or other
+ * registered {@link ExceptionMapper} runs, so this is the only place such a 
conversion failure can
+ * be intercepted.
+ */
+@Priority(1)
+public class ParamExceptionMapper implements ExceptionMapper<ParamException> {
+
+  @Override
+  public Response toResponse(ParamException exception) {
+    String message =
+        String.format(
+            "Invalid value for %s parameter '%s'%s",
+            exception.getParameterType().getSimpleName(),
+            exception.getParameterName(),
+            causeMessage(exception.getCause()));
+
+    if (exception.getResponse().getStatus() == 
Response.Status.NOT_FOUND.getStatusCode()) {
+      return Utils.notFound(exception.getClass().getSimpleName(), message);
+    }
+    return Utils.illegalArguments(message);

Review Comment:
   This mapper returns 404s with ErrorResponse.type set to the ParamException 
subclass name (e.g. "PathParamException"). The PR description/issue text says 
malformed typed path params should return type "NotFoundException"; please 
align the user-facing ErrorResponse.type (and the IT expectation) with the 
intended contract.



##########
server/src/main/java/org/apache/gravitino/server/GravitinoServer.java:
##########
@@ -170,6 +172,8 @@ protected void configure() {
     register(JsonProcessingExceptionMapper.class);
     register(JsonParseExceptionMapper.class);
     register(JsonMappingExceptionMapper.class);
+    register(ParamExceptionMapper.class);
+    register(NotFoundExceptionMapper.class);

Review Comment:
   The PR description states the fix is implemented via a Jetty 
JsonErrorHandler and a new JettyServer createErrorHandler() extension point, 
but this change set only registers JAX-RS ExceptionMappers. Either the 
Jetty-side handling is missing (and the original HTML-error-path may still 
occur) or the PR description/title should be updated to reflect the actual 
approach.



##########
server/src/main/java/org/apache/gravitino/server/web/mapper/ParamExceptionMapper.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.server.web.mapper;
+
+import javax.annotation.Nullable;
+import javax.annotation.Priority;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import org.apache.gravitino.server.web.Utils;
+import org.glassfish.jersey.server.ParamException;
+
+/**
+ * ParamExceptionMapper returns a structured JSON error body when Jersey fails 
to convert a request
+ * parameter (e.g. a typed {@code @PathParam}) into its declared Java type, 
instead of letting the
+ * servlet container fall back to Jetty's default HTML error page.
+ *
+ * <p>{@link ParamException} is thrown by Jersey itself, before any resource 
method or other
+ * registered {@link ExceptionMapper} runs, so this is the only place such a 
conversion failure can
+ * be intercepted.

Review Comment:
   The Javadoc claims ParamException is thrown "before any ... ExceptionMapper 
runs", but this class *is* an ExceptionMapper for ParamException. This is 
misleading about when/how the exception is handled; it’s thrown before resource 
methods execute, but it can still be mapped here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to