Skip to content

Throw useful error message when URL missing scheme #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-04-02 09:32:03 UTC using RuboCop version 1.75.1.
# on 2025-04-12 16:19:21 UTC using RuboCop version 1.75.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -9,7 +9,7 @@
# Offense count: 1
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 21
Max: 24

# Offense count: 4
# Configuration parameters: CountComments, CountAsOne.
Expand All @@ -19,7 +19,7 @@ Metrics/ClassLength:
# Offense count: 4
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 15
Max: 18

# Offense count: 3
# Configuration parameters: Max, CountKeywordArgs.
Expand Down
4 changes: 4 additions & 0 deletions lib/meilisearch/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
response = http_method.call(@base_url + relative_path, request_config)
rescue Errno::ECONNREFUSED, Errno::EPIPE => e
raise CommunicationError, e.message
rescue URI::InvalidURIError => e
raise CommunicationError, "Client URL missing scheme/protocol. Did you mean https://#{@base_url}" unless @base_url =~ %r{^\w+://}

raise e

Check warning on line 124 in lib/meilisearch/http_request.rb

View check run for this annotation

Codecov / codecov/patch

lib/meilisearch/http_request.rb#L124

Added line #L124 was not covered by tests
rescue Net::OpenTimeout, Net::ReadTimeout => e
attempts += 1
raise TimeoutError, e.message unless attempts <= max_retries && safe_to_retry?(config[:method_type], e)
Expand Down
9 changes: 9 additions & 0 deletions spec/meilisearch/client/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@
end.to raise_error(Meilisearch::InvalidDocumentId)
end
end

context 'when url is missing protocol' do
it 'throws a CommunicationError with a useful message' do
expect do
c = Meilisearch::Client.new('localhost:7700')
c.health
end.to raise_error(Meilisearch::CommunicationError).with_message(/protocol/)
end
end
end