Example usage for java.sql SQLException getNextException

List of usage examples for java.sql SQLException getNextException

Introduction

In this page you can find the example usage for java.sql SQLException getNextException.

Prototype

public SQLException getNextException() 

Source Link

Document

Retrieves the exception chained to this SQLException object by setNextException(SQLException ex).

Usage

From source file:hoot.services.controllers.review.ReviewResource.java

/**
 * Handles all thrown exceptions from review services
 *
 * @param e/*  w w w  .  j  ava2s . c o  m*/
 *            a thrown exception
 * @param errorMessageStart
 *            text to prepend to the error message
 * //TODO: go through and clean out these message text checks
 */
private static void handleError(Exception e, String errorMessageStart) {
    Status status = null;
    if (!StringUtils.isEmpty(e.getMessage())) {
        if (e.getMessage().contains("Invalid input parameter")
                || e.getMessage().contains("Invalid reviewed item")
                || e.getMessage().contains("Error parsing unique ID tag")
                || e.getMessage().contains("empty String") || e.getMessage().contains("Invalid coordinate")) {
            status = Status.BAD_REQUEST;
        } else if (e.getMessage().contains("record exists") || e.getMessage().contains("records exist")
                || e.getMessage().contains("to be updated does not exist")
                || e.getMessage().contains("does not exist")) {
            status = Status.NOT_FOUND;
        } else if (e.getMessage().contains("Invalid version") || e.getMessage().contains("Invalid changeset ID")
                || e.getMessage().contains("references itself")
                || e.getMessage().contains("Changeset maximum element threshold exceeded")
                || e.getMessage().contains("was closed at")
                || e.getMessage().contains("has become out of sync")) {
            status = Status.CONFLICT;
        } else if (e.getMessage().contains("exist specified for") || e.getMessage().contains("exist for")
                || e.getMessage().contains("is still used by")) {
            status = Status.PRECONDITION_FAILED;
        }
    }

    if (status == null) {
        status = Status.INTERNAL_SERVER_ERROR;
    }

    String message = "Error " + errorMessageStart + ": ";
    if ((e.getMessage() != null) && e.getMessage().contains("empty String")) {
        // added for giving a better error message when passing invalid params to jersey
        message += "Invalid input parameter";
    } else {
        message += e.getMessage();
    }

    if (e instanceof SQLException) {
        SQLException sqlException = (SQLException) e;
        if (sqlException.getNextException() != null) {
            message += "  " + sqlException.getNextException().getMessage();
        }
    }

    if (e.getCause() instanceof SQLException) {
        SQLException sqlException = (SQLException) e.getCause();
        if (sqlException.getNextException() != null) {
            message += "  " + sqlException.getNextException().getMessage();
        }
    }

    String exceptionCode = status.getStatusCode() + ": " + status.getReasonPhrase();
    logger.error("{} {}", exceptionCode, message, e);

    throw new WebApplicationException(e, Response.status(status).entity(message).build());
}

From source file:org.sipfoundry.sipxconfig.test.TestHelper.java

public static void cleanInsert(String resource) throws Exception {
    try {//from   ww w.  j av a  2  s  .c om
        DatabaseOperation.CLEAN_INSERT.execute(getConnection(), loadDataSet(resource));
    } catch (SQLException e) {
        throw e.getNextException();
    }
}

From source file:com.oracle.tutorial.jdbc.JDBCTutorialUtilities.java

public static void alternatePrintSQLException(SQLException ex) {
    while (ex != null) {
        System.err.println("SQLState: " + ex.getSQLState());
        System.err.println("Error Code: " + ex.getErrorCode());
        System.err.println("Message: " + ex.getMessage());
        Throwable t = ex.getCause();
        while (t != null) {
            System.out.println("Cause: " + t);
            t = t.getCause();// w w w .  ja  va 2  s. co m
        }
        ex = ex.getNextException();
    }
}

From source file:TaskManager.java

public static String readException(SQLException sqlX) {
    StringBuffer msg = new StringBuffer(1024);
    SQLException nextX;//from  w w  w.j  av a 2  s. c  o  m
    int exceptionNumber = 0;

    do {
        ++exceptionNumber;

        msg.append("Exception " + exceptionNumber + ": \n");
        msg.append("  Message: " + sqlX.getMessage() + "\n");
        msg.append("  State  : " + sqlX.getSQLState() + "\n");
        msg.append("  Code   : " + sqlX.getErrorCode() + "\n");
    } while ((nextX = sqlX.getNextException()) != null);

    return (msg.toString());
}

From source file:org.seasar.dbflute.logic.DfDBFluteTaskUtil.java

