aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribilo/condition.scm
blob: 820dcc59a46d7d846260fb5abf2fb5c24463d063 (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
;;; 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
;;; USA.

(define-module (skribilo condition)
  :autoload   (srfi srfi-34) (guard)
  :use-module (srfi srfi-35)
  :use-module (srfi srfi-39)
  :export     (&skribilo-error skribilo-error?
	       &invalid-argument-error invalid-argument-error?
	       &file-error file-error?
	       &file-search-error file-search-error?
	       &file-open-error file-open-error?
	       &file-write-error file-write-error?

	       %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))


;;;
;;; 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?)



;;;
;;; 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))

	    ((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)
	     (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