1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.Exceptions;
13 
14 import core.exception;
15 import std.exception;
16 
17 void implementationMissing(string name = __FUNCTION__, string file = __FILE__, int line = __LINE__)(
18         bool canThrow = true) {
19     if (canThrow)
20         throw new Exception("Implementation missing: " ~ name, file, line);
21     else {
22         version (HUNT_DEBUG) {
23             import hunt.logging;
24 
25             warningf("Implementation missing %s, in %s:%d", name, file, line);
26         }
27         else {
28             import std.stdio;
29 
30             stderr.writefln("======> Implementation missing %s, in %s:%d", name, file, line);
31         }
32     }
33 }
34 
35 mixin template ExceptionBuild(string name, string parent = "") {
36     import std.exception;
37 
38     enum buildStr = "class " ~ name ~ "Exception : " ~ parent ~ "Exception { \n\t"
39         ~ "mixin basicExceptionCtors;\n }";
40     mixin(buildStr);
41 }
42 
43 mixin template BasicExceptionCtors() {
44     this(size_t line = __LINE__, string file = __FILE__) @nogc @safe pure nothrow {
45         super("", file, line, null);
46     }
47 
48     this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) @nogc @safe pure nothrow {
49         super(msg, file, line, next);
50     }
51 
52     this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__) @nogc @safe pure nothrow {
53         super(msg, file, line, next);
54     }
55 
56     this(Throwable next, string file = __FILE__, size_t line = __LINE__) @nogc @safe pure nothrow {
57         super(next.msg, file, line, next);
58     }
59 
60     // mixin basicExceptionCtors;
61 }
62 
63 class NotImplementedException : Exception {
64     mixin BasicExceptionCtors;
65 }
66 
67 class NotSupportedException : Exception {
68     mixin BasicExceptionCtors;
69 }
70 
71 class IllegalArgumentException : Exception {
72     mixin BasicExceptionCtors;
73 }
74 
75 class RuntimeException : Exception {
76     this(Exception ex) {
77         super("", ex);
78     }
79 
80     /++
81         Params:
82             msg  = The message for the exception.
83             file = The file where the exception occurred.
84             line = The line number where the exception occurred.
85             next = The previous exception in the chain of exceptions, if any.
86     +/
87     this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) @nogc @safe pure nothrow {
88         super(msg, file, line, next);
89     }
90 
91     /++
92         Params:
93             msg  = The message for the exception.
94             next = The previous exception in the chain of exceptions.
95             file = The file where the exception occurred.
96             line = The line number where the exception occurred.
97     +/
98     this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__) @nogc @safe pure nothrow {
99         super(msg, file, line, next);
100     }
101     // mixin BasicExceptionCtors;
102 }
103 
104 class ExecutionException : Exception {
105     mixin BasicExceptionCtors;
106 }
107 
108 class InterruptedException : Exception {
109     mixin BasicExceptionCtors;
110 }
111 
112 class ParseException : Exception {
113     // mixin BasicExceptionCtors;
114 
115     /**
116      * Constructs a ParseException with the specified detail message and
117      * offset.
118      * A detail message is a String that describes this particular exception.
119      *
120      * @param s the detail message
121      * @param errorOffset the position where the error is found while parsing.
122      */
123     public this(string s, int errorOffset=-1) {
124         super(s);
125         this.errorOffset = errorOffset;
126     }
127 
128     /**
129      * Returns the position where the error was found.
130      *
131      * @return the position where the error was found
132      */
133     public int getErrorOffset () {
134         return errorOffset;
135     }
136 
137     //============ privates ============
138     /**
139      * The zero-based character offset into the string being parsed at which
140      * the error was found during parsing.
141      * @serial
142      */
143     private int errorOffset;
144 }
145 
146 class TimeoutException : Exception {
147     mixin BasicExceptionCtors;
148 }
149 
150 class FileNotFoundException : Exception {
151     mixin BasicExceptionCtors;
152 }
153 
154 class IOException : Exception {
155     mixin BasicExceptionCtors;
156 }
157 
158 class ClosedChannelException : IOException {
159     mixin BasicExceptionCtors;
160 }
161 
162 class EOFException : IOException {
163     mixin BasicExceptionCtors;
164 }
165 
166 class MalformedURLException : IOException {
167     mixin BasicExceptionCtors;
168 }
169 
170 class InterruptedIOException : IOException {
171     mixin BasicExceptionCtors;
172 }
173 
174 class RemoteException : IOException {
175     mixin BasicExceptionCtors;
176 }
177 
178 class URISyntaxException : IOException {
179     mixin BasicExceptionCtors;
180 }
181 
182 class AsynchronousCloseException : ClosedChannelException {
183     mixin BasicExceptionCtors;
184 }
185 
186 class CommonRuntimeException : RuntimeException {
187     mixin BasicExceptionCtors;
188 }
189 
190 class IndexOutOfBoundsException : RuntimeException {
191     mixin BasicExceptionCtors;
192 }
193 
194 class NegativeArraySizeException : RuntimeException {
195     mixin BasicExceptionCtors;
196 }
197 
198 class ReadOnlyBufferException : RuntimeException {
199     mixin BasicExceptionCtors;
200 }
201 
202 class BufferUnderflowException : RuntimeException {
203     mixin BasicExceptionCtors;
204 }
205 
206 class BufferOverflowException : RuntimeException {
207     mixin BasicExceptionCtors;
208 }
209 
210 class UnsupportedOperationException : RuntimeException {
211     mixin BasicExceptionCtors;
212 }
213 
214 class NoSuchElementException : RuntimeException {
215     // this()
216     // {
217     //     super("");
218     // }
219     mixin BasicExceptionCtors;
220 }
221 
222 class NumberFormatException : IllegalArgumentException {
223     mixin BasicExceptionCtors;
224 }
225 
226 class NullPointerException : RuntimeException {
227     mixin BasicExceptionCtors;
228 }
229 
230 class EofException : RuntimeException {
231     mixin BasicExceptionCtors;
232 }
233 
234 class SecureNetException : RuntimeException {
235     mixin BasicExceptionCtors;
236 }
237 
238 class ArithmeticException : RuntimeException {
239     mixin BasicExceptionCtors;
240 }
241 
242 class ArrayIndexOutOfBoundsException : IndexOutOfBoundsException {
243     mixin BasicExceptionCtors;
244 }
245 
246 class IllegalStateException : Exception {
247     mixin BasicExceptionCtors;
248 }
249 
250 class InvalidMarkException : IllegalStateException {
251     mixin BasicExceptionCtors;
252 }
253 
254 class WritePendingException : IllegalStateException {
255     mixin BasicExceptionCtors;
256 }
257 
258 class CancellationException : IllegalStateException {
259     mixin BasicExceptionCtors;
260 }
261 
262 class OutOfMemoryError : Error {
263     // this(string msg, Throwable nextInChain = null)
264     // {
265     //     super(msg, nextInChain);
266     // }
267 
268     mixin BasicExceptionCtors;
269 }
270 
271 class GeneralSecurityException : Exception {
272     mixin BasicExceptionCtors;
273 }
274 
275 class CertificateException : GeneralSecurityException {
276     mixin BasicExceptionCtors;
277 }
278 
279 class NoSuchAlgorithmException : GeneralSecurityException {
280     mixin BasicExceptionCtors;
281 }
282 
283 class ConcurrentModificationException : RuntimeException {
284     mixin BasicExceptionCtors;
285 }
286 
287 class InternalError : Exception {
288     mixin BasicExceptionCtors;
289 }
290 
291 class CRLException : GeneralSecurityException {
292     mixin BasicExceptionCtors;
293 }
294 
295 class NoSuchProviderException : Exception {
296     mixin BasicExceptionCtors;
297 }
298 
299 class CertificateNotYetValidException : Exception {
300     mixin BasicExceptionCtors;
301 }
302 
303 class CertificateExpiredException : Exception {
304     mixin BasicExceptionCtors;
305 }
306 
307 class SignatureException : Exception {
308     mixin BasicExceptionCtors;
309 }
310 
311 class CertificateEncodingException : Exception {
312     mixin BasicExceptionCtors;
313 }
314 
315 class ParsingException : Exception {
316     mixin BasicExceptionCtors;
317 }
318 
319 class CertificateParsingException : ParsingException {
320     mixin BasicExceptionCtors;
321 }
322 
323 class InvalidKeyException : Exception {
324     mixin BasicExceptionCtors;
325 }
326 
327 class KeyStoreException : Exception {
328     mixin BasicExceptionCtors;
329 }
330 
331 class UnrecoverableKeyException : Exception {
332     mixin BasicExceptionCtors;
333 }
334 
335 class KeyManagementException : Exception {
336     mixin BasicExceptionCtors;
337 }
338 
339 class StringIndexOutOfBoundsException : Exception {
340     mixin BasicExceptionCtors;
341 }
342 
343 class IllegalThreadStateException : IllegalArgumentException {
344     mixin BasicExceptionCtors;
345 }
346 
347 class IllegalMonitorStateException : RuntimeException {
348     mixin BasicExceptionCtors;
349 }
350 
351 class NestedRuntimeException : Exception {
352     mixin BasicExceptionCtors;
353 }
354 
355 class InvalidClassException : Exception {
356     mixin BasicExceptionCtors;
357 }
358 
359 class InvalidObjectException : Exception {
360     mixin BasicExceptionCtors;
361 }
362 
363 class ClassCastException : Exception {
364     mixin BasicExceptionCtors;
365 }
366 
367 class SystemException : Exception {
368     mixin BasicExceptionCtors;
369 }