protected static void buildSQLExceptionMessage(ExceptionMessageBuilder br, SQLException e) {
    final String sqlState = DfJDBCException.extractSQLState(e);
    br.addItem("SQLState");
    br.addElement(sqlState);/*from  w w w . j av  a  2s .c  o  m*/
    final Integer errorCode = DfJDBCException.extractErrorCode(e);
    br.addItem("ErrorCode");
    br.addElement(errorCode);
    br.addItem("SQLException");
    br.addElement(e.getClass().getName());
    if (e instanceof DfJDBCException) {
        br.addElement("*Look at the message on the stack trace");
    } else {
        br.addElement(DfJDBCException.extractMessage(e));
    }
    final SQLException nextEx = e.getNextException();
    if (nextEx != null) {
        br.addItem("NextException");
        br.addElement(nextEx.getClass().getName());
        br.addElement(DfJDBCException.extractMessage(nextEx));
        final SQLException nextNextEx = nextEx.getNextException();
        if (nextNextEx != null) {
            br.addItem("NextNextException");
            br.addElement(nextNextEx.getClass().getName());
            br.addElement(DfJDBCException.extractMessage(nextNextEx));
        }
    }
}

From source file:com.xqdev.sql.MLSQL.java

private static void addExceptions(Element meta, SQLException e) {
    if (e == null)
        return;//from w  ww.j av  a 2 s  .c  o m

    Namespace sql = meta.getNamespace();
    Element exceptions = new Element("exceptions", sql);
    meta.addContent(exceptions);
    do {
        exceptions.addContent(new Element("exception", sql).setAttribute("type", e.getClass().getName())
                .addContent(new Element("reason", sql).setText(e.getMessage()))
                .addContent(new Element("sql-state", sql).setText(e.getSQLState()))
                .addContent(new Element("vendor-code", sql).setText("" + e.getErrorCode())));
        e = e.getNextException();
    } while (e != null);
}

From source file:uk.ac.kcl.texthunter.utils.Utils.java

public static void printSQLException(SQLException e) {
    // Unwraps the entire exception chain to unveil the real cause of the
    // Exception.
    while (e != null) {
        System.err.println("\n----- SQLException -----");
        System.err.println("  SQL State:  " + e.getSQLState());
        System.err.println("  Error Code: " + e.getErrorCode());
        System.err.println("  Message:    " + e.getMessage());
        // for stack traces, refer to derby.log or uncomment this:
        //e.printStackTrace(System.err);
        e = e.getNextException();
    }//from   www .  j a  v a2s .c  o  m
}

From source file:sample.jooq.JooqExceptionTranslator.java

/**
 * Handle a single exception in the chain. SQLExceptions might be nested multiple
 * levels deep. The outermost exception is usually the least interesting one
 * ("Call getNextException to see the cause."). Therefore the innermost exception is
 * propagated and all other exceptions are logged.
 * @param context the execute context//  w ww.  j  av a2 s  .co m
 * @param translator the exception translator
 * @param exception the exception
 */
private void handle(ExecuteContext context, SQLExceptionTranslator translator, SQLException exception) {
    DataAccessException translated = translate(context, translator, exception);
    if (exception.getNextException() == null) {
        context.exception(translated);
    } else {
        logger.error("Execution of SQL statement failed.", translated);
    }
}

From source file:com.epam.catgenome.controller.ExceptionHandlerAdvice.java

@ResponseBody
@Order(Ordered.HIGHEST_PRECEDENCE)//from   w  w  w  . j  a va  2  s .  c  o m
@ExceptionHandler(Throwable.class)
public final ResponseEntity<Result<String>> handleUncaughtException(final Throwable exception,
        final WebRequest request) {
    // adds information about encountered error to application log
    LOG.error(MessageHelper.getMessage("logger.error", request.getDescription(true)), exception);
    HttpStatus code = HttpStatus.OK;

    String message;
    if (exception instanceof FileNotFoundException) {
        // any details about real path of a resource should be normally prevented to send to the client
        message = MessageHelper.getMessage("error.io.not.found");
    } else if (exception instanceof DataAccessException) {
        // any details about data access error should be normally prevented to send to the client,
        // as its message can contain information about failed SQL query or/and database schema
        if (exception instanceof BadSqlGrammarException) {
            // for convenience we need to provide detailed information about occurred BadSqlGrammarException,
            // but it can be retrieved
            SQLException root = ((BadSqlGrammarException) exception).getSQLException();
            if (root.getNextException() != null) {
                LOG.error(MessageHelper.getMessage("logger.error.root.cause", request.getDescription(true)),
                        root.getNextException());
            }
            message = MessageHelper.getMessage("error.sql.bad.grammar");
        } else {
            message = MessageHelper.getMessage("error.sql");
        }
    } else if (exception instanceof UnauthorizedClientException) {
        message = exception.getMessage();
        code = HttpStatus.UNAUTHORIZED;
    } else {
        message = exception.getMessage();
    }

    return new ResponseEntity<>(Result.error(StringUtils.defaultString(StringUtils.trimToNull(message),
            MessageHelper.getMessage("error" + ".default"))), code);
}

From source file:sample.jooq.JooqExceptionTranslator.java

@Override
public void exception(ExecuteContext context) {
    SQLExceptionTranslator translator = getTranslator(context);
    // The exception() callback is not only triggered for SQL exceptions but also for
    // "normal" exceptions. In those cases sqlException() returns null.
    SQLException exception = context.sqlException();
    while (exception != null) {
        handle(context, translator, exception);
        exception = exception.getNextException();
    }//from   ww  w. j  a va  2s .c  o m
}