@@ -4,72 +4,80 @@ module OAI
4
4
# messages will be wrapped in an XML response to the client.
5
5
6
6
class Exception < RuntimeError
7
+ CODE = nil
8
+ MESSAGE = nil
9
+
7
10
attr_reader :code
8
11
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
12
25
end
13
26
end
14
27
15
28
class ArgumentException < Exception
16
- def initialize ( )
17
- super ( 'The request includes ' \
29
+ CODE = 'badArgument'
30
+ MESSAGE = 'The request includes ' \
18
31
'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 )
22
34
end
23
35
24
36
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 )
30
41
end
31
42
32
43
class FormatException < Exception
33
- def initialize ( )
34
- super ( 'The metadata format identified by ' \
44
+ CODE = 'cannotDisseminateFormat'
45
+ MESSAGE = 'The metadata format identified by ' \
35
46
'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 )
38
49
end
39
50
40
51
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 )
45
56
end
46
57
47
58
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 )
53
63
end
54
64
55
65
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 )
60
70
end
61
71
62
72
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 )
66
76
end
67
77
68
78
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 )
73
82
end
74
-
75
83
end
0 commit comments