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.
Fix [Broker Interceptor] Pulsar didn't respond error messages when th…
…row InterceptException (#17)
- Loading branch information
1 parent
84241d4
commit 9e65a8c
Showing
8 changed files
with
200 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"properties" : { | ||
"maxTenants" : 5, | ||
"maxNamespaces" : 2, | ||
"maxTopics" : 5, | ||
"maxBundlesPerNamespace" : 1, | ||
"resourceCacheRefreshInternalInMillis": 1000 | ||
} | ||
} |
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,53 @@ | ||
<!-- | ||
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. | ||
--> | ||
<configuration status="WARN" monitorInterval="30"> | ||
<Properties> | ||
<!--自定义一些常量,之后使用${变量名}引用--> | ||
<Property name="logFilePath">/Users/tboy/logs</Property> | ||
<Property name="logFileName">test.log</Property> | ||
</Properties> | ||
<appenders> | ||
<console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%c{50}#%M:%L] : %m%n"/> | ||
</console> | ||
|
||
<RollingFile name="RollingFileWarn" fileName="${logFilePath}/warn.log" | ||
filePattern="${logFilePath}/warn-%d{yyyy-MM-dd}-%i.log"> | ||
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/> | ||
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> | ||
<Policies> | ||
<TimeBasedTriggeringPolicy/> | ||
<SizeBasedTriggeringPolicy size="100 MB"/> | ||
</Policies> | ||
</RollingFile> | ||
</appenders> | ||
|
||
<loggers> | ||
<logger name="org.apache.zookeeper" level="warn"/> | ||
<logger name="org.eclipse.jetty" level="info"/> | ||
<logger name="org.asynchttpclient" level="info"/> | ||
<logger name="org.apache.bookkeeper.proto" level="info"/> | ||
<root level="INFO"> | ||
<appender-ref ref="Console"/> | ||
<!-- <appender-ref ref="RollingFileWarn"/>--> | ||
</root> | ||
</loggers> | ||
</configuration> |
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
55 changes: 55 additions & 0 deletions
55
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,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()); | ||
handler.handle(response, new IllegalArgumentException("illegal argument exception ")); | ||
Mockito.verify(response, Mockito.times(3)).sendError(Mockito.anyInt(), Mockito.anyString()); | ||
Response response2 = Mockito.mock(Response.class); | ||
HttpChannel httpChannel = Mockito.mock(HttpChannel.class); | ||
Mockito.when(response2.getHttpChannel()).thenReturn(httpChannel); | ||
handler.handle(response2, new InterceptException(HttpStatus.PRECONDITION_FAILED_412, "Reach the max tenants [5] 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