diff --git a/uboot-common/pom.xml b/uboot-common/pom.xml index b10a2dc..31ddff0 100644 --- a/uboot-common/pom.xml +++ b/uboot-common/pom.xml @@ -53,6 +53,7 @@ com.chinaunicom.ebtp mall-ebtp-cloud-seata-starter + true diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdviceDefault.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdviceDefault.java index 5a2f137..3e49e62 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdviceDefault.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdviceDefault.java @@ -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 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(); diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdvicePro.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdvicePro.java index ad8f253..6c65027 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdvicePro.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdvicePro.java @@ -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 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()); } diff --git a/uboot-common/src/main/java/io/seata/core/exception/AbstractExceptionHandler.java b/uboot-common/src/main/java/io/seata/core/exception/AbstractExceptionHandler.java new file mode 100644 index 0000000..34033cc --- /dev/null +++ b/uboot-common/src/main/java/io/seata/core/exception/AbstractExceptionHandler.java @@ -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 the type parameter + * @param the type parameter + */ + public interface Callback { + /** + * 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 + implements Callback { + + @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 void exceptionHandleTemplate(Callback 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); + } + } + +} diff --git a/uboot-common/src/main/java/io/seata/core/exception/BranchTransactionException.java b/uboot-common/src/main/java/io/seata/core/exception/BranchTransactionException.java new file mode 100644 index 0000000..49e6d30 --- /dev/null +++ b/uboot-common/src/main/java/io/seata/core/exception/BranchTransactionException.java @@ -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); + } +} diff --git a/uboot-common/src/main/java/io/seata/core/exception/GlobalTransactionException.java b/uboot-common/src/main/java/io/seata/core/exception/GlobalTransactionException.java new file mode 100644 index 0000000..d0ba2ba --- /dev/null +++ b/uboot-common/src/main/java/io/seata/core/exception/GlobalTransactionException.java @@ -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); + } +} diff --git a/uboot-common/src/main/java/io/seata/core/exception/RmTransactionException.java b/uboot-common/src/main/java/io/seata/core/exception/RmTransactionException.java new file mode 100644 index 0000000..2e5ee22 --- /dev/null +++ b/uboot-common/src/main/java/io/seata/core/exception/RmTransactionException.java @@ -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); + } +} diff --git a/uboot-common/src/main/java/io/seata/core/exception/TmTransactionException.java b/uboot-common/src/main/java/io/seata/core/exception/TmTransactionException.java new file mode 100644 index 0000000..35deb9f --- /dev/null +++ b/uboot-common/src/main/java/io/seata/core/exception/TmTransactionException.java @@ -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); + } +} diff --git a/uboot-common/src/main/java/io/seata/core/exception/TransactionException.java b/uboot-common/src/main/java/io/seata/core/exception/TransactionException.java new file mode 100644 index 0000000..d094086 --- /dev/null +++ b/uboot-common/src/main/java/io/seata/core/exception/TransactionException.java @@ -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; + } +} diff --git a/uboot-common/src/main/java/io/seata/core/exception/TransactionExceptionCode.java b/uboot-common/src/main/java/io/seata/core/exception/TransactionExceptionCode.java new file mode 100644 index 0000000..98a27dd --- /dev/null +++ b/uboot-common/src/main/java/io/seata/core/exception/TransactionExceptionCode.java @@ -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 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; + } + +}