Skip to content

Commit 8324688

Browse files
authored
Merge pull request #103 from code4lib/client-named-exceptions
use named exceptions in OAI::Response fixes #50
2 parents 39a8937 + 9458aa4 commit 8324688

File tree

5 files changed

+50
-42
lines changed

5 files changed

+50
-42
lines changed

lib/oai/client/response.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def initialize(doc, &resumption_block)
4646
end
4747
end
4848
end
49-
raise OAI::Exception.new(message, code)
49+
raise OAI::Exception.for(message: message, code: code)
5050
end
5151

5252
end

lib/oai/exception.rb

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
7583
end

test/client/tc_exception.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_xml_error
1818

1919
def test_oai_error
2020
client = OAI::Client.new 'http://localhost:3333/oai'
21-
assert_raises(OAI::Exception) do
21+
assert_raises(OAI::ResumptionTokenException) do
2222
client.list_identifiers :resumption_token => 'bogus'
2323
end
2424
end

test/client/tc_get_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_missing_identifier
2525
begin
2626
client.get_record :metadata_prefix => 'oai_dc'
2727
flunk 'invalid get_record did not throw OAI::Exception'
28-
rescue OAI::Exception => e
28+
rescue OAI::ArgumentException => e
2929
assert_match /The request includes illegal arguments/, e.to_s
3030
end
3131
end

test/client/tc_libxml.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def test_oai_exception
77

88
uri = 'http://localhost:3333/oai'
99
client = OAI::Client.new uri, :parser => 'libxml'
10-
assert_raises(OAI::Exception) {client.get_record(:identifier => 'nosuchid')}
10+
assert_raises(OAI::IdException) {client.get_record(:identifier => 'nosuchid')}
1111
end
1212

1313
def test_list_records

0 commit comments

Comments
 (0)