-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(proxy): add global exception handler for proxy (#537)
Signed-off-by: SSpirits <[email protected]>
- Loading branch information
1 parent
9f166e0
commit 90cc1cf
Showing
10 changed files
with
196 additions
and
25 deletions.
There are no files selected for viewing
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
61 changes: 61 additions & 0 deletions
61
proxy/src/main/java/com/automq/rocketmq/proxy/exception/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,61 @@ | ||
/* | ||
* 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 com.automq.rocketmq.proxy.exception; | ||
|
||
import apache.rocketmq.v2.Code; | ||
import apache.rocketmq.v2.Status; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.apache.rocketmq.remoting.protocol.RemotingCommand; | ||
import org.apache.rocketmq.remoting.protocol.ResponseCode; | ||
|
||
public class ExceptionHandler { | ||
private static final Map<Code, Integer> PROXY_EXCEPTION_RESPONSE_CODE_MAP = new HashMap<>() { | ||
{ | ||
put(Code.FORBIDDEN, ResponseCode.NO_PERMISSION); | ||
put(Code.TOPIC_NOT_FOUND, ResponseCode.TOPIC_NOT_EXIST); | ||
put(Code.CONSUMER_GROUP_NOT_FOUND, ResponseCode.SUBSCRIPTION_GROUP_NOT_EXIST); | ||
put(Code.MESSAGE_NOT_FOUND, ResponseCode.NO_MESSAGE); | ||
put(Code.MESSAGE_PROPERTY_CONFLICT_WITH_TYPE, ResponseCode.MESSAGE_ILLEGAL); | ||
put(Code.INTERNAL_ERROR, ResponseCode.SYSTEM_ERROR); | ||
put(Code.INTERNAL_SERVER_ERROR, ResponseCode.SYSTEM_ERROR); | ||
} | ||
}; | ||
|
||
public static Optional<RemotingCommand> convertToRemotingResponse(Throwable throwable) { | ||
if (throwable instanceof ProxyException proxyException) { | ||
Integer responseCode = PROXY_EXCEPTION_RESPONSE_CODE_MAP.getOrDefault(proxyException.getErrorCode(), ResponseCode.SYSTEM_ERROR); | ||
RemotingCommand response = RemotingCommand.buildErrorResponse(responseCode, proxyException.getMessage()); | ||
return Optional.of(response); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public static Optional<Status> convertToGrpcStatus(Throwable throwable) { | ||
if (throwable instanceof ProxyException proxyException) { | ||
return Optional.of( | ||
Status.newBuilder() | ||
.setCode(proxyException.getErrorCode()) | ||
.setMessage(proxyException.getMessage()) | ||
.build() | ||
); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
proxy/src/main/java/com/automq/rocketmq/proxy/exception/ProxyException.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,48 @@ | ||
/* | ||
* 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 com.automq.rocketmq.proxy.exception; | ||
|
||
import apache.rocketmq.v2.Code; | ||
import com.automq.rocketmq.common.exception.RocketMQRuntimeException; | ||
|
||
public class ProxyException extends RocketMQRuntimeException { | ||
public ProxyException(Code errorCode) { | ||
super(errorCode.getNumber()); | ||
} | ||
|
||
public ProxyException(Code errorCode, String message) { | ||
super(errorCode.getNumber(), message); | ||
} | ||
|
||
public ProxyException(Code errorCode, String message, Throwable cause) { | ||
super(errorCode.getNumber(), message, cause); | ||
} | ||
|
||
public ProxyException(Code errorCode, Throwable cause) { | ||
super(errorCode.getNumber(), cause); | ||
} | ||
|
||
public ProxyException(Code errorCode, String message, Throwable cause, boolean enableSuppression, | ||
boolean writableStackTrace) { | ||
super(errorCode.getNumber(), message, cause, enableSuppression, writableStackTrace); | ||
} | ||
|
||
public Code getErrorCode() { | ||
return Code.forNumber(errorCode); | ||
} | ||
} |
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
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.