cmake build system: visiblity support for clang
[supercollider.git] / SCClassLibrary / Common / Core / Error.sc
blobcf31681bf6c3cc38e8a9ab62a709239a9623229e
1 Exception {
2         classvar <>handling = false;
3         classvar <>debug = false;
4         classvar <>inProtectedFunction = false;
6         var <>what, <>protectedBacktrace, <>path;
8         *new { arg what;
9                 var protectedBacktrace, instance;
10                 if (debug || inProtectedFunction, {
11                         protectedBacktrace = this.getBackTrace.caller;
12                         inProtectedFunction = false;
13                 });
14                 ^super.newCopyArgs(what ? this.name, protectedBacktrace, thisProcess.nowExecutingPath);
15         }
16         errorString {
17                 ^"EXCEPTION: " ++ what
18         }
19         reportError {
20                 this.errorString.postln;
21                 if(protectedBacktrace.notNil, { this.postProtectedBacktrace });
22                 this.dumpBackTrace;
23                 this.adviceLink.postln;
24         }
25         adviceLink {
26                 ^("For advice: [http://supercollider.sf.net/wiki/index.php/%]"
27                         .format(this.adviceLinkPage));
28         }
29         adviceLinkPage {
30                 ^this.errorString.tr($ , $_).tr($\n, $_);
31         }
32         postProtectedBacktrace {
33                 var out, currentFrame, def, ownerClass, methodName, pos = 0;
34                 out = CollStream.new;
35                 "\nPROTECTED CALL STACK:".postln;
36                 currentFrame = protectedBacktrace;
37                 while({currentFrame.notNil}, {
38                         def = currentFrame.functionDef;
39                         if(def.isKindOf(Method), {
40                                 ownerClass = def.ownerClass;
41                                 methodName = def.name;
42                                 if(ownerClass == Function && (methodName == 'protect'), {
43                                         pos = out.pos;
44                                 });
45                                 out << "\t%:%\t%\n".format(ownerClass, methodName, currentFrame.address);
46                         }, {
47                                 out << "\ta FunctionDef\t%\n".format(currentFrame.address);
48                                 out << "\t\tsourceCode = %\n".format(def.sourceCode ? "<an open Function>");
49                         });
50                         def.argNames.do({|name, i|
51                                 out << "\t\targ % = %\n".format(name, currentFrame.args[i]);
52                         });
53                         def.varNames.do({|name, i|
54                                 out << "\t\targ % = %\n".format(name, currentFrame.args[i]);
55                         });
56                         currentFrame = currentFrame.caller;
57                 });
58                 // lose everything after the last Function:protect
59                 // it just duplicates the normal stack with less info
60                 out.collection.copyFromStart(pos).postln;
61         }
63         isException { ^true }
66 Error : Exception {
67         errorString {
68                 ^"ERROR: " ++ what
69         }
70         errorPathString {
71                 ^if(path.isNil) { "" } { "PATH:" + path ++ "\n" }
72         }
75 MethodError : Error {
76         var <>receiver;
78         *new { arg what, receiver;
79                 ^super.new(what).receiver_(receiver)
80         }
81         reportError {
82                 this.errorString.postln;
83                 "RECEIVER:\n".post;
84                 receiver.dump;
85                 this.errorPathString.post;
86                 if(protectedBacktrace.notNil, { this.postProtectedBacktrace });
87                 this.dumpBackTrace;
88                 this.adviceLink.postln;
89         }
90         adviceLinkPage {
91                 ^this.class.name
92         }
96 PrimitiveFailedError : MethodError {
97         var <>failedPrimitiveName;
99         *new { arg receiver;
100                 ^super.new(Thread.primitiveErrorString, receiver)
101                         .failedPrimitiveName_(thisThread.failedPrimitiveName)
102         }
103         errorString {
104                 ^"ERROR: Primitive '%' failed.\n%".format(failedPrimitiveName, what ? "")
105         }
108 SubclassResponsibilityError : MethodError {
109         var <>method, <>class;
110         *new { arg receiver, method, class;
111                 ^super.new(nil, receiver).method_(method).class_(class)
112         }
113         errorString {
114                 ^"ERROR: '" ++ method.name ++ "' should have been implemented by "
115                         ++ class.name ++ "."
116         }
119 ShouldNotImplementError : MethodError {
120         var <>method, <>class;
121         *new { arg receiver, method, class;
122                 ^super.new(nil, receiver).method_(method).class_(class)
123         }
124         errorString {
125                 ^"ERROR: '" ++ method.ownerClass.name ++ "-" ++ method.name
126                         ++ "' Message not valid for this subclass: "
127                         ++ class.name ++ "."
128         }
131 DoesNotUnderstandError : MethodError {
132         var <>selector, <>args;
133         *new { arg receiver, selector, args;
134                 ^super.new(nil, receiver).selector_(selector).args_(args)
135         }
136         errorString {
137                 ^"ERROR: Message '" ++ selector ++ "' not understood."
138         }
139         reportError {
140                 this.errorString.postln;
141                 "RECEIVER:\n".post;
142                 receiver.dump;
143                 "ARGS:\n".post;
144                 args.dumpAll;
145                 this.errorPathString.post;
146                 if(protectedBacktrace.notNil, { this.postProtectedBacktrace });
147                 this.dumpBackTrace;
148                 this.adviceLink.postln;
149         }
150         adviceLinkPage {
151                 ^"%#%".format(this.class.name, selector)
152         }
156 MustBeBooleanError : MethodError {
157         errorString {
158                 ^"ERROR: Non Boolean in test."
159         }
162 NotYetImplementedError : MethodError {
165 OutOfContextReturnError : MethodError {
166         var <>method, <>result;
167         *new { arg receiver, method, result;
168                 ^super.new(nil, receiver).method_(method).result_(result)
169         }
170         errorString {
171                 ^"ERROR: '" ++ method.ownerClass.name ++ "-" ++ method.name
172                         ++ "' Out of context return of value: " ++ result
173         }
176 ImmutableError : MethodError {
177         var <>value;
178         *new { arg receiver, value;
179                 ^super.new(nil, receiver).value_(value)
180         }
181         errorString {
182                 ^"ERROR: Object is immutable: " ++ receiver
183         }
186 BinaryOpFailureError : DoesNotUnderstandError {
187         errorString {
188                 ^"ERROR: binary operator '" ++ selector ++ "' failed."
189         }
192 DeprecatedError : MethodError {
193         var <>method, <>class, <>alternateMethod;
195         *new { arg receiver, method, alternateMethod, class;
196                 ^super.new(nil).receiver_(receiver).method_(method).class_(class).alternateMethod_(alternateMethod)
197         }
198         errorString {
199                 var methodSignature = { arg m;
200                         var c = m.ownerClass;
201                         var str = c.name.asString;
202                         if(c.isMetaClass)
203                                 { str = str.drop( str.indexOf($_) + 1 ) ++ ":*" ++ m.name }
204                                 { str = str ++ ":-" ++ m.name };
205                         str;
206                 };
207                 var string;
208                 string = "WARNING: Method" + methodSignature.value(method) + "is deprecated and will be removed.";
209                 if(alternateMethod.notNil, {
210                         string = string + "Use" + methodSignature.value(alternateMethod) + "instead.";
211                 });
212                 ^string;
213         }
215         reportError {
216                 this.errorString.postln;
217                 this.errorPathString.post;
218                 this.adviceLink.postln;
219         }
221         throw {
222                 Error.handling = true;
223                 this.reportError;
224                 if (Error.debug) {
225                         if(protectedBacktrace.notNil, { this.postProtectedBacktrace });
226                         this.dumpBackTrace;
227                         Error.handling = false;
228                         this.halt;
229                 } {
230                         Error.handling = false;
231                 };
233         }
234         adviceLinkPage {
235                 ^"DeprecatedError"
236         }