Skip to content
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

Errors should have the variables as properties (#819) #823

Merged
merged 1 commit into from
Mar 8, 2022
Merged
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
37 changes: 28 additions & 9 deletions src/avram/errors.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,53 @@ module Avram
# Raised when trying to access a record that was not preloaded and lazy load
# is disabled.
class LazyLoadError < AvramError
def initialize(model : String, association : String)
getter model
getter association

def initialize(@model : String, @association : String)
super "#{association} for #{model} must be preloaded with 'preload_#{association}'"
end
end

# Raised when Avram cannot find a record by given id
class RecordNotFoundError < AvramError
def initialize(model : TableName, id : String)
getter model
getter id = nil
getter query = nil

def initialize(@model : TableName, @id : String)
super "Could not find #{model} with id of #{id}"
end

def initialize(model : TableName, query : Symbol)
def initialize(@model : TableName, @query : Symbol)
super "Could not find #{query} record in #{model}"
end
end

class MissingRequiredAssociationError < AvramError
def initialize(model : Avram::Model.class, association : Avram::Model.class)
getter model
getter association

def initialize(@model : Avram::Model.class, @association : Avram::Model.class)
super "Expected #{model} to have an association with #{association} but one was not found."
end
end

# Raised when a validation is expecting an impossible constraint
class ImpossibleValidation < AvramError
def initialize(attribute : Symbol, message = "an impossible validation")
getter attribute

def initialize(@attribute : Symbol, message = "an impossible validation")
super "Validation for #{attribute} can never satisfy #{message}"
end
end

# Raised when using the create!, update!, or delete! methods on an operation when it does not have the proper attributes
class InvalidOperationError < AvramError
class InvalidOperationError(T) < AvramError
getter operation
getter errors : Hash(Symbol, Array(String))

def initialize(operation)
def initialize(@operation : T)
message = String.build do |string|
string << "Could not perform #{operation.class.name}."
string << "\n"
Expand All @@ -71,7 +84,10 @@ module Avram
class ConnectionError < AvramError
DEFAULT_PG_PORT = 5432

def initialize(connection_details : URI, database_class : Avram::Database.class)
getter connection_details
getter database_class

def initialize(@connection_details : URI, @database_class : Avram::Database.class)
error = String.build do |message|
message << "#{database_class.name}: Failed to connect to database '#{connection_details.path.try(&.[1..-1])}' with username '#{connection_details.user}'.\n"
message << "Try this..."
Expand Down Expand Up @@ -144,7 +160,10 @@ module Avram
end

class FailedMigration < AvramError
def initialize(@migration : String, statements : Array(String), cause : Exception)
getter migration
getter statements

def initialize(@migration : String, @statements : Array(String), cause : Exception)
super(message(statements), cause)
end

Expand Down