001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella.execution;
017:
018: import scriptella.configuration.Location;
019: import scriptella.core.EtlCancelledException;
020: import scriptella.core.ExceptionInterceptor;
021: import scriptella.expression.Expression;
022: import scriptella.spi.ProviderException;
023: import scriptella.util.StringUtils;
024:
025: import java.io.PrintWriter;
026: import java.io.StringWriter;
027:
028: /**
029: * Thrown on script execution failure
030: *
031: * @author Fyodor Kupolov
032: * @version 1.0
033: */
034: public class EtlExecutorException extends Exception {
035: private ProviderException lastProvider;
036: private Throwable lastExpression;
037: private Location lastElementLocation;
038: private String message;
039: private boolean cancelled;
040:
041: public EtlExecutorException(Throwable cause) {
042: super (cause);
043:
044: for (Throwable ex = cause; ex != null; ex = ex.getCause()) {
045: if (isExpression(ex)) {
046: lastExpression = ex;
047: }
048:
049: if (ex instanceof ProviderException) {
050: ProviderException providerEx = (ProviderException) ex;
051: lastProvider = providerEx;
052: }
053:
054: if (ex instanceof ExceptionInterceptor.ExecutionException) {
055: lastElementLocation = ((ExceptionInterceptor.ExecutionException) ex)
056: .getLocation();
057: }
058:
059: if (ex instanceof EtlCancelledException
060: || ex instanceof InterruptedException) {
061: cancelled = true;
062: }
063: }
064:
065: final StringWriter out = new StringWriter();
066: PrintWriter pw = new PrintWriter(out); //Use print writer to handle line separators
067: if ((lastProvider == null || lastElementLocation == null)
068: && cause != null && cause.getMessage() != null) {
069: pw.println(cause.getMessage());
070: }
071: if (lastElementLocation != null) {
072: pw.print("Location: ");
073: pw.println(lastElementLocation);
074: }
075: if (lastProvider != null) {
076: pw.print(lastProvider.getProviderName()
077: + " provider exception: ");
078: pw.println(lastProvider.getMessage());
079:
080: if (lastProvider.getErrorStatement() != null) {
081: pw.print("Error statement: ");
082: pw.print(lastProvider.getErrorStatement());
083: pw.println();
084: }
085: pw.println("Error codes: " + lastProvider.getErrorCodes());
086: Throwable nativeException = lastProvider
087: .getNativeException();
088: if (nativeException != null) {
089: pw.print("Driver exception: ");
090: pw.print(nativeException.toString());
091: pw.println();
092: }
093: }
094: if (lastExpression != null) {
095: pw.print("Expression exception: ");
096: pw.print(lastExpression.getMessage());
097: pw.println();
098: }
099: this .message = StringUtils.consoleFormat(out.toString());
100: }
101:
102: public String getMessage() {
103: return message;
104: }
105:
106: public ProviderException getLastProvider() {
107: return lastProvider;
108: }
109:
110: public Throwable getLastExpression() {
111: return lastExpression;
112: }
113:
114: public Location getLastElementLocation() {
115: return lastElementLocation;
116: }
117:
118: public boolean isCancelled() {
119: return cancelled;
120: }
121:
122: private static boolean isExpression(final Throwable cause) {
123: return cause instanceof Expression.ParseException
124: || cause instanceof Expression.EvaluationException;
125: }
126:
127: }
|