forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Broker Interceptor] Fix Pulsar didn't respond error messages when th…
…row InterceptException (apache#11650)
- Loading branch information
1 parent
e505018
commit 4793ff7
Showing
13 changed files
with
173 additions
and
32 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
pulsar-broker/src/main/java/org/apache/pulsar/broker/web/ExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* 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 java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.ws.rs.core.Response; | ||
import org.apache.pulsar.common.intercept.InterceptException; | ||
import org.eclipse.jetty.http.HttpVersion; | ||
import org.eclipse.jetty.http.MetaData; | ||
|
||
/** | ||
* Exception handler for handle exception. | ||
*/ | ||
public class ExceptionHandler { | ||
|
||
public void handle(ServletResponse response, Exception ex) throws IOException { | ||
if (ex instanceof InterceptException) { | ||
String reason = ex.getMessage(); | ||
byte[] content = reason.getBytes(StandardCharsets.UTF_8); | ||
MetaData.Response info = new MetaData.Response(); | ||
info.setHttpVersion(HttpVersion.HTTP_1_1); | ||
info.setReason(reason); | ||
info.setStatus(((InterceptException) ex).getErrorCode()); | ||
info.setContentLength(content.length); | ||
if (response instanceof org.eclipse.jetty.server.Response) { | ||
((org.eclipse.jetty.server.Response) response).getHttpChannel().sendResponse(info, | ||
ByteBuffer.wrap(content), true); | ||
} else { | ||
((HttpServletResponse) response).sendError(((InterceptException) ex).getErrorCode(), | ||
ex.getMessage()); | ||
} | ||
} else { | ||
((HttpServletResponse) response).sendError(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), | ||
ex.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
pulsar-broker/src/test/java/org/apache/pulsar/broker/web/ExceptionHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* 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.server.HttpChannel; | ||
import org.eclipse.jetty.server.Response; | ||
import org.mockito.Mockito; | ||
import org.testng.annotations.Test; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import static org.eclipse.jetty.http.HttpStatus.INTERNAL_SERVER_ERROR_500; | ||
import static org.eclipse.jetty.http.HttpStatus.PRECONDITION_FAILED_412; | ||
|
||
/** | ||
* Unit test for ExceptionHandler. | ||
*/ | ||
@Test(groups = "broker") | ||
public class ExceptionHandlerTest { | ||
|
||
@Test | ||
@SneakyThrows | ||
public void testHandle() { | ||
String restriction = "Reach the max tenants [5] restriction"; | ||
String internal = "internal exception"; | ||
String illegal = "illegal argument exception "; | ||
ExceptionHandler handler = new ExceptionHandler(); | ||
HttpServletResponse response = Mockito.mock(HttpServletResponse.class); | ||
handler.handle(response, new InterceptException(PRECONDITION_FAILED_412, restriction)); | ||
Mockito.verify(response).sendError(PRECONDITION_FAILED_412, restriction); | ||
|
||
handler.handle(response, new InterceptException(INTERNAL_SERVER_ERROR_500, internal)); | ||
Mockito.verify(response).sendError(INTERNAL_SERVER_ERROR_500, internal); | ||
|
||
handler.handle(response, new IllegalArgumentException(illegal)); | ||
Mockito.verify(response).sendError(INTERNAL_SERVER_ERROR_500, illegal); | ||
|
||
Response response2 = Mockito.mock(Response.class); | ||
HttpChannel httpChannel = Mockito.mock(HttpChannel.class); | ||
Mockito.when(response2.getHttpChannel()).thenReturn(httpChannel); | ||
handler.handle(response2, new InterceptException(PRECONDITION_FAILED_412, restriction)); | ||
Mockito.verify(httpChannel).sendResponse(Mockito.any(), Mockito.any(), Mockito.anyBoolean()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.