@@ -4,72 +4,80 @@ module OAI
44 # messages will be wrapped in an XML response to the client.
55
66 class Exception < RuntimeError
7+ CODE = nil
8+ MESSAGE = nil
9+
710 attr_reader :code
811
9- def initialize ( message , code = nil )
10- super ( message )
11- @code = code
12+ @@codes = { }
13+
14+ def self . register_exception_code ( code , exception_class )
15+ @@codes [ code ] = exception_class if exception_class . superclass == OAI ::Exception
16+ end
17+
18+ def self . for ( message : nil , code : nil )
19+ @@codes . fetch ( code , Exception ) . new ( message )
20+ end
21+
22+ def initialize ( message = nil , code = nil )
23+ super ( message || self . class ::MESSAGE )
24+ @code = code || self . class ::CODE
1225 end
1326 end
1427
1528 class ArgumentException < Exception
16- def initialize ( )
17- super ( 'The request includes ' \
29+ CODE = 'badArgument'
30+ MESSAGE = 'The request includes ' \
1831 'illegal arguments, is missing required arguments, includes a ' \
19- 'repeated argument, or values for arguments have an illegal syntax.' ,
20- 'badArgument' )
21- end
32+ 'repeated argument, or values for arguments have an illegal syntax.'
33+ register_exception_code ( CODE , self )
2234 end
2335
2436 class VerbException < Exception
25- def initialize ( )
26- super ( 'Value of the verb argument is not a legal OAI-PMH ' \
27- 'verb, the verb argument is missing, or the verb argument is repeated.' ,
28- 'badVerb' )
29- end
37+ CODE = 'badVerb'
38+ MESSAGE = 'Value of the verb argument is not a legal OAI-PMH ' \
39+ 'verb, the verb argument is missing, or the verb argument is repeated.'
40+ register_exception_code ( CODE , self )
3041 end
3142
3243 class FormatException < Exception
33- def initialize ( )
34- super ( 'The metadata format identified by ' \
44+ CODE = 'cannotDisseminateFormat'
45+ MESSAGE = 'The metadata format identified by ' \
3546 'the value given for the metadataPrefix argument is not supported ' \
36- 'by the item or by the repository.' , 'cannotDisseminateFormat' )
37- end
47+ 'by the item or by the repository.'
48+ register_exception_code ( CODE , self )
3849 end
3950
4051 class IdException < Exception
41- def initialize ( )
42- super ( 'The value of the identifier argument is ' \
43- 'unknown or illegal in this repository.' , 'idDoesNotExist' )
44- end
52+ CODE = 'idDoesNotExist'
53+ MESSAGE = 'The value of the identifier argument is ' \
54+ 'unknown or illegal in this repository.'
55+ register_exception_code ( CODE , self )
4556 end
4657
4758 class NoMatchException < Exception
48- def initialize ( )
49- super ( 'The combination of the values of the from, ' \
50- 'until, set and metadataPrefix arguments results in an empty list.' ,
51- 'noRecordsMatch' )
52- end
59+ CODE = 'noRecordsMatch'
60+ MESSAGE = 'The combination of the values of the from, ' \
61+ 'until, set and metadataPrefix arguments results in an empty list.'
62+ register_exception_code ( CODE , self )
5363 end
5464
5565 class MetadataFormatException < Exception
56- def initialize ( )
57- super ( 'There are no metadata formats available ' \
58- 'for the specified item.' , 'noMetadataFormats' )
59- end
66+ CODE = 'noMetadataFormats'
67+ MESSAGE = 'There are no metadata formats available ' \
68+ 'for the specified item.'
69+ register_exception_code ( CODE , self )
6070 end
6171
6272 class SetException < Exception
63- def initialize ( )
64- super ( 'This repository does not support sets.' , 'noSetHierarchy' )
65- end
73+ CODE = 'noSetHierarchy'
74+ MESSAGE = 'This repository does not support sets.'
75+ register_exception_code ( CODE , self )
6676 end
6777
6878 class ResumptionTokenException < Exception
69- def initialize ( )
70- super ( 'The value of the resumptionToken argument is invalid or expired.' ,
71- 'badResumptionToken' )
72- end
79+ CODE = 'badResumptionToken'
80+ MESSAGE = 'The value of the resumptionToken argument is invalid or expired.'
81+ register_exception_code ( CODE , self )
7382 end
74-
7583end
0 commit comments