Skip to content
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

Extending business exceptions through the java spi mechanism #298

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -21,15 +21,33 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.apache.apisix.plugin.runner.A6ErrResponse;
import org.apache.apisix.plugin.runner.exception.ExceptionCaught;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class ExceptionCaughtHandler extends ChannelInboundHandlerAdapter {
private final Logger logger = LoggerFactory.getLogger(ExceptionCaughtHandler.class);

List<ExceptionCaught> exceptionCaughtList = new ArrayList<ExceptionCaught>();

public ExceptionCaughtHandler() {

}

public ExceptionCaughtHandler(List<ExceptionCaught> exceptionCaughtList) {
this.exceptionCaughtList = exceptionCaughtList;
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
logger.error("handle request error: ", cause);
if (!exceptionCaughtList.isEmpty()) {
exceptionCaughtList.get(0).exceptionCaught(ctx, cause);
return;
}
A6ErrResponse errResponse = new A6ErrResponse(Code.SERVICE_UNAVAILABLE);
ctx.writeAndFlush(errResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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));
}

private void cleanCtx() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@

package org.apache.apisix.plugin.runner.server;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.google.common.cache.Cache;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
Expand All @@ -47,16 +32,33 @@
import io.netty.channel.unix.DomainSocketChannel;
import io.netty.handler.logging.LoggingHandler;
import lombok.RequiredArgsConstructor;

import org.apache.apisix.plugin.runner.A6Conf;
import org.apache.apisix.plugin.runner.A6ConfigWatcher;
import org.apache.apisix.plugin.runner.exception.ExceptionCaught;
import org.apache.apisix.plugin.runner.filter.PluginFilter;
import org.apache.apisix.plugin.runner.handler.PrepareConfHandler;
import org.apache.apisix.plugin.runner.handler.RpcCallHandler;
import org.apache.apisix.plugin.runner.handler.PayloadDecoder;
import org.apache.apisix.plugin.runner.handler.BinaryProtocolDecoder;
import org.apache.apisix.plugin.runner.handler.PayloadEncoder;
import org.apache.apisix.plugin.runner.handler.ExceptionCaughtHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.ServiceLoader;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
Expand All @@ -72,9 +74,16 @@ public class ApplicationRunner implements CommandLineRunner {
private ObjectProvider<PluginFilter> filterProvider;
private ObjectProvider<A6ConfigWatcher> watcherProvider;

public static final List<ExceptionCaught> EXCEPTION_LIST = new ArrayList<ExceptionCaught>();

static {
ServiceLoader<ExceptionCaught> serviceLoader = ServiceLoader.load(ExceptionCaught.class);
serviceLoader.forEach(EXCEPTION_LIST::add);
}

@Autowired
public ApplicationRunner(Cache<Long, A6Conf> cache,
ObjectProvider<PluginFilter> filterProvider, ObjectProvider<A6ConfigWatcher> watcherProvider) {
ObjectProvider<PluginFilter> filterProvider, ObjectProvider<A6ConfigWatcher> watcherProvider) {
this.cache = cache;
this.filterProvider = filterProvider;
this.watcherProvider = watcherProvider;
Expand Down Expand Up @@ -133,7 +142,7 @@ protected void initChannel(DomainSocketChannel channel) {
.addLast("payloadDecoder", new PayloadDecoder())
.addAfter("payloadDecoder", "prepareConfHandler", createConfigReqHandler(cache, filterProvider, watcherProvider))
.addAfter("prepareConfHandler", "hTTPReqCallHandler", createA6HttpHandler(cache))
.addLast("exceptionCaughtHandler", new ExceptionCaughtHandler());
.addLast("exceptionCaughtHandler", new ExceptionCaughtHandler(EXCEPTION_LIST));

}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
import org.apache.apisix.plugin.runner.A6ConfigRequest;
import org.apache.apisix.plugin.runner.A6ConfigResponse;
import org.apache.apisix.plugin.runner.A6ConfigWatcher;
import org.apache.apisix.plugin.runner.A6ErrResponse;
import org.apache.apisix.plugin.runner.HttpRequest;
import org.apache.apisix.plugin.runner.HttpResponse;
import org.apache.apisix.plugin.runner.filter.PluginFilter;
import org.apache.apisix.plugin.runner.filter.PluginFilterChain;
import org.apache.apisix.plugin.runner.server.ApplicationRunner;
import org.apache.apisix.plugin.runner.PostResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -173,15 +174,15 @@ public Boolean requiredBody() {

A6ConfigRequest request = new A6ConfigRequest(req);
prepareConfHandler = new PrepareConfHandler(cache, filters, watchers);
channel = new EmbeddedChannel(new BinaryProtocolDecoder(), prepareConfHandler);
channel = new EmbeddedChannel(new BinaryProtocolDecoder(), prepareConfHandler, new ExceptionCaughtHandler(ApplicationRunner.EXCEPTION_LIST));
channel.writeInbound(request);
channel.finish();
A6ConfigResponse response = channel.readOutbound();
confToken = response.getConfToken();

prepareConfHandler = new PrepareConfHandler(cache, filters, watchers);
rpcCallHandler = new RpcCallHandler(cache);
channel = new EmbeddedChannel(new BinaryProtocolDecoder(), prepareConfHandler, rpcCallHandler);
channel = new EmbeddedChannel(new BinaryProtocolDecoder(), prepareConfHandler, rpcCallHandler, new ExceptionCaughtHandler(ApplicationRunner.EXCEPTION_LIST));
}

@AfterEach
Expand All @@ -202,9 +203,9 @@ void testCannotFindConfToken() {
HttpRequest request = new HttpRequest(req);
channel.writeInbound(request);
channel.finish();
A6ErrResponse response = channel.readOutbound();
PostResponse response = channel.readOutbound();
io.github.api7.A6.Err.Resp err = io.github.api7.A6.Err.Resp.getRootAsResp(response.encode());
Assertions.assertEquals(err.code(), Code.CONF_TOKEN_NOT_FOUND);
Assertions.assertEquals(err.code(), Code.SERVICE_UNAVAILABLE);
}

@Test
Expand Down
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);
}
}
Loading
Loading