Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.javaclub.cdl.client.util.ExceptionUtils Maven / Gradle / Ivy
package com.github.javaclub.cdl.client.util;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.javaclub.cdl.client.parameter.ParameterContext;
public class ExceptionUtils {
private static final Logger log = LoggerFactory.getLogger(ExceptionUtils.class);
public static StackTraceElement split = new StackTraceElement("------- one sql exceptions-----", "", "", 0);
public static final String SQL_EXECUTION_ERROR_CONTEXT_LOG = "SQL_EXECUTION_ERROR_CONTEXT_LOG";
private static final String SQL_EXECUTION_ERROR_CONTEXT_MESSAGE = "SQLException ,context is ";
public static void throwSQLException(List exceptions, String sql, List args) throws SQLException {
if (exceptions != null && !exceptions.isEmpty()) {
SQLException first = exceptions.get(0);
if (sql != null) {
log.info(("CDL SQL EXECUTE ERROR REPORTER:" + getErrorContext(sql, args, SQL_EXECUTION_ERROR_CONTEXT_MESSAGE)), first);
}
for (int i = 1, n = exceptions.size(); i < n; i++) {
if (sql != null) {
log.info(
("layer:" + n + "CDL SQL EXECUTE ERROR REPORTER :" + getErrorContext(sql, args,
SQL_EXECUTION_ERROR_CONTEXT_MESSAGE)), exceptions.get(i));
}
}
throw mergeException(exceptions);
}
}
public static List appendToExceptionList(List list, SQLException sqlException) {
if (list == null) {
list = new LinkedList();
}
list.add(sqlException);
return list;
}
public static SQLException mergeException(List exceptions) {
// return new OneToManySQLExceptionsWrapper(exceptions);
SQLException first = exceptions.get(0);
List stes = new ArrayList(30 * exceptions.size());
// stes.addAll(Arrays.asList(first.getStackTrace()));
boolean hasSplit = false;
for (StackTraceElement ste : first.getStackTrace()) {
stes.add(ste);
if (ste == split) {
hasSplit = true;
}
}
if (!hasSplit) {
stes.add(split);
}
SQLException current = null;
for (int i = 1, n = exceptions.size(); i < n; i++) {
// newEx.setNextException(exceptions.get(i));
// current.setNextException(exceptions.get(i));
current = exceptions.get(i);
// stes.addAll(Arrays.asList(exceptions.get(i).getStackTrace()));
hasSplit = false;
for (StackTraceElement ste : current.getStackTrace()) {
stes.add(ste);
if (ste == split) {
hasSplit = true;
}
}
if (!hasSplit) {
stes.add(split);
}
}
// newEx.getCause();
first.setStackTrace(stes.toArray(new StackTraceElement[stes.size()]));
return first;
}
public static void throwSQLException(SQLException exception, String sql, List args) throws SQLException {
if (sql != null) {
log.info(("CDL SQL EXECUTE ERROR REPORTER:" + getErrorContext(sql, args, SQL_EXECUTION_ERROR_CONTEXT_MESSAGE))
+ "nest Exceptions is " + exception.getMessage(), exception);
}
throw exception;
}
public static String getErrorContext(String sql, List arguments, String message) {
StringBuilder sb = new StringBuilder();
sb.append(message).append(sql).append("|||arguments:");
printArgument(arguments, sb);
return sb.toString();
}
private static void printArgument(List parameters, StringBuilder sb) {
int i = 0;
if (parameters != null) {
for (Object param : parameters) {
sb.append("[index:").append(i).append("|parameter:").append(param).append("|typeclass:")
.append(param == null ? null : param.getClass().getName()).append("]");
i++;
}
} else {
sb.append("[empty]");
}
}
public static void throwSQLException(List exceptions, String sql, Map parameter)
throws SQLException {
if (exceptions != null && !exceptions.isEmpty()) {
SQLException first = exceptions.get(0);
if (sql != null) {
log.info(("CDL SQL EXECUTE ERROR REPORTER:" + getErrorContext(sql, parameter, SQL_EXECUTION_ERROR_CONTEXT_MESSAGE)), first);
}
for (int i = 1, n = exceptions.size(); i < n; i++) {
if (sql != null) {
log.info(
("layer:" + n + "CDL SQL EXECUTE ERROR REPORTER :" + getErrorContext(sql, parameter,
SQL_EXECUTION_ERROR_CONTEXT_MESSAGE)), exceptions.get(i));
}
}
throw mergeException(exceptions);
}
}
public static void throwSQLException(SQLException exception, String sql, Map parameter) throws SQLException {
if (sql != null) {
log.info(("CDL SQL EXECUTE ERROR REPORTER:" + getErrorContext(sql, parameter, SQL_EXECUTION_ERROR_CONTEXT_MESSAGE))
+ "nest Exceptions is " + exception.getMessage(), exception);
}
throw exception;
}
public static String getErrorContext(String sql, Map parameter, String message) {
StringBuilder sb = new StringBuilder();
sb.append(message).append(sql).append("|||arguments:");
printArgument(parameter, sb);
return sb.toString();
}
private static void printArgument(Map parameter, StringBuilder sb) {
int i = 0;
if (parameter != null) {
for (Object param : parameter.entrySet()) {
sb.append("[index:").append(i).append("|parameter:").append(param).append("|typeclass:")
.append(param == null ? null : param.getClass().getName()).append("]");
i++;
}
} else {
sb.append("[empty]");
}
}
/**
*
* @param logger
* @param message
* @param sqlExceptions
*/
public static void printSQLExceptionToErrorLog(Logger logger, String message, List sqlExceptions) {
if (sqlExceptions != null && !sqlExceptions.isEmpty()) {
for (SQLException sqlException : sqlExceptions) {
logger.error(message, sqlException);
}
sqlExceptions.clear();
}
}
}