Copilot commented on code in PR #12878: URL: https://github.com/apache/gravitino/pull/12878#discussion_r3922720516
########## server/src/main/java/org/apache/gravitino/server/web/mapper/WebApplicationExceptionMapper.java: ########## @@ -0,0 +1,72 @@ +/* + * 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.Priority; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.dto.responses.ErrorResponse; +import org.eclipse.jetty.http.HttpStatus; + +/** + * WebApplicationExceptionMapper returns a structured JSON error body for any {@link + * WebApplicationException} that JAX-RS/Jersey raises itself before a resource method runs (e.g. a + * wrong HTTP method or an unacceptable {@code Accept}/{@code Content-Type} header), instead of + * letting the servlet container fall back to Jetty's default HTML error page. + * + * <p>{@link org.glassfish.jersey.server.ParamException} and {@link javax.ws.rs.NotFoundException} + * are subtypes of {@link WebApplicationException} with their own, more specific mappers ({@link + * ParamExceptionMapper}, {@link NotFoundExceptionMapper}); JAX-RS always prefers the mapper + * registered for the nearest type in the exception's class hierarchy, so this mapper only applies + * to every other case in the family. + */ +@Priority(1) +public class WebApplicationExceptionMapper implements ExceptionMapper<WebApplicationException> { + + @Override + public Response toResponse(WebApplicationException exception) { + int status = exception.getResponse().getStatus(); + String message = + StringUtils.isBlank(exception.getMessage()) + ? HttpStatus.getMessage(status) + : exception.getMessage(); + + return Response.status(status) + .entity(toErrorResponse(status, message)) + .type(MediaType.APPLICATION_JSON) + .build(); Review Comment: This mapper rebuilds a fresh `Response` via `Response.status(status)`, which drops any headers present on the original `WebApplicationException` response. For `NotAllowedException` (405), JAX-RS is expected to include an `Allow` header listing permitted methods; losing it is a behavioral regression for clients and violates HTTP semantics. Prefer building from the original response and only overriding the entity + Content-Type. ########## server/src/test/java/org/apache/gravitino/server/web/mapper/TestWebApplicationExceptionMapper.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.ws.rs.NotAcceptableException; +import javax.ws.rs.NotAllowedException; +import javax.ws.rs.NotSupportedException; +import javax.ws.rs.core.Response; +import org.apache.gravitino.dto.responses.ErrorConstants; +import org.apache.gravitino.dto.responses.ErrorResponse; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestWebApplicationExceptionMapper { + + private final WebApplicationExceptionMapper mapper = new WebApplicationExceptionMapper(); + + @Test + public void testWrongHttpMethodReturnsJsonBody() { + Response response = mapper.toResponse(new NotAllowedException("GET")); + ErrorResponse entity = (ErrorResponse) response.getEntity(); + + Assertions.assertEquals( + Response.Status.METHOD_NOT_ALLOWED.getStatusCode(), response.getStatus()); + Assertions.assertEquals(ErrorConstants.UNSUPPORTED_OPERATION_CODE, entity.getCode()); + Assertions.assertFalse(entity.getMessage().isEmpty()); + } Review Comment: The 405 case test only checks status/code/message, but not that the mapper preserves HTTP semantics like the `Allow` header from `NotAllowedException`. Adding an assertion for `Allow` (contains "GET") will prevent regressions where headers are dropped. -- 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]
