io.seata.core.exception.AbstractExceptionHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of seata-core Show documentation
Show all versions of seata-core Show documentation
core for Seata built with Maven
/*
* 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;
/**
* The type Abstract exception handler.
*
* @author sharajava
*/
public abstract class AbstractExceptionHandler {
/**
* 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 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, AbstractTransactionRequest request,
AbstractTransactionResponse response) {
try {
callback.execute(request, response);
callback.onSuccess(request, response);
} catch (TransactionException tex) {
callback.onTransactionException(request, response, tex);
} catch (RuntimeException rex) {
callback.onException(request, response, rex);
}
}
}