aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribilo/condition.scm
blob: 349013510174f8eb18deddc3188ae16823bf3391 (plain)
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
;;; condition.scm  --  Skribilo SRFI-35 error condition hierarchy.
;;;
;;; Copyright 2006  Ludovic Court�s  <ludovic.courtes@laas.fr>
;;;
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
;;; USA.

(define-module (skribilo condition)
  :autoload   (srfi srfi-1)  (find)
  :autoload   (srfi srfi-34) (guard)
  :use-module (srfi srfi-35)
  :use-module (srfi srfi-39)
  :autoload   (skribilo utils syntax) (_ N_)
  :export     (&skribilo-error skribilo-error?
	       &invalid-argument-error invalid-argument-error?
	       &too-few-arguments-error too-few-arguments-error?

	       &file-error file-error?
	       &file-search-error file-search-error?
	       &file-open-error file-open-error?
	       &file-write-error file-write-error?

	       register-error-condition-handler!
	       lookup-error-condition-handler

	       %call-with-skribilo-error-catch
	       call-with-skribilo-error-catch))

;;; Author:  Ludovic Court�s
;;;
;;; Commentary:
;;;
;;; Top-level of Skribilo's SRFI-35 error conditions.
;;;
;;; Code:


;;;
;;; Standard error conditions.
;;;

(define-condition-type &skribilo-error &error
  skribilo-error?)


;;;
;;; Generic errors.
;;;

(define-condition-type &invalid-argument-error &skribilo-error
  invalid-argument-error?
  (proc-name invalid-argument-error:proc-name)
  (argument  invalid-argument-error:argument))

(define-condition-type &too-few-arguments-error &skribilo-error
  too-few-arguments-error?
  (proc-name too-few-arguments-error:proc-name)
  (arguments too-few-arguments-error:arguments))


;;;
;;; File errors.
;;;

(define-condition-type &file-error &skribilo-error
  file-error?
  (file-name file-error:file-name))

(define-condition-type &file-search-error &file-error
  file-search-error?
  (path file-search-error:path))

(define-condition-type &file-open-error &file-error
  file-open-error?)

(define-condition-type &file-write-error &file-error
  file-write-error?)



;;;
;;; Adding new error conditions from other modules.
;;;

(define %external-error-condition-alist '())

(define (register-error-condition-handler! pred handler)
  (set! %external-error-condition-alist
	(cons (cons pred handler)
	      %external-error-condition-alist)))

(define (lookup-error-condition-handler c)
  (let ((pair (find (lambda (pair)
		      (let ((pred (car pair)))
			(pred c)))
		    %external-error-condition-alist)))
    (if (pair? pair)
	(cdr pair)
	#f)))



;;;
;;; Convenience functions.
;;;

(define (%call-with-skribilo-error-catch thunk exit exit-val)
  (guard (c ((invalid-argument-error? c)
	     (format (current-error-port)
                     (_ "in `~a': invalid argument: ~S~%")
		     (invalid-argument-error:proc-name c)
		     (invalid-argument-error:argument c))
	     (exit exit-val))

	    ((too-few-arguments-error? c)
	     (format (current-error-port)
                     (_ "in `~a': too few arguments: ~S~%")
		     (too-few-arguments-error:proc-name c)
		     (too-few-arguments-error:arguments c)))

	    ((file-search-error? c)
	     (format (current-error-port)
                     (_ "~a: not found in path `~S'~%")
		     (file-error:file-name c)
		     (file-search-error:path c))
	     (exit exit-val))

	    ((file-open-error? c)
	     (format (current-error-port)
                     (_ "~a: cannot open file~%")
		     (file-error:file-name c))
	     (exit exit-val))

	    ((file-write-error? c)
	     (format (current-error-port)
                     (_ "~a: cannot write to file~%")
		     (file-error:file-name c))
	     (exit exit-val))

	    ((file-error? c)
	     (format (current-error-port)
                     (_ "file error: ~a~%")
		     (file-error:file-name c))
	     (exit exit-val))

	    (;;(skribilo-error? c)
	     #t ;; XXX: The SRFI-35 currently in `guile-lib' doesn't work
 	        ;; properly with non-direct super-types.
	     (let ((handler (lookup-error-condition-handler c)))
	       (if (procedure? handler)
		   (handler c)
		   (format (current-error-port)
			   (_ "undefined skribilo error: ~S~%")
			   c)))
	     (exit exit-val)))

	 (thunk)))

(define-macro (call-with-skribilo-error-catch thunk)
  `(call/cc (lambda (cont)
	      (%call-with-skribilo-error-catch ,thunk cont #f))))

;;; arch-tag: 285010f9-06ea-4c39-82c2-6c3604f668b3

;;; conditions.scm ends here