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 (Posix) {
23             import std.stdio;
24 
25             enum PRINT_COLOR_NONE = "\033[m";
26             enum PRINT_COLOR_YELLOW = "\033[1;33m";
27             stderr.writefln(PRINT_COLOR_YELLOW ~ "Implementation missing %s, in %s:%d" ~ PRINT_COLOR_NONE,
28                     name, file, line);
29         } else version(Windows) {
30             import hunt.system.WindowsHelper;
31             import core.sys.windows.wincon;
32             import std.format;
33             string msg = format("Implementation missing %s, in %s:%d", name, file, line);
34             ConsoleHelper.writeWithAttribute(msg, FOREGROUND_GREEN | FOREGROUND_RED);
35         } else {
36             assert(false, "Unsupported OS.");
37         }
38     }
39 }
40 
41 mixin template ExceptionBuild(string name, string parent = "") {
42     import std.exception;
43 
44     enum buildStr = "class " ~ name ~ "Exception : " ~ parent ~ "Exception { \n\t"
45         ~ "mixin basicExceptionCtors;\n }";
46     mixin(buildStr);
47 }
48 
49 mixin template BasicExceptionCtors() {
50     this(size_t line = __LINE__, string file = __FILE__) @nogc @safe pure nothrow {
51         super("", file, line, null);
52     }
53 
54     this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) @nogc @safe pure nothrow {
55         super(msg, file, line, next);
56     }
57 
58     this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__) @nogc @safe pure nothrow {
59         super(msg, file, line, next);
60     }
61 
62     this(Throwable next, string file = __FILE__, size_t line = __LINE__) @nogc @safe pure nothrow {
63         assert(next !is null);
64         super(next.msg, file, line, next);
65     }
66 
67     // mixin basicExceptionCtors;
68 }
69 
70 class ClassNotFoundException : Exception {
71     mixin BasicExceptionCtors;
72 }
73 
74 class NotImplementedException : Exception {
75     mixin BasicExceptionCtors;
76 }
77 
78 class NotSupportedException : Exception {
79     mixin BasicExceptionCtors;
80 }
81 
82 class IllegalArgumentException : Exception {
83     mixin BasicExceptionCtors;
84 }
85 
86 class RuntimeException : Exception {
87     this(Throwable ex) {
88         super(ex.msg, ex);
89     }
90 
91     /++
92         Params:
93             msg  = The message for the exception.
94             file = The file where the exception occurred.
95             line = The line number where the exception occurred.
96             next = The previous exception in the chain of exceptions, if any.
97     +/
98     this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) @nogc @safe pure nothrow {
99         super(msg, file, line, next);
100     }
101 
102     /++
103         Params:
104             msg  = The message for the exception.
105             next = The previous exception in the chain of exceptions.
106             file = The file where the exception occurred.
107             line = The line number where the exception occurred.
108     +/
109     this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__) @nogc @safe pure nothrow {
110         super(msg, file, line, next);
111     }
112     // mixin BasicExceptionCtors;
113 }
114 
115 class ExecutionException : Exception {
116     mixin BasicExceptionCtors;
117 }
118 
119 class InterruptedException : Exception {
120     mixin BasicExceptionCtors;
121 }
122 
123 class ParseException : Exception {
124     // mixin BasicExceptionCtors;
125 
126     /**
127      * Constructs a ParseException with the specified detail message and
128      * offset.
129      * A detail message is a String that describes this particular exception.
130      *
131      * @param s the detail message
132      * @param errorOffset the position where the error is found while parsing.
133      */
134     public this(string s, int errorOffset = -1) {
135         super(s);
136         this.errorOffset = errorOffset;
137     }
138 
139     /**
140      * Returns the position where the error was found.
141      *
142      * @return the position where the error was found
143      */
144     public int getErrorOffset() {
145         return errorOffset;
146     }
147 
148     //============ privates ============
149     /**
150      * The zero-based character offset into the string being parsed at which
151      * the error was found during parsing.
152      * @serial
153      */
154     private int errorOffset;
155 }
156 
157 class CloneNotSupportedException : Exception {
158     mixin BasicExceptionCtors;
159 }
160 
161 class TimeoutException : Exception {
162     mixin BasicExceptionCtors;
163 }
164 
165 class FileNotFoundException : Exception {
166     mixin BasicExceptionCtors;
167 }
168 
169 class IOException : Exception {
170     mixin BasicExceptionCtors;
171 }
172 
173 class CharacterCodingException : IOException {
174     mixin BasicExceptionCtors;
175 }
176 
177 class ClosedChannelException : IOException {
178     mixin BasicExceptionCtors;
179 }
180 
181 class EOFException : IOException {
182     mixin BasicExceptionCtors;
183 }
184 
185 class MalformedURLException : IOException {
186     mixin BasicExceptionCtors;
187 }
188 
189 class InterruptedIOException : IOException {
190     mixin BasicExceptionCtors;
191 }
192 
193 class RemoteException : IOException {
194     mixin BasicExceptionCtors;
195 }
196 
197 class URISyntaxException : IOException {
198     mixin BasicExceptionCtors;
199 }
200 
201 class UnsupportedEncodingException : IOException {
202     mixin BasicExceptionCtors;
203 }
204 
205 class AsynchronousCloseException : ClosedChannelException {
206     mixin BasicExceptionCtors;
207 }
208 
209 class CommonRuntimeException : RuntimeException {
210     mixin BasicExceptionCtors;
211 }
212 
213 class IndexOutOfBoundsException : RuntimeException {
214     mixin BasicExceptionCtors;
215 }
216 
217 class NegativeArraySizeException : RuntimeException {
218     mixin BasicExceptionCtors;
219 }
220 
221 class ReadOnlyBufferException : RuntimeException {
222     mixin BasicExceptionCtors;
223 }
224 
225 class BufferUnderflowException : RuntimeException {
226     mixin BasicExceptionCtors;
227 }
228 
229 class BufferOverflowException : RuntimeException {
230     mixin BasicExceptionCtors;
231 }
232 
233 class UnsupportedOperationException : RuntimeException {
234     mixin BasicExceptionCtors;
235 }
236 
237 class NoSuchElementException : RuntimeException {
238     mixin BasicExceptionCtors;
239 }
240 
241 class NumberFormatException : IllegalArgumentException {
242     mixin BasicExceptionCtors;
243 }
244 
245 class NullPointerException : RuntimeException {
246     mixin BasicExceptionCtors;
247 }
248 
249 class EofException : RuntimeException {
250     mixin BasicExceptionCtors;
251 }
252 
253 class SecureNetException : RuntimeException {
254     mixin BasicExceptionCtors;
255 }
256 
257 class ArithmeticException : RuntimeException {
258     mixin BasicExceptionCtors;
259 }
260 
261 class ArrayIndexOutOfBoundsException : IndexOutOfBoundsException {
262     mixin BasicExceptionCtors;
263 }
264 
265 class IllegalStateException : Exception {
266     mixin BasicExceptionCtors;
267 }
268 
269 class InvalidMarkException : IllegalStateException {
270     mixin BasicExceptionCtors;
271 }
272 
273 class WritePendingException : IllegalStateException {
274     mixin BasicExceptionCtors;
275 }
276 
277 class CancellationException : IllegalStateException {
278     mixin BasicExceptionCtors;
279 }
280 
281 class GeneralSecurityException : Exception {
282     mixin BasicExceptionCtors;
283 }
284 
285 class CertificateException : GeneralSecurityException {
286     mixin BasicExceptionCtors;
287 }
288 
289 class NoSuchAlgorithmException : GeneralSecurityException {
290     mixin BasicExceptionCtors;
291 }
292 
293 class ConcurrentModificationException : RuntimeException {
294     mixin BasicExceptionCtors;
295 }
296 
297 class InternalError : Exception {
298     mixin BasicExceptionCtors;
299 }
300 
301 class CRLException : GeneralSecurityException {
302     mixin BasicExceptionCtors;
303 }
304 
305 class NoSuchProviderException : Exception {
306     mixin BasicExceptionCtors;
307 }
308 
309 class CertificateNotYetValidException : Exception {
310     mixin BasicExceptionCtors;
311 }
312 
313 class CertificateExpiredException : Exception {
314     mixin BasicExceptionCtors;
315 }
316 
317 class SignatureException : Exception {
318     mixin BasicExceptionCtors;
319 }
320 
321 class CertificateEncodingException : Exception {
322     mixin BasicExceptionCtors;
323 }
324 
325 class ParsingException : Exception {
326     mixin BasicExceptionCtors;
327 }
328 
329 class CertificateParsingException : ParsingException {
330     mixin BasicExceptionCtors;
331 }
332 
333 class InvalidKeyException : Exception {
334     mixin BasicExceptionCtors;
335 }
336 
337 class KeyStoreException : Exception {
338     mixin BasicExceptionCtors;
339 }
340 
341 class UnrecoverableKeyException : Exception {
342     mixin BasicExceptionCtors;
343 }
344 
345 class KeyManagementException : Exception {
346     mixin BasicExceptionCtors;
347 }
348 
349 class StringIndexOutOfBoundsException : Exception {
350     mixin BasicExceptionCtors;
351 }
352 
353 class IllegalThreadStateException : IllegalArgumentException {
354     mixin BasicExceptionCtors;
355 }
356 
357 class IllegalMonitorStateException : RuntimeException {
358     mixin BasicExceptionCtors;
359 }
360 
361 class NestedRuntimeException : Exception {
362     mixin BasicExceptionCtors;
363 }
364 
365 class InvalidClassException : Exception {
366     mixin BasicExceptionCtors;
367 }
368 
369 class InvalidObjectException : Exception {
370     mixin BasicExceptionCtors;
371 }
372 
373 class ClassCastException : Exception {
374     mixin BasicExceptionCtors;
375 }
376 
377 class SystemException : Exception {
378     mixin BasicExceptionCtors;
379 }
380 
381 class SQLException : Exception {
382     mixin BasicExceptionCtors;
383 }
384 
385 class ConfigurationException : Exception {
386     mixin BasicExceptionCtors;
387 }
388 
389 
390 // ===================== Error =====================
391 
392 class IncompatibleClassChangeError : LinkageError {
393     mixin BasicExceptionCtors;
394 }
395 
396 class AssertionError : Error {
397     mixin BasicExceptionCtors;
398 }
399 
400 class LinkageError : Error {
401     mixin BasicExceptionCtors;
402 }
403 
404 class OutOfMemoryError : Error {
405     mixin BasicExceptionCtors;
406 }
407 
408 class ThreadDeath : Error {
409     mixin BasicExceptionCtors;
410 }
411 
412 class InstantiationError : IncompatibleClassChangeError {
413     mixin BasicExceptionCtors;
414 }