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