common调整seata异常捕获

This commit is contained in:
ajaxfan
2021-04-16 16:03:27 +08:00
parent 44e8145ac2
commit 4127f39422
10 changed files with 769 additions and 6 deletions

View File

@ -53,6 +53,7 @@
<dependency>
<groupId>com.chinaunicom.ebtp</groupId>
<artifactId>mall-ebtp-cloud-seata-starter</artifactId>
<optional>true</optional>
</dependency>
<!--普罗米修斯 -->

View File

@ -15,7 +15,6 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.TransactionSystemException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
@ -35,6 +34,7 @@ import com.chinaunicom.mall.ebtp.common.util.JsonUtils;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.ExceptionUtil;
import io.seata.core.context.RootContext;
import io.seata.core.exception.BranchTransactionException;
import io.seata.core.exception.RmTransactionException;
import lombok.Getter;
import lombok.Setter;
@ -258,10 +258,10 @@ public class BusinessExceptionHandlerAdviceDefault {
return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常", Convert.toStr(body));
}
@ExceptionHandler({ TransactionSystemException.class, RmTransactionException.class })
@ExceptionHandler({ BranchTransactionException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> handleTransactionSystemException(HttpServletRequest request,
TransactionSystemException exception) {
BranchTransactionException exception) {
log.info(ExceptionUtil.stacktraceToString(exception));
if (((String) Objects.requireNonNull(exception.getMessage())).contains("may be has finished")) {
String xid = RootContext.getXID();

View File

@ -35,6 +35,7 @@ import com.chinaunicom.mall.ebtp.common.util.JsonUtils;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.ExceptionUtil;
import io.seata.core.context.RootContext;
import io.seata.core.exception.BranchTransactionException;
import io.seata.core.exception.RmTransactionException;
import io.seata.core.exception.TransactionException;
import lombok.Getter;
@ -257,10 +258,10 @@ public class BusinessExceptionHandlerAdvicePro {
return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常", Convert.toStr(body));
}
@ExceptionHandler({ TransactionException.class })
@ExceptionHandler({ BranchTransactionException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> handleTransactionSystemException(HttpServletRequest request,
TransactionSystemException exception) {
BranchTransactionException exception) {
log.info(ExceptionUtil.stacktraceToString(exception));
if (((String) Objects.requireNonNull(exception.getMessage())).contains("may be has finished")) {
String xid = RootContext.getXID();
@ -271,7 +272,6 @@ public class BusinessExceptionHandlerAdvicePro {
return BaseResponse.fail("系统繁忙,请重试", null);
}
}
return BaseResponse.fail("系统异常", exception.getMessage());
}

View File

@ -0,0 +1,127 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.exception;
import io.seata.config.Configuration;
import io.seata.config.ConfigurationFactory;
import io.seata.core.protocol.ResultCode;
import io.seata.core.protocol.transaction.AbstractTransactionRequest;
import io.seata.core.protocol.transaction.AbstractTransactionResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The type Abstract exception handler.
*
* @author sharajava
*/
public abstract class AbstractExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractExceptionHandler.class);
/**
* The constant CONFIG.
*/
protected static final Configuration CONFIG = ConfigurationFactory.getInstance();
/**
* The interface Callback.
*
* @param <T> the type parameter
* @param <S> the type parameter
*/
public interface Callback<T extends AbstractTransactionRequest, S extends AbstractTransactionResponse> {
/**
* Execute.
*
* @param request the request
* @param response the response
* @throws TransactionException the transaction exception
*/
void execute(T request, S response) throws TransactionException;
/**
* on Success
*
* @param request
* @param response
*/
void onSuccess(T request, S response);
/**
* onTransactionException
*
* @param request
* @param response
* @param exception
*/
void onTransactionException(T request, S response, TransactionException exception);
/**
* on other exception
*
* @param request
* @param response
* @param exception
*/
void onException(T request, S response, Exception exception);
}
public abstract static class AbstractCallback<T extends AbstractTransactionRequest, S extends AbstractTransactionResponse>
implements Callback<T, S> {
@Override
public void onSuccess(T request, S response) {
response.setResultCode(ResultCode.Success);
}
@Override
public void onTransactionException(T request, S response,
TransactionException tex) {
response.setTransactionExceptionCode(tex.getCode());
response.setResultCode(ResultCode.Failed);
response.setMsg("TransactionException[" + tex.getMessage() + "]");
}
@Override
public void onException(T request, S response, Exception rex) {
response.setResultCode(ResultCode.Failed);
response.setMsg("RuntimeException[" + rex.getMessage() + "]");
}
}
/**
* Exception handle template.
*
* @param callback the callback
* @param request the request
* @param response the response
*/
public <T extends AbstractTransactionRequest, S extends AbstractTransactionResponse> void exceptionHandleTemplate(Callback<T, S> callback, T request, S response) {
try {
callback.execute(request, response);
callback.onSuccess(request, response);
} catch (TransactionException tex) {
LOGGER.error("Catch TransactionException while do RPC, request: {}", request, tex);
callback.onTransactionException(request, response, tex);
} catch (RuntimeException rex) {
LOGGER.error("Catch RuntimeException while do RPC, request: {}", request, rex);
callback.onException(request, response, rex);
}
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.exception;
/**
* The type BranchTransaction exception.
*
* @author will
*/
public class BranchTransactionException extends TransactionException {
/**
* Instantiates a new Transaction exception.
*
* @param code the code
*/
public BranchTransactionException(TransactionExceptionCode code) {
super(code);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param cause the cause
*/
public BranchTransactionException(TransactionExceptionCode code, Throwable cause) {
super(code, cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param message the message
*/
public BranchTransactionException(String message) {
super(message);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param message the message
*/
public BranchTransactionException(TransactionExceptionCode code, String message) {
super(code, message);
}
/**
* Instantiates a new Transaction exception.
*
* @param cause the cause
*/
public BranchTransactionException(Throwable cause) {
super(cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param message the message
* @param cause the cause
*/
public BranchTransactionException(String message, Throwable cause) {
super(message, cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param message the message
* @param cause the cause
*/
public BranchTransactionException(TransactionExceptionCode code, String message, Throwable cause) {
super(code, message, cause);
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.exception;
/**
* The type GlobalTransaction exception.
*
* @author will
*/
public class GlobalTransactionException extends TransactionException {
/**
* Instantiates a new Transaction exception.
*
* @param code the code
*/
public GlobalTransactionException(TransactionExceptionCode code) {
super(code);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param cause the cause
*/
public GlobalTransactionException(TransactionExceptionCode code, Throwable cause) {
super(code, cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param message the message
*/
public GlobalTransactionException(String message) {
super(message);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param message the message
*/
public GlobalTransactionException(TransactionExceptionCode code, String message) {
super(code, message);
}
/**
* Instantiates a new Transaction exception.
*
* @param cause the cause
*/
public GlobalTransactionException(Throwable cause) {
super(cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param message the message
* @param cause the cause
*/
public GlobalTransactionException(String message, Throwable cause) {
super(message, cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param message the message
* @param cause the cause
*/
public GlobalTransactionException(TransactionExceptionCode code, String message, Throwable cause) {
super(code, message, cause);
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.exception;
/**
* The type RmTransaction exception.
*
* @author will
*/
public class RmTransactionException extends BranchTransactionException {
/**
* Instantiates a new Transaction exception.
*
* @param code the code
*/
public RmTransactionException(TransactionExceptionCode code) {
super(code);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param cause the cause
*/
public RmTransactionException(TransactionExceptionCode code, Throwable cause) {
super(code, cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param message the message
*/
public RmTransactionException(String message) {
super(message);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param message the message
*/
public RmTransactionException(TransactionExceptionCode code, String message) {
super(code, message);
}
/**
* Instantiates a new Transaction exception.
*
* @param cause the cause
*/
public RmTransactionException(Throwable cause) {
super(cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param message the message
* @param cause the cause
*/
public RmTransactionException(String message, Throwable cause) {
super(message, cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param message the message
* @param cause the cause
*/
public RmTransactionException(TransactionExceptionCode code, String message, Throwable cause) {
super(code, message, cause);
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.exception;
/**
* The type TmTransaction exception.
*
* @author will
*/
public class TmTransactionException extends GlobalTransactionException {
/**
* Instantiates a new Transaction exception.
*
* @param code the code
*/
public TmTransactionException(TransactionExceptionCode code) {
super(code);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param cause the cause
*/
public TmTransactionException(TransactionExceptionCode code, Throwable cause) {
super(code, cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param message the message
*/
public TmTransactionException(String message) {
super(message);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param message the message
*/
public TmTransactionException(TransactionExceptionCode code, String message) {
super(code, message);
}
/**
* Instantiates a new Transaction exception.
*
* @param cause the cause
*/
public TmTransactionException(Throwable cause) {
super(cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param message the message
* @param cause the cause
*/
public TmTransactionException(String message, Throwable cause) {
super(message, cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param message the message
* @param cause the cause
*/
public TmTransactionException(TransactionExceptionCode code, String message, Throwable cause) {
super(code, message, cause);
}
}

View File

@ -0,0 +1,109 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.exception;
/**
* The type Transaction exception.
*
* @author sharajava
*/
public class TransactionException extends Exception {
/**
* The Code.
*/
protected TransactionExceptionCode code = TransactionExceptionCode.Unknown;
/**
* Gets code.
*
* @return the code
*/
public TransactionExceptionCode getCode() {
return code;
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
*/
public TransactionException(TransactionExceptionCode code) {
this.code = code;
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param cause the cause
*/
public TransactionException(TransactionExceptionCode code, Throwable cause) {
super(cause);
this.code = code;
}
/**
* Instantiates a new Transaction exception.
*
* @param message the message
*/
public TransactionException(String message) {
super(message);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param message the message
*/
public TransactionException(TransactionExceptionCode code, String message) {
super(message);
this.code = code;
}
/**
* Instantiates a new Transaction exception.
*
* @param cause the cause
*/
public TransactionException(Throwable cause) {
super(cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param message the message
* @param cause the cause
*/
public TransactionException(String message, Throwable cause) {
super(message, cause);
}
/**
* Instantiates a new Transaction exception.
*
* @param code the code
* @param message the message
* @param cause the cause
*/
public TransactionException(TransactionExceptionCode code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}

View File

@ -0,0 +1,158 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.core.exception;
import java.util.HashMap;
import java.util.Map;
/**
* The enum Transaction exception code.
*
* @author sharajava
*/
public enum TransactionExceptionCode {
/**
* Unknown transaction exception code.
*/
Unknown,
/**
* BeginFailed
*/
BeginFailed,
/**
* Lock key conflict transaction exception code.
*/
LockKeyConflict,
/**
* Io transaction exception code.
*/
IO,
/**
* Branch rollback failed retriable transaction exception code.
*/
BranchRollbackFailed_Retriable,
/**
* Branch rollback failed unretriable transaction exception code.
*/
BranchRollbackFailed_Unretriable,
/**
* Branch register failed transaction exception code.
*/
BranchRegisterFailed,
/**
* Branch report failed transaction exception code.
*/
BranchReportFailed,
/**
* Lockable check failed transaction exception code.
*/
LockableCheckFailed,
/**
* Branch transaction not exist transaction exception code.
*/
BranchTransactionNotExist,
/**
* Global transaction not exist transaction exception code.
*/
GlobalTransactionNotExist,
/**
* Global transaction not active transaction exception code.
*/
GlobalTransactionNotActive,
/**
* Global transaction status invalid transaction exception code.
*/
GlobalTransactionStatusInvalid,
/**
* Failed to send branch commit request transaction exception code.
*/
FailedToSendBranchCommitRequest,
/**
* Failed to send branch rollback request transaction exception code.
*/
FailedToSendBranchRollbackRequest,
/**
* Failed to add branch transaction exception code.
*/
FailedToAddBranch,
/**
* Failed to lock global transaction exception code.
*/
FailedLockGlobalTranscation,
/**
* FailedWriteSession
*/
FailedWriteSession,
/**
* Failed to store exception code
*/
FailedStore
;
private static final Map<Integer, TransactionExceptionCode> MAP = new HashMap<>(values().length * 2);
static {
for (TransactionExceptionCode code : values()) {
MAP.put(code.ordinal(), code);
}
}
/**
* Get transaction exception code.
*
* @param ordinal the ordinal
* @return the transaction exception code
*/
public static TransactionExceptionCode get(byte ordinal) {
return get((int)ordinal);
}
/**
* Get transaction exception code.
*
* @param ordinal the ordinal
* @return the transaction exception code
*/
public static TransactionExceptionCode get(int ordinal) {
TransactionExceptionCode code = MAP.get(ordinal);
if (code == null) {
throw new IllegalArgumentException("Unknown TransactionExceptionCode[" + ordinal + "]");
}
return code;
}
}