1 //2 // ========================================================================3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.4 // ------------------------------------------------------------------------5 // All rights reserved. This program and the accompanying materials6 // are made available under the terms of the Eclipse Public License v1.07 // and Apache License v2.0 which accompanies this distribution.8 //9 // The Eclipse Public License is available at10 // http://www.eclipse.org/legal/epl-v10.html11 //12 // The Apache License v2.0 is available at13 // http://www.opensource.org/licenses/apache2.0.php14 //15 // You may elect to redistribute this code under either of these licenses.16 // ========================================================================17 //18 19 modulehunt.http.codec.http.model.MultiException;
20 21 importhunt.collection;
22 23 importhunt.Exceptions;
24 importhunt.text.Common;
25 importhunt.util.StringBuilder;
26 27 /**
28 * Wraps multiple exceptions.
29 *
30 * Allows multiple exceptions to be thrown as a single exception.
31 */32 // 33 classMultiException : Exception34 {
35 privateList!Exceptionnested;
36 37 /* ------------------------------------------------------------ */38 this()
39 {
40 super("Multiple exceptions");
41 }
42 43 /* ------------------------------------------------------------ */44 voidadd(Exceptione)
45 {
46 if (eisnull)
47 thrownewIllegalArgumentException("");
48 49 if(nestedisnull)
50 {
51 // initCause(e);52 nested = newArrayList!Exception();
53 }
54 // else55 // addSuppressed(e);56 57 if (typeid(e) == typeid(MultiException))
58 {
59 MultiExceptionme = cast(MultiException)e;
60 // nested.addAll(me.nested);61 }
62 else63 nested.add(e);
64 }
65 66 /* ------------------------------------------------------------ */67 intsize()
68 {
69 return (nestedisnull)?0:nested.size();
70 }
71 72 /* ------------------------------------------------------------ */73 List!ExceptiongetThrowables()
74 {
75 if(nestedisnull)
76 returnnewEmptyList!Exception(); // Collections.emptyList();77 returnnested;
78 }
79 80 /* ------------------------------------------------------------ */81 ExceptiongetThrowable(inti)
82 {
83 returnnested.get(i);
84 }
85 86 /* ------------------------------------------------------------ */87 /** Throw a multiexception.
88 * If this multi exception is empty then no action is taken. If it
89 * contains a single exception that is thrown, otherwise the this
90 * multi exception is thrown.
91 * @exception Exception the Error or Exception if nested is 1, or the MultiException itself if nested is more than 1.
92 */93 voidifExceptionThrow()
94 {
95 if(nestedisnull)
96 return;
97 98 switch (nested.size())
99 {
100 case0:
101 break;
102 case1:
103 Exceptionth=nested.get(0);
104 if (typeid(th) == typeid(Error))
105 throwcast(Error)th;
106 if (typeid(th) == typeid(Exception))
107 throwcast(Exception)th;
108 break;
109 110 default:
111 throwthis;
112 }
113 }
114 115 /* ------------------------------------------------------------ */116 /** Throw a Runtime exception.
117 * If this multi exception is empty then no action is taken. If it
118 * contains a single error or runtime exception that is thrown, otherwise the this
119 * multi exception is thrown, wrapped in a runtime onException.
120 * @exception Error If this exception contains exactly 1 {@link Error}
121 * @exception RuntimeException If this exception contains 1 {@link Exception} but it is not an error,
122 * or it contains more than 1 {@link Exception} of any type.
123 */124 voidifExceptionThrowRuntime()
125 {
126 if(nestedisnull)
127 return;
128 129 switch (nested.size())
130 {
131 case0:
132 break;
133 case1:
134 Exceptionth = nested.get(0);
135 Errorerr = cast(Error)th;
136 if (err !isnull)
137 throwerr;
138 139 RuntimeExceptionrex = cast(RuntimeException)th;
140 if (rex !isnull)
141 throwrex;
142 143 thrownewRuntimeException(th);
144 145 default:
146 thrownewRuntimeException(this);
147 }
148 }
149 150 /* ------------------------------------------------------------ */151 /** Throw a multiexception.
152 * If this multi exception is empty then no action is taken. If it
153 * contains a any exceptions then this
154 * multi exception is thrown.
155 * @throws MultiException the multiexception if there are nested exception
156 */157 voidifExceptionThrowMulti()
158 {
159 if(nestedisnull)
160 return;
161 162 if (nested.size()>0)
163 throwthis;
164 }
165 166 /* ------------------------------------------------------------ */167 override168 stringtoString()
169 {
170 StringBuilderstr = newStringBuilder();
171 str.append(MultiException.stringof);
172 if((nestedisnull) || (nested.size()<=0)) {
173 str.append("[]");
174 } else {
175 str.append(nested.toString());
176 }
177 returnstr.toString();
178 }
179 180 }