001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.util;
021
022
023 import java.io.PrintStream;
024 import java.io.PrintWriter;
025
026
027 /**
028 * The base class of all errors which can contain other exceptions.
029 *
030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031 */
032 public class NestableError extends Error implements Nestable
033 {
034
035 static final long serialVersionUID = -9211839990752243375L;
036
037 /**
038 * The helper instance which contains much of the code which we delegate to.
039 */
040 protected NestableDelegate delegate = new NestableDelegate( this );
041
042 /**
043 * Holds the reference to the exception or error that caused this exception
044 * to be thrown.
045 */
046 private Throwable cause = null;
047
048
049 /**
050 * Constructs a new <code>NestableError</code> without specified detail
051 * message.
052 */
053 public NestableError()
054 {
055 super();
056 }
057
058
059 /**
060 * Constructs a new <code>NestableError</code> with specified detail
061 * message.
062 *
063 * @param msg
064 * The error message.
065 */
066 public NestableError(String msg)
067 {
068 super( msg );
069 }
070
071
072 /**
073 * Constructs a new <code>NestableError</code> with specified nested
074 * <code>Throwable</code>.
075 *
076 * @param cause
077 * the exception or error that caused this exception to be thrown
078 */
079 public NestableError(Throwable cause)
080 {
081 super();
082 this.cause = cause;
083 }
084
085
086 /**
087 * Constructs a new <code>NestableError</code> with specified detail
088 * message and nested <code>Throwable</code>.
089 *
090 * @param msg
091 * the error message
092 * @param cause
093 * the exception or error that caused this exception to be thrown
094 */
095 public NestableError(String msg, Throwable cause)
096 {
097 super( msg );
098 this.cause = cause;
099 }
100
101
102 public Throwable getCause()
103 {
104 return cause;
105 }
106
107
108 /**
109 * Returns the detail message string of this throwable. If it was created
110 * with a null message, returns the following: (cause==null ? null :
111 * cause.toString()).
112 */
113 public String getMessage()
114 {
115 if ( super.getMessage() != null )
116 {
117 return super.getMessage();
118 }
119 else if ( cause != null )
120 {
121 return cause.toString();
122 }
123 else
124 {
125 return null;
126 }
127 }
128
129
130 public String getMessage( int index )
131 {
132 if ( index == 0 )
133 {
134 return super.getMessage();
135 }
136 else
137 {
138 return delegate.getMessage( index );
139 }
140 }
141
142
143 public String[] getMessages()
144 {
145 return delegate.getMessages();
146 }
147
148
149 public Throwable getThrowable( int index )
150 {
151 return delegate.getThrowable( index );
152 }
153
154
155 public int getThrowableCount()
156 {
157 return delegate.getThrowableCount();
158 }
159
160
161 public Throwable[] getThrowables()
162 {
163 return delegate.getThrowables();
164 }
165
166
167 public int indexOfThrowable( Class type )
168 {
169 return delegate.indexOfThrowable( type, 0 );
170 }
171
172
173 public int indexOfThrowable( Class type, int fromIndex )
174 {
175 return delegate.indexOfThrowable( type, fromIndex );
176 }
177
178
179 public void printStackTrace()
180 {
181 delegate.printStackTrace();
182 }
183
184
185 public void printStackTrace( PrintStream out )
186 {
187 delegate.printStackTrace( out );
188 }
189
190
191 public void printStackTrace( PrintWriter out )
192 {
193 delegate.printStackTrace( out );
194 }
195
196
197 public final void printPartialStackTrace( PrintWriter out )
198 {
199 super.printStackTrace( out );
200 }
201 }