2 # e2mmap.rb - for ruby 1.1
3 # $Release Version: 2.0$
5 # $Date: 1999/02/17 12:33:17 $
13 # extend Exception2MessageMapper
14 # def_e2message ExistingExceptionClass, "message..."
15 # def_exception :NewExceptionClass, "message..."[, superclass]
21 # extend Exception2MessageMapper
22 # def_e2meggage ExistingExceptionClass, "message..."
23 # def_exception :NewExceptionClass, "message..."[, superclass]
36 # extend Exception2MessageMapper
37 # def_e2message ExistingExceptionClass, "message..."
38 # def_exception :NewExceptionClass, "message..."[, superclass]
42 # extend Exception2MessageMapper
47 # Foo.Fail NewExceptionClass, arg...
48 # Foo.Fail ExistingExceptionClass, arg...
51 module Exception2MessageMapper
52 @RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
54 E2MM = Exception2MessageMapper
56 def E2MM.extend_object(cl)
58 cl.bind(self) unless cl < E2MM
63 def Raise(err = nil, *rest)
64 Exception2MessageMapper.Raise(self.class, err, *rest)
68 def self.included(mod)
69 mod.extend Exception2MessageMapper
76 # rest: message arguments
78 def Raise(err = nil, *rest)
79 E2MM.Raise(self, err, *rest)
86 # define exception c with message m.
88 def def_e2message(c, m)
89 E2MM.def_e2message(self, c, m)
92 # def_exception(n, m, s)
95 # s: superclass(default: StandardError)
96 # define exception named ``c'' with message m.
98 def def_exception(n, m, s = StandardError)
99 E2MM.def_exception(self, n, m, s)
103 # Private definitions.
105 # {[class, exp] => message, ...}
108 # E2MM.def_exception(k, e, m)
109 # k: class to define exception under.
112 # define exception c with message m.
114 def E2MM.def_e2message(k, c, m)
115 E2MM.instance_eval{@MessageMap[[k, c]] = m}
119 # E2MM.def_exception(k, n, m, s)
120 # k: class to define exception under.
123 # s: superclass(default: StandardError)
124 # define exception named ``c'' with message m.
126 def E2MM.def_exception(k, n, m, s = StandardError)
127 n = n.id2name if n.kind_of?(Fixnum)
129 E2MM.instance_eval{@MessageMap[[k, e]] = m}
133 # Fail(klass, err, *rest)
134 # klass: class to define exception under.
136 # rest: message arguments
138 def E2MM.Raise(klass = E2MM, err = nil, *rest)
139 if form = e2mm_message(klass, err)
140 b = $@.nil? ? caller(1) : $@
143 b.shift if b[0] =~ /^#{Regexp.quote(__FILE__)}:/
144 raise err, sprintf(form, *rest), b
146 E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
153 def E2MM.e2mm_message(klass, exp)
154 for c in klass.ancestors
155 if mes = @MessageMap[[c,exp]]
157 m = klass.instance_eval('"' + mes + '"')
164 alias message e2mm_message
167 E2MM.def_exception(E2MM,
168 :ErrNotRegisteredException,
169 "not registerd exception(%s)")