-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr.rkt
86 lines (69 loc) · 3.39 KB
/
err.rkt
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
#lang racket
;; Err - logging facilities and error handling
(require racket/logging
"lang.rkt"
"lang-defaults.rkt")
(provide log-fatal log-error log-info log-debug log-warning app-logger register-errors current-error-messages
warn fatal fail info debug)
(define-logger app)
(current-logger app-logger)
(define current-error-messages (make-parameter (make-hash)))
(define (register-errors alist)
(for ([spec (in-list alist)])
(cond
((hash-has-key? (current-error-messages) (first spec))
(log-fatal "register-errors: duplicate error number ~s, already used for message ~s attempting to redefine for message ~s"
(first spec) (hash-ref (current-error-messages) (first spec) "unknown") (second spec))
(error 'register-errors "duplicate error number ~s, already used for message ~s attempting to redefine for message ~s"
(first spec) (hash-ref (current-error-messages) (first spec) "unknown") (second spec)))
(else (hash-set! (current-error-messages) (first spec) (second spec))))))
(define (translate-level level)
(case level
((warning) (appy-Warning))
((info) (appy-Info))
((debug) (appy-Debug))
((fatal) (appy-Fatal))
(else (appy-Error))))
(define (app-error level source num args)
(let ((msg (if (hash-has-key? (current-error-messages) num)
(let ((error-msg (hash-ref (current-error-messages) num 'appy-unknown-error)))
(format "[~a] ~a: ~a [~a ~s]" num (translate-level level)
(if (string? error-msg)
(if (null? args) error-msg (apply format (cons error-msg args)))
(apply tr (cons error-msg args)))
(appy-Module)
(symbol->string source)))
(appy-err-event
num (appy-Error) (translate-level level) num (appy-Module) (symbol->string source)))))
(case level
((info) (log-info msg))
((fatal) (log-fatal msg) (error msg))
((debug) (log-debug msg))
((warning) (log-warning msg))
(else (log-error msg) (error msg)))))
(define (warn source num . args)
(app-error 'warning source num args))
(define (fatal source num . args)
(app-error 'fatal source num args))
(define (info source num . args)
(app-error 'info source num args))
(define (fail source num . args)
(app-error 'error source num args))
(define (debug source num . args)
(app-error 'debug source num args))
(module+ test
(require rackunit)
(check-not-exn (lambda () (register-errors
'((-1 "just some warning ~a")
(-2 "some info string with arg1=~s and arg2=~s")
(-3 "some debugging information ~s")
(-4 "some general error")
(-5 "some fatal error condition due to ~a")))))
(check-not-exn (lambda () (warn 'warning-test -1 "for testing")))
(check-not-exn (lambda () (info 'info-test -2 "argument 1" "argument 2")))
(check-not-exn (lambda () (debug 'debug-test -3 '(debug this))))
(check-exn exn:fail? (lambda () (fail 'fail-test -4)))
(check-exn exn:fail? (lambda () (fatal 'fatal-error-test -5 "intentional failure")))
(check-not-exn (lambda () (warn 'warning-without-message -6)))
(check-exn exn:fail? (lambda () (fatal 'fatal-error-without-message -7)))
)