Skip to content

Commit 5b64dac

Browse files
committed
Merge pull request #605 from estolfo/uri-errors
Put URI errors with the other error classes
2 parents f0cc2d1 + f5baf15 commit 5b64dac

File tree

5 files changed

+88
-57
lines changed

5 files changed

+88
-57
lines changed

lib/mongo/error.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class Error < StandardError
7373
require 'mongo/error/invalid_replacement_document'
7474
require 'mongo/error/invalid_signature'
7575
require 'mongo/error/invalid_update_document'
76+
require 'mongo/error/invalid_uri'
77+
require 'mongo/error/invalid_uri_option'
7678
require 'mongo/error/max_bson_size'
7779
require 'mongo/error/max_message_size'
7880
require 'mongo/error/multi_index_drop'

lib/mongo/error/invalid_uri.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (C) 2014-2015 MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
module Mongo
16+
class Error
17+
18+
# Exception that is raised when trying to parse a URI that does not match
19+
# the specification.
20+
#
21+
# @since 2.0.0
22+
class InvalidURI < Error
23+
24+
# Instantiate the new exception.
25+
#
26+
# @example Instantiate the exception.
27+
# Mongo::Error::InvalidURI.new(uri)
28+
#
29+
# @since 2.0.0
30+
def initialize(uri)
31+
super("MongoDB URI must be in the following format: #{Mongo::URI::FORMAT}\n" +
32+
"Please see the following URL for more information: #{Mongo::URI::HELP}\n" +
33+
"Bad URI: #{uri}")
34+
end
35+
end
36+
end
37+
end

lib/mongo/error/invalid_uri_option.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (C) 2014-2015 MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
module Mongo
16+
class Error
17+
18+
# Raised if the URI is in the correct format but an option is provided that
19+
# is not recognized.
20+
#
21+
# @since 2.0.0
22+
class InvalidURIOption < Error
23+
24+
# Create the error.
25+
#
26+
# @example Create the error with the invalid option name.
27+
# InvalidURIOption.new('nothing')
28+
#
29+
# @param [ String ] name The invalid option name.
30+
#
31+
# @since 2.0.0
32+
def initialize(name)
33+
super("Invalid option in URI: '#{name}'.\n" +
34+
"Please see the following URL for more information: #{Mongo::URI::HELP}\n")
35+
end
36+
end
37+
end
38+
end

lib/mongo/uri.rb

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ class URI
8080
# @since 2.0.0
8181
URI = /#{SCHEME}#{CREDENTIALS}#{SERVERS}#{DATABASE}#{OPTIONS}/.freeze
8282

83+
84+
# MongoDB URI format specification.
85+
#
86+
# @since 2.0.0
87+
FORMAT = 'mongodb://[username:password@]host1[:port1][,host2[:port2]' +
88+
',...[,hostN[:portN]]][/[database][?options]]'.freeze
89+
8390
# MongoDB URI (connection string) documentation url
8491
#
8592
# @since 2.0.0
@@ -117,7 +124,7 @@ class URI
117124
# @since 2.0.0
118125
def initialize(string)
119126
@match = string.match(URI)
120-
raise Invalid.new(string) unless @match
127+
raise Error::InvalidURI.new(string) unless @match
121128
end
122129

123130
# Get the servers provided in the URI.
@@ -203,65 +210,12 @@ def options
203210
parsed_options.split('&').reduce({}) do |options, option|
204211
key, value = option.split('=')
205212
strategy = OPTION_MAP[key]
206-
raise InvalidOption.new(key) if strategy.nil?
213+
raise Error::InvalidURIOption.new(key) if strategy.nil?
207214
add_option(strategy, value, options)
208215
options
209216
end
210217
end
211218

212-
# Exception that is raised when trying to parse a URI that does not match
213-
# the specification.
214-
#
215-
# @since 2.0.0
216-
class Invalid < RuntimeError
217-
218-
# MongoDB URI format specification.
219-
#
220-
# @since 2.0.0
221-
FORMAT = 'mongodb://[username:password@]host1[:port1][,host2[:port2]' +
222-
',...[,hostN[:portN]]][/[database][?options]]'.freeze
223-
224-
# Creates a new instance of the BadURI error.
225-
#
226-
# @example Initialize the error.
227-
# BadURI.new(uri)
228-
#
229-
# @param [ String ] uri The bad URI.
230-
#
231-
# @since 2.0.0
232-
def initialize(uri)
233-
super(message(uri))
234-
end
235-
236-
private
237-
238-
def message(uri)
239-
"MongoDB URI must be in the following format: #{FORMAT}\n" +
240-
"Please see the following URL for more information: #{HELP}\n" +
241-
"Bad URI: #{uri}"
242-
end
243-
end
244-
245-
# Raised if the URI is in the correct format but an option is provided that
246-
# is not recognized.
247-
#
248-
# @since 2.0.0
249-
class InvalidOption < RuntimeError
250-
251-
# Create the error.
252-
#
253-
# @example Create the error with the invalid option name.
254-
# InvalidOption.new('nothing')
255-
#
256-
# @param [ String ] name The invalid option name.
257-
#
258-
# @since 2.0.0
259-
def initialize(name)
260-
super("Invalid option in URI: '#{name}'.\n" +
261-
"Please see the following URL for more information: #{HELP}\n")
262-
end
263-
end
264-
265219
private
266220

267221
# Hash for storing map of URI option parameters to conversion strategies

spec/mongo/uri_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
context 'string is not uri' do
99
let(:string) { 'tyler' }
1010
it 'raises an error' do
11-
expect { uri }.to raise_error(Mongo::URI::Invalid)
11+
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
1212
end
1313
end
1414
end
@@ -510,7 +510,7 @@
510510
it 'raises an exception' do
511511
expect {
512512
uri.options
513-
}.to raise_error(Mongo::URI::InvalidOption)
513+
}.to raise_error(Mongo::Error::InvalidURIOption)
514514
end
515515
end
516516
end

0 commit comments

Comments
 (0)