Skip to content

Commit

Permalink
Merge pull request #605 from estolfo/uri-errors
Browse files Browse the repository at this point in the history
Put URI errors with the other error classes
  • Loading branch information
estolfo committed Apr 17, 2015
2 parents f0cc2d1 + f5baf15 commit 5b64dac
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 57 deletions.
2 changes: 2 additions & 0 deletions lib/mongo/error.rb
Expand Up @@ -73,6 +73,8 @@ class Error < StandardError
require 'mongo/error/invalid_replacement_document'
require 'mongo/error/invalid_signature'
require 'mongo/error/invalid_update_document'
require 'mongo/error/invalid_uri'
require 'mongo/error/invalid_uri_option'
require 'mongo/error/max_bson_size'
require 'mongo/error/max_message_size'
require 'mongo/error/multi_index_drop'
Expand Down
37 changes: 37 additions & 0 deletions lib/mongo/error/invalid_uri.rb
@@ -0,0 +1,37 @@
# Copyright (C) 2014-2015 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
class Error

# Exception that is raised when trying to parse a URI that does not match
# the specification.
#
# @since 2.0.0
class InvalidURI < Error

# Instantiate the new exception.
#
# @example Instantiate the exception.
# Mongo::Error::InvalidURI.new(uri)
#
# @since 2.0.0
def initialize(uri)
super("MongoDB URI must be in the following format: #{Mongo::URI::FORMAT}\n" +
"Please see the following URL for more information: #{Mongo::URI::HELP}\n" +
"Bad URI: #{uri}")
end
end
end
end
38 changes: 38 additions & 0 deletions lib/mongo/error/invalid_uri_option.rb
@@ -0,0 +1,38 @@
# Copyright (C) 2014-2015 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
class Error

# Raised if the URI is in the correct format but an option is provided that
# is not recognized.
#
# @since 2.0.0
class InvalidURIOption < Error

# Create the error.
#
# @example Create the error with the invalid option name.
# InvalidURIOption.new('nothing')
#
# @param [ String ] name The invalid option name.
#
# @since 2.0.0
def initialize(name)
super("Invalid option in URI: '#{name}'.\n" +
"Please see the following URL for more information: #{Mongo::URI::HELP}\n")
end
end
end
end
64 changes: 9 additions & 55 deletions lib/mongo/uri.rb
Expand Up @@ -80,6 +80,13 @@ class URI
# @since 2.0.0
URI = /#{SCHEME}#{CREDENTIALS}#{SERVERS}#{DATABASE}#{OPTIONS}/.freeze


# MongoDB URI format specification.
#
# @since 2.0.0
FORMAT = 'mongodb://[username:password@]host1[:port1][,host2[:port2]' +
',...[,hostN[:portN]]][/[database][?options]]'.freeze

# MongoDB URI (connection string) documentation url
#
# @since 2.0.0
Expand Down Expand Up @@ -117,7 +124,7 @@ class URI
# @since 2.0.0
def initialize(string)
@match = string.match(URI)
raise Invalid.new(string) unless @match
raise Error::InvalidURI.new(string) unless @match
end

# Get the servers provided in the URI.
Expand Down Expand Up @@ -203,65 +210,12 @@ def options
parsed_options.split('&').reduce({}) do |options, option|
key, value = option.split('=')
strategy = OPTION_MAP[key]
raise InvalidOption.new(key) if strategy.nil?
raise Error::InvalidURIOption.new(key) if strategy.nil?
add_option(strategy, value, options)
options
end
end

# Exception that is raised when trying to parse a URI that does not match
# the specification.
#
# @since 2.0.0
class Invalid < RuntimeError

# MongoDB URI format specification.
#
# @since 2.0.0
FORMAT = 'mongodb://[username:password@]host1[:port1][,host2[:port2]' +
',...[,hostN[:portN]]][/[database][?options]]'.freeze

# Creates a new instance of the BadURI error.
#
# @example Initialize the error.
# BadURI.new(uri)
#
# @param [ String ] uri The bad URI.
#
# @since 2.0.0
def initialize(uri)
super(message(uri))
end

private

def message(uri)
"MongoDB URI must be in the following format: #{FORMAT}\n" +
"Please see the following URL for more information: #{HELP}\n" +
"Bad URI: #{uri}"
end
end

# Raised if the URI is in the correct format but an option is provided that
# is not recognized.
#
# @since 2.0.0
class InvalidOption < RuntimeError

# Create the error.
#
# @example Create the error with the invalid option name.
# InvalidOption.new('nothing')
#
# @param [ String ] name The invalid option name.
#
# @since 2.0.0
def initialize(name)
super("Invalid option in URI: '#{name}'.\n" +
"Please see the following URL for more information: #{HELP}\n")
end
end

private

# Hash for storing map of URI option parameters to conversion strategies
Expand Down
4 changes: 2 additions & 2 deletions spec/mongo/uri_spec.rb
Expand Up @@ -8,7 +8,7 @@
context 'string is not uri' do
let(:string) { 'tyler' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::URI::Invalid)
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
end
Expand Down Expand Up @@ -510,7 +510,7 @@
it 'raises an exception' do
expect {
uri.options
}.to raise_error(Mongo::URI::InvalidOption)
}.to raise_error(Mongo::Error::InvalidURIOption)
end
end
end
Expand Down

0 comments on commit 5b64dac

Please sign in to comment.