/*
 * Copyright 2014, Timo Westkämper
 *
 * 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 com.mysema.query.sql.support;

import java.sql.SQLException;
import java.util.ArrayList;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.mysema.query.QueryException;

/**
 * A {@link SQLExceptionWrapper} that wraps the additional exception
 * information in a {@code WrappedSQLCauseException} and links it to the chain.
 *
 * @author Shredder121
 */
class JavaSE6SQLExceptionWrapper extends SQLExceptionWrapper {

    @Override
    public RuntimeException wrap(SQLException exception) {
        Iterable<Throwable> linkedSQLExceptions = getLinkedSQLExceptions(exception);
        return new QueryException(
                new WrappedSQLCauseException(linkedSQLExceptions, exception));
    }

    @Override
    public RuntimeException wrap(String message, SQLException exception) {
        Iterable<Throwable> linkedSQLExceptions = getLinkedSQLExceptions(exception);
        return new QueryException(message,
                new WrappedSQLCauseException(linkedSQLExceptions, exception));
    }

    private static Iterable<Throwable> getLinkedSQLExceptions(SQLException exception) {
        ArrayList<Throwable> rv = Lists.newArrayList();
        SQLException nextException = exception.getNextException();
        while (nextException != null) {
            rv.add(nextException);
            nextException = nextException.getNextException();
        }
        return rv;
    }

    private static class WrappedSQLCauseException extends Exception {

        private static final long serialVersionUID = 1L;

        private WrappedSQLCauseException(Iterable<Throwable> exceptions, SQLException exception) {
            super("Detailed SQLException information:\n" + Iterables
                    .transform(exceptions, exceptionMessageFunction), exception);
        }
    }
    private static final Function<Throwable, String> exceptionMessageFunction = new Function<Throwable, String>() {
        @Override
        public String apply(Throwable input) {
            if (input instanceof SQLException) {
                SQLException sqle = (SQLException) input;
                return new StringBuilder()
                        .append("SQLState: ").append(sqle.getSQLState())
                        .append(", ")
                        .append("ErrorCode: ").append(sqle.getErrorCode())
                        .append(", ")
                        .append("Message: ").append(sqle.getMessage())
                        .toString();
            }
            return input.toString();
        }
    };

}

@@ -618,24 +618,158 @@
 l.support;%0A%0A
+import static com.google.common.base.StandardSystemProperty.LINE_SEPARATOR;%0A%0Aimport java.io.PrintWriter;%0Aimport java.io.StringWriter;%0A
 import java.
@@ -851,24 +851,62 @@
 e.Function;%0A
+import com.google.common.base.Joiner;%0A
 import com.g
@@ -2516,14 +2516,73 @@
 ion:
-%5Cn
 %22 + 
+LINE_SEPARATOR.value()%0A                    + lineJoiner.join(
 Iter
@@ -2587,16 +2587,24 @@
 erables%0A
+        
         
@@ -2663,16 +2663,17 @@
 unction)
+)
 , except
@@ -2689,24 +2689,105 @@
      %7D%0A    %7D
+%0A%0A    private static final Joiner lineJoiner = Joiner.on(LINE_SEPARATOR.value());
 %0A    private
@@ -3083,144 +3083,164 @@
     
-return new StringBuilder()%0A                        .append(%22SQLState: %22).append(sqle.getSQLState())%0A                        .append(%22, %22
+StringWriter writer = new StringWriter();%0A                new PrintWriter(writer, true)%0A                        .printf(%22SQLState: %25s%25n%22, sqle.getSQLState()
 )%0A  
@@ -3262,22 +3262,22 @@
        .
-append
+printf
 (%22ErrorC
@@ -3281,26 +3281,23 @@
 orCode: 
-%22).append(
+%25s%25n%22, 
 sqle.get
@@ -3338,52 +3338,14 @@
    .
-append(%22, %22)%0A                        .append
+printf
 (%22Me
@@ -3355,18 +3355,15 @@
 ge: 
-%22).append(
+%25s%25n%22, 
 sqle
@@ -3372,24 +3372,25 @@
 etMessage())
+;
 %0A           
@@ -3394,24 +3394,29 @@
         
-        
+return writer
 .toStrin

Separate the individual exceptions with a blank line