1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
# e2mmap.rb - for ruby 1.1
|
1999-08-13 09:45:20 +04:00
|
|
|
# $Release Version: 2.0$
|
|
|
|
# $Revision: 1.10 $
|
1998-01-16 15:13:05 +03:00
|
|
|
# by Keiju ISHITSUKA
|
|
|
|
#
|
|
|
|
# --
|
1999-01-20 07:59:39 +03:00
|
|
|
# Usage:
|
|
|
|
#
|
1999-08-13 09:45:20 +04:00
|
|
|
# U1)
|
1999-01-20 07:59:39 +03:00
|
|
|
# class Foo
|
2000-09-22 06:37:13 +04:00
|
|
|
# extend Exception2MessageMapper
|
1999-08-13 09:45:20 +04:00
|
|
|
# def_e2message ExistingExceptionClass, "message..."
|
1999-01-20 07:59:39 +03:00
|
|
|
# def_exception :NewExceptionClass, "message..."[, superclass]
|
1999-08-13 09:45:20 +04:00
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# U2)
|
|
|
|
# module Error
|
2000-09-22 06:37:13 +04:00
|
|
|
# extend Exception2MessageMapper
|
1999-01-20 07:59:39 +03:00
|
|
|
# def_e2meggage ExistingExceptionClass, "message..."
|
1999-08-13 09:45:20 +04:00
|
|
|
# def_exception :NewExceptionClass, "message..."[, superclass]
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
# class Foo
|
2000-09-22 06:37:13 +04:00
|
|
|
# include Error
|
1999-08-13 09:45:20 +04:00
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# foo = Foo.new
|
|
|
|
# foo.Fail ....
|
|
|
|
#
|
|
|
|
# U3)
|
|
|
|
# module Error
|
2000-09-22 06:37:13 +04:00
|
|
|
# extend Exception2MessageMapper
|
1999-08-13 09:45:20 +04:00
|
|
|
# def_e2message ExistingExceptionClass, "message..."
|
|
|
|
# def_exception :NewExceptionClass, "message..."[, superclass]
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
# class Foo
|
|
|
|
# extend Exception2MessageMapper
|
|
|
|
# include Error
|
1999-01-20 07:59:39 +03:00
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Foo.Fail NewExceptionClass, arg...
|
|
|
|
# Foo.Fail ExistingExceptionClass, arg...
|
1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
#
|
1999-08-13 09:45:20 +04:00
|
|
|
module Exception2MessageMapper
|
|
|
|
@RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
|
|
|
|
|
|
|
|
E2MM = Exception2MessageMapper
|
|
|
|
|
|
|
|
def E2MM.extend_object(cl)
|
|
|
|
super
|
2007-12-15 09:03:22 +03:00
|
|
|
cl.bind(self) unless cl < E2MM
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
def bind(cl)
|
|
|
|
self.module_eval %[
|
|
|
|
def Raise(err = nil, *rest)
|
2002-10-02 20:45:35 +04:00
|
|
|
Exception2MessageMapper.Raise(self.class, err, *rest)
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
|
|
|
alias Fail Raise
|
|
|
|
|
2001-05-06 19:06:00 +04:00
|
|
|
def self.included(mod)
|
1999-08-13 09:45:20 +04:00
|
|
|
mod.extend Exception2MessageMapper
|
|
|
|
end
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Fail(err, *rest)
|
2000-03-07 11:37:59 +03:00
|
|
|
# err: exception
|
|
|
|
# rest: message arguments
|
1999-08-13 09:45:20 +04:00
|
|
|
#
|
|
|
|
def Raise(err = nil, *rest)
|
|
|
|
E2MM.Raise(self, err, *rest)
|
|
|
|
end
|
|
|
|
alias Fail Raise
|
2008-01-09 17:23:34 +03:00
|
|
|
alias fail Raise
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
# def_e2message(c, m)
|
|
|
|
# c: exception
|
|
|
|
# m: message_form
|
2000-03-07 11:37:59 +03:00
|
|
|
# define exception c with message m.
|
1999-08-13 09:45:20 +04:00
|
|
|
#
|
|
|
|
def def_e2message(c, m)
|
|
|
|
E2MM.def_e2message(self, c, m)
|
|
|
|
end
|
|
|
|
|
2000-09-22 06:37:13 +04:00
|
|
|
# def_exception(n, m, s)
|
1999-08-13 09:45:20 +04:00
|
|
|
# n: exception_name
|
|
|
|
# m: message_form
|
2000-03-07 11:37:59 +03:00
|
|
|
# s: superclass(default: StandardError)
|
|
|
|
# define exception named ``c'' with message m.
|
1999-08-13 09:45:20 +04:00
|
|
|
#
|
|
|
|
def def_exception(n, m, s = StandardError)
|
|
|
|
E2MM.def_exception(self, n, m, s)
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#
|
|
|
|
# Private definitions.
|
|
|
|
#
|
|
|
|
# {[class, exp] => message, ...}
|
|
|
|
@MessageMap = {}
|
|
|
|
|
2008-06-16 09:46:28 +04:00
|
|
|
# E2MM.def_e2message(k, e, m)
|
2000-03-07 11:37:59 +03:00
|
|
|
# k: class to define exception under.
|
1999-08-13 09:45:20 +04:00
|
|
|
# e: exception
|
|
|
|
# m: message_form
|
2000-03-07 11:37:59 +03:00
|
|
|
# define exception c with message m.
|
1999-08-13 09:45:20 +04:00
|
|
|
#
|
|
|
|
def E2MM.def_e2message(k, c, m)
|
|
|
|
E2MM.instance_eval{@MessageMap[[k, c]] = m}
|
|
|
|
c
|
|
|
|
end
|
|
|
|
|
2000-09-22 06:37:13 +04:00
|
|
|
# E2MM.def_exception(k, n, m, s)
|
2000-03-07 11:37:59 +03:00
|
|
|
# k: class to define exception under.
|
1999-08-13 09:45:20 +04:00
|
|
|
# n: exception_name
|
|
|
|
# m: message_form
|
2000-03-07 11:37:59 +03:00
|
|
|
# s: superclass(default: StandardError)
|
|
|
|
# define exception named ``c'' with message m.
|
1999-08-13 09:45:20 +04:00
|
|
|
#
|
|
|
|
def E2MM.def_exception(k, n, m, s = StandardError)
|
|
|
|
n = n.id2name if n.kind_of?(Fixnum)
|
|
|
|
e = Class.new(s)
|
|
|
|
E2MM.instance_eval{@MessageMap[[k, e]] = m}
|
|
|
|
k.const_set(n, e)
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
# Fail(klass, err, *rest)
|
2000-03-07 11:37:59 +03:00
|
|
|
# klass: class to define exception under.
|
|
|
|
# err: exception
|
|
|
|
# rest: message arguments
|
1999-08-13 09:45:20 +04:00
|
|
|
#
|
|
|
|
def E2MM.Raise(klass = E2MM, err = nil, *rest)
|
|
|
|
if form = e2mm_message(klass, err)
|
2007-12-15 09:03:22 +03:00
|
|
|
b = $@.nil? ? caller(1) : $@
|
1999-08-13 09:45:20 +04:00
|
|
|
#p $@
|
|
|
|
#p __FILE__
|
2007-12-15 09:03:22 +03:00
|
|
|
b.shift if b[0] =~ /^#{Regexp.quote(__FILE__)}:/
|
|
|
|
raise err, sprintf(form, *rest), b
|
1999-08-13 09:45:20 +04:00
|
|
|
else
|
|
|
|
E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class <<E2MM
|
|
|
|
alias Fail Raise
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
def E2MM.e2mm_message(klass, exp)
|
|
|
|
for c in klass.ancestors
|
|
|
|
if mes = @MessageMap[[c,exp]]
|
1999-09-18 08:48:51 +04:00
|
|
|
#p mes
|
1999-08-13 09:45:20 +04:00
|
|
|
m = klass.instance_eval('"' + mes + '"')
|
|
|
|
return m
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
class <<self
|
|
|
|
alias message e2mm_message
|
|
|
|
end
|
|
|
|
|
|
|
|
E2MM.def_exception(E2MM,
|
|
|
|
:ErrNotRegisteredException,
|
|
|
|
"not registerd exception(%s)")
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
|