You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/08/12 13:28:31 UTC

[GitHub] [pulsar] eolivelli commented on a change in pull request #11650: Fix [Broker Interceptor] Pulsar didn't respond error messages when throw InterceptException

eolivelli commented on a change in pull request #11650:
URL: https://github.com/apache/pulsar/pull/11650#discussion_r687710188



##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/web/WebService.java
##########
@@ -159,11 +159,12 @@ public void addServlet(String path, ServletHolder servletHolder, boolean require
                 context.setAttribute(key, value);
             });
         }
+        ExceptionHandler handler = new ExceptionHandler();

Review comment:
       we can push this "new" inside the "if" block, this way we can save us from creating it if not needed

##########
File path: pulsar-broker/src/test/java/org/apache/pulsar/broker/web/ExceptionHandlerTest.java
##########
@@ -0,0 +1,55 @@
+/**
+ * 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.pulsar.broker.web;
+
+import lombok.SneakyThrows;
+import org.apache.pulsar.common.intercept.InterceptException;
+import org.eclipse.jetty.http.HttpStatus;
+import org.eclipse.jetty.server.HttpChannel;
+import org.eclipse.jetty.server.Response;
+import org.mockito.Mockito;
+import org.testng.annotations.Test;
+
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Unit test for ExceptionHandler.
+ */
+@Test(groups = "broker")
+public class ExceptionHandlerTest {
+
+    @Test
+    @SneakyThrows
+    public void testHandle() {
+        ExceptionHandler handler = new ExceptionHandler();
+        HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
+        handler.handle(response, new InterceptException(HttpStatus.PRECONDITION_FAILED_412, "Reach the max tenants [5] restriction"));
+        Mockito.verify(response).sendError(Mockito.anyInt(), Mockito.anyString());
+        handler.handle(response, new InterceptException(HttpStatus.INTERNAL_SERVER_ERROR_500, "internal exception"));
+        Mockito.verify(response, Mockito.times(2)).sendError(Mockito.anyInt(), Mockito.anyString());

Review comment:
       can we match the exact error code and message ?




-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org