1 module yu.exception;
2 
3 public import std.exception : basicExceptionCtors;
4 
5 mixin template ExceptionBuild(string name, string parent = "") {
6     enum buildStr = "class " ~ name ~ "Exception : " ~ parent ~ "Exception { \n\t" ~ "mixin basicExceptionCtors;\n }";
7     mixin(buildStr);
8 }
9 
10 mixin ExceptionBuild!("Yu");
11 
12 mixin template ThrowExceptionBuild() {
13     ///Note:from GC
14     pragma(inline, true) void throwExceptionBuild(string name = "")(string msg = "",
15         string file = __FILE__, size_t line = __LINE__) {
16         mixin("throw new " ~ name ~ "Exception(msg,file,line);");
17     }
18 }
19 
20 pragma(inline) Exception showException(int line = __LINE__,
21     string file = __FILE__, string funcName = __FUNCTION__)(Exception e) nothrow {
22     import std.experimental.logger;
23     import std.exception;
24     if(e !is null)
25         collectException(error!(line, file, funcName)(e.toString));
26     return e;
27 }
28 
29 string buildErroCodeException(T)() if (is(T == enum)) {
30     string str = "mixin ExceptionBuild!(\"" ~ T.stringof ~ "\");\n";
31     foreach (memberName; __traits(derivedMembers, T)) {
32         str ~= "mixin ExceptionBuild!(\"" ~ memberName ~ "\", \"" ~ T.stringof ~ "\");\n";
33     }
34     return str;
35 }
36 
37 Exception yuCathException(E)(lazy E expression) nothrow {
38     import std.experimental.logger;
39     import std.exception : collectException;
40     import std.stdio;
41 
42     try {
43         expression();
44     }
45     catch (Exception e) {
46         return e;
47     }
48     catch (Error e) {
49         collectException({ error(e.toString); writeln(e.toString()); }());
50         import core.stdc.stdlib;
51 
52         exit(-1);
53     }
54     return null;
55 }
56 
57 Exception yuCathException(E, T)(lazy E expression, ref T value) nothrow {
58     import std.experimental.logger;
59     import std.exception : collectException;
60     import std.stdio;
61 
62     try {
63         value = expression();
64     }
65     catch (Exception e) {
66         return e;
67     }
68     catch (Error e) {
69         collectException({ error(e.toString); writeln(e.toString()); }());
70         import core.stdc.stdlib;
71 
72         exit(-1);
73     }
74     return null;
75 }
76 
77 version (unittest) {
78     enum Test {
79         MyTest1,
80         MyTest2,
81     }
82     //mixin ExceptionBuild!"MyTest1";
83     //mixin ExceptionBuild!"MyTest2";
84     mixin(buildErroCodeException!Test());
85     mixin ThrowExceptionBuild;
86 }
87 
88 unittest {
89     import std.stdio;
90     import std.exception;
91 
92     auto e = collectException!TestException(throwExceptionBuild!"Test"("test Exception"));
93     assert(e !is null);
94     auto e1 = collectException!MyTest1Exception(throwExceptionBuild!"MyTest1"("test Exception"));
95     assert(e1 !is null);
96     auto e2 = collectException!MyTest2Exception(throwExceptionBuild!"MyTest2"("test Exception"));
97     assert(e2 !is null);
98 }