-
Notifications
You must be signed in to change notification settings - Fork 115
/
error.ss
386 lines (314 loc) · 12.1 KB
/
error.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
;;; -*- Gerbil -*-
;;; (C) vyzo
;;; Gerbil error objects
(import :gerbil/runtime/error
:gerbil/runtime/hash
:gerbil/runtime/thread
(for-syntax :gerbil/expander))
(export Exception Exception?
RuntimeException RuntimeException?
Error Error?
Error-message Error-irritants Error-where
exception-context
Error:::init!
deferror-class
defraise/context
raise/context
StackTrace StackTrace?
check-argument
exception-context
IOError IOError? raise-io-error io-error?
Closed Closed? raise-io-closed io-closed-error?
PrematureEndOfInput PrematureEndOfInput? raise-premature-end-of-input
premature-end-of-input-error?
Timeout Timeout? raise-timeout timeout-error?
UnboundKeyError UnboundKeyError? raise-unbound-key unbound-key-error?
ContextError ContextError? raise-context-error context-error?
UnsupportedMethod UnsupportedMethod? raise-unsupported-method unsupported-method-error?
ContractViolation ContractViolation? raise-contract-violation contract-violation-error?
raise-bad-argument
(rename: raise-bug BUG)
is-it-bug?
with-exception-stack-trace
dump-stack-trace?
dump-stack-trace!
exit-with-error
exit-on-error?
call-with-exit-on-error
with-exit-on-error)
;; utility macro for definint error classes
(defsyntax (deferror-class stx)
(syntax-case stx ()
((_ Class slots)
(identifier? #'Class)
#'(deferror-class (Class Error) slots #f Error:::init!))
((_ Class slots predicate-alias)
(identifier? #'Class)
#'(deferror-class (Class Error) slots predicate-alias Error:::init!))
((_ Class slots predicate-alias kons)
(identifier? #'Class)
#'(deferror-class (Class Error) slots predicate-alias kons))
((_ (Class Mixin ...) slots)
(identifier-list? #'(Class Mixin ...))
#'(deferror-class (Class Mixin ...) slots #f Error:::init!))
((_ (Class Mixin ...) slots predicate-alias)
(identifier-list? #'(Class Mixin ...))
#'(deferror-class (Class Mixin ...) slots predicate-alias Error:::init!))
((_ (Class Mixin ...) slots predicate-alias kons)
(identifier-list? #'(Class Mixin ...))
(with-syntax ((defpredicate-alias
(if (stx-e #'predicate-alias)
(with-syntax ((Class? (stx-identifier #'Class #'Class "?")))
#'(def predicate-alias Class?))
#'(begin))))
#'(begin
(defclass (Class Mixin ...) slots transparent: #t)
(defmethod {:init! Class}
kons)
defpredicate-alias)))))
;; IO Errors
(deferror-class IOError () io-error?)
(deferror-class (PrematureEndOfInput IOError) () premature-end-of-input-error?)
(deferror-class (Closed IOError) () io-closed-error?)
;; Timeouts
(deferror-class Timeout () timeout-error?)
;; contextual errors
(deferror-class ContextError () context-error?)
;; unsupported interface methods
(deferror-class UnsupportedMethod () unsupported-method-error?)
;; utility macros
(defsyntax (exception-context stx)
(syntax-case stx ()
((macro)
#'(exception-context macro))
((_ here)
(with-syntax ((where
(cond
((or (stx-source #'here)
(stx-source stx))
=> (lambda (locat)
(call-with-output-string "" (cut ##display-locat locat #t <>))))
(else
(expander-context-id (core-context-top))))))
#'(quote where)))))
(defrules check-argument ()
((_ expr expectation argument)
(begin-annotation @contract
(unless expr
(abort!
(raise-contract-violation argument expr (string-append "bad argument; expected " expectation) argument))))))
;; check to the raiser!
(defrules raise/context ()
((macro exn)
(raise/context exn where: macro))
((_ exn where: ctx)
(abort!
(let (e exn)
(set! (@ e where) (exception-context ctx))
(raise e)))))
(defrules defraise/context ()
((_ (rule where args ...) (Klass message irritants: irritants))
(defrules rule ()
((_ where args ...)
(abort!
(raise
(Klass message
where: (exception-context where)
irritants: (cons 'where irritants))))))))
(defraise/context (raise-io-error where message irritants ...)
(IOError message irritants: [irritants ...]))
(defraise/context (raise-premature-end-of-input where irritants ...)
(PrematureEndOfInput "premature end of input" irritants: [irritants ...]))
(defraise/context (raise-io-closed where message irritants ...)
(Closed message irritants: [irritants ...]))
(defraise/context (raise-timeout where message irritants ...)
(Timeout message irritants: [irritants ...]))
(defraise/context (raise-context-error where message irritants ...)
(ContextError message irritants: [irritants ...]))
(defraise/context (raise-unsupported-method where)
(UnsupportedMethod "unsupported method" irritants: []))
(defraise/context (raise-contract-violation where contract irritants ...)
(ContractViolation "contract violation" irritants: ['contract irritants ...]))
(defraise/context (raise-bad-argument where expectation irritants ...)
(ContractViolation "contract violation" irritants: [expectation irritants ...]))
(defraise/context (raise-unbound-key where irritants ...)
(UnboundKeyError "no value associated with key" irritants: [irritants ...]))
;; it's a bug
(deferror-class BUG () is-it-bug?)
(defraise/context (raise-bug where message irritants ...)
(BUG (string-append "BUG: " message) irritants: [irritants ...]))
;; runtime exceptions
(defrules export-runtime-exceptions ()
((_ (symbol ...) ...)
(begin (export symbol ...) ...)))
(export-runtime-exceptions
(abandoned-mutex-exception?)
(cfun-conversion-exception?
cfun-conversion-exception-arguments
cfun-conversion-exception-code
cfun-conversion-exception-message
cfun-conversion-exception-procedure)
(datum-parsing-exception?
datum-parsing-exception-kind
datum-parsing-exception-parameters
datum-parsing-exception-readenv)
(deadlock-exception?)
(divide-by-zero-exception?
divide-by-zero-exception-arguments
divide-by-zero-exception-procedure)
(error-exception?
error-exception-message
error-exception-parameters)
(expression-parsing-exception?
expression-parsing-exception-kind
expression-parsing-exception-parameters
expression-parsing-exception-source)
(file-exists-exception?
file-exists-exception-arguments
file-exists-exception-procedure)
(fixnum-overflow-exception?
fixnum-overflow-exception-arguments
fixnum-overflow-exception-procedure)
(heap-overflow-exception?)
(inactive-thread-exception?
inactive-thread-exception-arguments
inactive-thread-exception-procedure)
(initialized-thread-exception?
initialized-thread-exception-arguments
initialized-thread-exception-procedure)
(invalid-hash-number-exception?
invalid-hash-number-exception-arguments
invalid-hash-number-exception-procedure)
(invalid-utf8-encoding-exception?
invalid-utf8-encoding-exception-arguments
invalid-utf8-encoding-exception-procedure)
(join-timeout-exception?
join-timeout-exception-arguments
join-timeout-exception-procedure)
(keyword-expected-exception?
keyword-expected-exception-arguments
keyword-expected-exception-procedure)
(length-mismatch-exception?
length-mismatch-exception-arg-id
length-mismatch-exception-arguments
length-mismatch-exception-procedure)
(mailbox-receive-timeout-exception?
mailbox-receive-timeout-exception-arguments
mailbox-receive-timeout-exception-procedure)
(module-not-found-exception?
module-not-found-exception-arguments
module-not-found-exception-procedure)
(multiple-c-return-exception?)
(no-such-file-or-directory-exception?
no-such-file-or-directory-exception-arguments
no-such-file-or-directory-exception-procedure)
(noncontinuable-exception?
noncontinuable-exception-reason)
(nonempty-input-port-character-buffer-exception?
nonempty-input-port-character-buffer-exception-arguments
nonempty-input-port-character-buffer-exception-procedure)
(nonprocedure-operator-exception?
nonprocedure-operator-exception-arguments
nonprocedure-operator-exception-code
nonprocedure-operator-exception-operator
nonprocedure-operator-exception-rte)
(not-in-compilation-context-exception?
not-in-compilation-context-exception-arguments
not-in-compilation-context-exception-procedure)
(number-of-arguments-limit-exception?
number-of-arguments-limit-exception-arguments
number-of-arguments-limit-exception-procedure)
(os-exception?
os-exception-arguments
os-exception-code
os-exception-message
os-exception-procedure)
(permission-denied-exception?
permission-denied-exception-arguments
permission-denied-exception-procedure)
(range-exception?
range-exception-arg-id
range-exception-arguments
range-exception-procedure)
(rpc-remote-error-exception?
rpc-remote-error-exception-arguments
rpc-remote-error-exception-message
rpc-remote-error-exception-procedure)
(scheduler-exception?
scheduler-exception-reason)
(sfun-conversion-exception?
sfun-conversion-exception-arguments
sfun-conversion-exception-code
sfun-conversion-exception-message
sfun-conversion-exception-procedure)
(stack-overflow-exception?)
(started-thread-exception?
started-thread-exception-arguments
started-thread-exception-procedure)
(terminated-thread-exception?
terminated-thread-exception-arguments
terminated-thread-exception-procedure)
(type-exception?
type-exception-arg-id
type-exception-arguments
type-exception-procedure
type-exception-type-id)
(unbound-global-exception?
unbound-global-exception-code
unbound-global-exception-rte
unbound-global-exception-variable)
(unbound-key-exception?
unbound-key-exception-arguments
unbound-key-exception-procedure)
(unbound-os-environment-variable-exception?
unbound-os-environment-variable-exception-arguments
unbound-os-environment-variable-exception-procedure)
(unbound-serial-number-exception?
unbound-serial-number-exception-arguments
unbound-serial-number-exception-procedure)
(uncaught-exception?
uncaught-exception-arguments
uncaught-exception-procedure
uncaught-exception-reason)
(uninitialized-thread-exception?
uninitialized-thread-exception-arguments
uninitialized-thread-exception-procedure)
(unknown-keyword-argument-exception?
unknown-keyword-argument-exception-arguments
unknown-keyword-argument-exception-procedure)
(unterminated-process-exception?
unterminated-process-exception-arguments
unterminated-process-exception-procedure)
(wrong-number-of-arguments-exception?
wrong-number-of-arguments-exception-arguments
wrong-number-of-arguments-exception-procedure)
(wrong-number-of-values-exception?
wrong-number-of-values-exception-code
wrong-number-of-values-exception-rte
wrong-number-of-values-exception-vals)
(wrong-processor-c-return-exception?))
(def exit-on-error? (make-parameter #t))
(def (exit-with-error e)
(def port (current-error-port))
(defrules ignore-errors () ((_ body ...) (with-catch void (lambda () body ...))))
(ignore-errors (force-output port))
(ignore-errors (display-build-manifest (build-manifest) port))
(ignore-errors (newline port))
(ignore-errors (display-exception e port))
;; If the stack trace was printed, making the message out of reach of the user,
;; then redundantly print the error message at the bottom without the stack trace.
(ignore-errors
(when (and (dump-stack-trace?) (StackTrace? e))
(parameterize ((dump-stack-trace? #f))
(display-exception e port))))
(ignore-errors (force-output port))
(exit 2))
(def (call-with-exit-on-error thunk)
(with-catch
(lambda (e)
(if (exit-on-error?)
(exit-with-error e)
(raise e)))
thunk))
(defrules with-exit-on-error ()
((_ body ...) (call-with-exit-on-error (lambda () body ...))))