Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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,24 @@
/*
* 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.servicecomb.transport.rest.client;

import io.vertx.core.Handler;
import org.apache.servicecomb.foundation.common.utils.SPIOrder;

public interface HttpRestClientInvocationHook extends Handler<Throwable>, SPIOrder {
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
import org.apache.servicecomb.foundation.common.utils.ExceptionUtils;
import org.apache.servicecomb.foundation.common.utils.JsonUtils;
import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
import org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext;
import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
Expand All @@ -47,6 +48,7 @@
import org.apache.servicecomb.swagger.invocation.Response;
import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.apache.servicecomb.transport.rest.client.HttpRestClientInvocationHook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -116,12 +118,17 @@ public void invoke(Invocation invocation, AsyncResponse asyncResp) throws Except
filter.beforeSendRequest(invocation, requestEx);
}
}
List<HttpRestClientInvocationHook> httpRestClientInvocationHooks =
SPIServiceUtils.getAllService(HttpRestClientInvocationHook.class);

clientRequest.exceptionHandler(e -> {
invocation.getTraceIdLogger()
.error(LOGGER, "Failed to send request, alreadyFailed:{}, local:{}, remote:{}, message={}.",
alreadyFailed, getLocalAddress(), ipPort.getSocketAddress(),
ExceptionUtils.getExceptionMessageWithoutTrace(e));
httpRestClientInvocationHooks.forEach(httpServerExceptionHandler -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can add a custom handler to fillful your requirements.

e.g. 'MyHandler implements Handler'

servicecomb.handler.chain.Consumer.defaut: your_current_handlers,myhandler

myhandler can process the exception thrown in RestClientInvocation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.servicecomb.io/java-chassis/zh_CN/references-handlers/intruduction/

I found it didn't meet my requirements through experiments.

My handler executes before the exception is returned, and does not return to handler after the exception is thrown.

I can't get this exception message.

Is there any other way

Copy link
Contributor

@liubao68 liubao68 Dec 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public class MyHandler implements Handler {

  private static final Logger LOGGER = LoggerFactory.getLogger(MyHandler.class);

  @Override
  public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    LOGGER.info("before");

    invocation.next(response -> {
         LOGGER.info("after");
    });
  }
}

You can find many Handler implementations in servicecomb-java-chassis project.

httpServerExceptionHandler.handle(e);
});
throwableHandler.handle(e);
});

Expand Down