Skip to content
Merged
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
8 changes: 4 additions & 4 deletions src/avram/connection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class Avram::Connection

def connect_listen(*channels : String, &block : PQ::Notification ->) : Nil
PG.connect_listen(credentials.url, *channels, &block)
rescue DB::ConnectionRefused
raise ConnectionError.new(credentials.uri, database_class: @database_class)
rescue e : DB::ConnectionRefused
raise ConnectionError.new(credentials.uri, database_class: @database_class, cause: e)
end

def try_connection! : DB::Database
DB.open(credentials.url)
rescue DB::ConnectionRefused
raise ConnectionError.new(credentials.uri, database_class: @database_class)
rescue e : DB::ConnectionRefused
raise ConnectionError.new(credentials.uri, database_class: @database_class, cause: e)
end
end
8 changes: 4 additions & 4 deletions src/avram/errors.cr
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module Avram
getter connection_details : URI
getter database_class : Avram::Database.class

def initialize(@connection_details : URI, @database_class : Avram::Database.class)
def initialize(@connection_details : URI, @database_class : Avram::Database.class, cause = nil)
error = String.build do |message|
message << database_class.name << ": Failed to connect to database '"
message << connection_details.path.try(&.[1..-1]) << "' with username '"
Expand All @@ -108,7 +108,7 @@ module Avram
end
end

super error
super error, cause
end
end

Expand Down Expand Up @@ -139,8 +139,8 @@ module Avram
end

class PGNotRunningError < AvramError
def initialize(original_message : String)
super <<-ERROR
def initialize(original_message : String, cause = nil)
super <<-ERROR, cause
It looks like Postgres is not running.

Message from Postgres:
Expand Down
7 changes: 4 additions & 3 deletions src/avram/migrator/runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Avram::Migrator::Runner
rescue e : DB::ConnectionRefused
message = e.message.to_s
if message.blank?
raise ConnectionError.new(URI.parse(credentials.url_without_query_params), Avram.settings.database_to_migrate)
raise ConnectionError.new(URI.parse(credentials.url_without_query_params), Avram.settings.database_to_migrate, cause: e)
else
raise e
end
Expand All @@ -98,7 +98,7 @@ class Avram::Migrator::Runner
puts "Already created #{self.db_name.colorize(:green)}"
end
elsif message.includes?("Cannot establish connection")
raise PGNotRunningError.new(message)
raise PGNotRunningError.new(message, cause: e)
else
raise e
end
Expand Down Expand Up @@ -264,6 +264,7 @@ class Avram::Migrator::Runner
yield
end
rescue e : DB::ConnectionRefused
raise "Unable to connect to the database. Please check your configuration.".colorize(:red).to_s
message = "Unable to connect to the database. Please check your configuration.".colorize(:red).to_s
raise Exception.new(message, cause: e)
end
end
2 changes: 1 addition & 1 deletion src/avram/queryable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ module Avram::Queryable(T)
direction = Avram::OrderBy::Direction.parse(direction.to_s)
order_by(Avram::OrderBy.new(column, direction, null_sorting))
rescue e : ArgumentError
raise "#{e.message}. Accepted values are: :asc, :desc"
raise ArgumentError.new("#{e.message}. Accepted values are: :asc, :desc")
end

def order_by(order : Avram::OrderByClause) : self
Expand Down
4 changes: 2 additions & 2 deletions src/avram/tasks/db/verify_connection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Db::VerifyConnection < BaseTask
DB.open(Avram.settings.database_to_migrate.settings.credentials.url) do |_db|
output.puts "✔ Connection verified" unless quiet?
end
rescue Avram::ConnectionError | DB::ConnectionRefused
raise <<-ERROR
rescue e : Avram::ConnectionError | DB::ConnectionRefused
raise Exception.new <<-ERROR, cause: e
Unable to connect to Postgres for database '#{Avram.settings.database_to_migrate}'.

This is what we tried to connect to:
Expand Down
Loading