Skip to content

Commit

Permalink
Add context to RecordNotSaved/RecordNotDestroyed error messages (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrsmk committed Apr 24, 2023
1 parent 2b56979 commit d9fca08
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion spec/granite/exceptions/record_invalid_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe Granite::RecordNotSaved do
Granite::RecordNotSaved
.new(Parent.name, parent)
.message
.should eq("Could not process Parent")
.should eq("Could not process Parent: Name cannot be blank")
end

it "should have a model" do
Expand Down
2 changes: 1 addition & 1 deletion spec/granite/exceptions/record_not_destroyed_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe Granite::RecordNotDestroyed do
Granite::RecordNotDestroyed
.new(Parent.name, parent)
.message
.should eq("Could not destroy Parent")
.should eq("Could not destroy Parent: Name cannot be blank")
end

it "should have a model" do
Expand Down
12 changes: 4 additions & 8 deletions src/granite/exceptions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ module Granite
class RecordNotSaved < ::Exception
getter model : Granite::Base

def initialize(class_name : String, model : Granite::Base)
super("Could not process #{class_name}")

@model = model
def initialize(class_name : String, @model : Granite::Base)
super("Could not process #{class_name}: #{model.errors.first.message}")
end
end

class RecordNotDestroyed < ::Exception
getter model : Granite::Base

def initialize(class_name : String, model : Granite::Base)
super("Could not destroy #{class_name}")

@model = model
def initialize(class_name : String, @model : Granite::Base)
super("Could not destroy #{class_name}: #{model.errors.first.message}")
end
end
end

0 comments on commit d9fca08

Please sign in to comment.