-
Notifications
You must be signed in to change notification settings - Fork 101
Extending business exceptions through the java spi mechanism #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
3faaf84
8e4b4c2
b09f68d
3621373
c409b42
76c2903
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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.apisix.plugin.runner.exception; | ||
|
||
public class ApisixException extends RuntimeException { | ||
|
||
private int code; | ||
|
||
public ApisixException(int code, String msg) { | ||
super(msg); | ||
this.code = code; | ||
} | ||
|
||
public int getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(int code) { | ||
this.code = code; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.apisix.plugin.runner.exception; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
|
||
public interface ExceptionCaught { | ||
|
||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,39 +17,37 @@ | |
|
||
package org.apache.apisix.plugin.runner.handler; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
|
||
import com.google.common.cache.Cache; | ||
import io.github.api7.A6.Err.Code; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.apisix.plugin.runner.A6Conf; | ||
import org.apache.apisix.plugin.runner.A6ErrRequest; | ||
import org.apache.apisix.plugin.runner.A6ErrResponse; | ||
import org.apache.apisix.plugin.runner.A6Request; | ||
import org.apache.apisix.plugin.runner.ExtraInfoRequest; | ||
import org.apache.apisix.plugin.runner.ExtraInfoResponse; | ||
import org.apache.apisix.plugin.runner.HttpRequest; | ||
import org.apache.apisix.plugin.runner.HttpResponse; | ||
import org.apache.apisix.plugin.runner.PostRequest; | ||
import org.apache.apisix.plugin.runner.PostResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.util.CollectionUtils; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.apache.apisix.plugin.runner.exception.ApisixException; | ||
import org.apache.apisix.plugin.runner.constants.Constants; | ||
import org.apache.apisix.plugin.runner.filter.PluginFilter; | ||
import org.apache.apisix.plugin.runner.filter.PluginFilterChain; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.util.CollectionUtils; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
|
||
@RequiredArgsConstructor | ||
public class RpcCallHandler extends SimpleChannelInboundHandler<A6Request> { | ||
|
@@ -311,8 +309,7 @@ private void preReadReq() { | |
} | ||
|
||
private void errorHandle(ChannelHandlerContext ctx, int code) { | ||
A6ErrResponse errResponse = new A6ErrResponse(code); | ||
ctx.writeAndFlush(errResponse); | ||
throw new ApisixException(code, Code.name(code)); | ||
Comment on lines
-314
to
+312
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please provide detailed explanation and impact assesments on moving exception handling out of the Netty stack? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no exception returned here, but I want to return it to the custom catch through throws |
||
} | ||
|
||
private void cleanCtx() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* 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.apisix.plugin.runner.handler; | ||
|
||
import io.github.api7.A6.Err.Code; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import org.apache.apisix.plugin.runner.PostResponse; | ||
import org.apache.apisix.plugin.runner.exception.ExceptionCaught; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class GlobalException implements ExceptionCaught { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(GlobalException.class); | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
// expand exception | ||
logger.warn("plugin expend"); | ||
PostResponse errResponse = new PostResponse(Code.SERVICE_UNAVAILABLE); | ||
errResponse.setStatusCode(10001); | ||
errResponse.setBody("test exception"); | ||
ctx.writeAndFlush(errResponse); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a list if we always use the first one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Spi extension is loaded as a list. Currently, only the first one is taken, which is also a preparation for subsequent expansion of other