Skip to content

Commit

Permalink
More descriptive error messages (#108)
Browse files Browse the repository at this point in the history
* more descriptive error messages

* fix typos
  • Loading branch information
Sam-Robin authored Sep 10, 2024
1 parent f8b6574 commit b642d61
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 14 additions & 2 deletions lib/discard/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,23 @@ def undiscard!
private

def _raise_record_not_discarded
raise ::Discard::RecordNotDiscarded.new("Failed to discard the record", self)
raise ::Discard::RecordNotDiscarded.new(discarded_fail_message, self)
end

def _raise_record_not_undiscarded
raise ::Discard::RecordNotUndiscarded.new("Failed to undiscard the record", self)
raise ::Discard::RecordNotUndiscarded.new(undiscarded_fail_message, self)
end

def discarded_fail_message
return "A discarded record cannot be discarded" if discarded?

"Failed to discard the record"
end

def undiscarded_fail_message
return "An undiscarded record cannot be undiscarded" if undiscarded?

"Failed to undiscard the record"
end
end
end
10 changes: 5 additions & 5 deletions spec/discard/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
it "raises Discard::RecordNotUndiscarded" do
expect {
post.undiscard!
}.to raise_error(Discard::RecordNotUndiscarded)
}.to raise_error(Discard::RecordNotUndiscarded, 'An undiscarded record cannot be undiscarded')
end
end
end
Expand Down Expand Up @@ -153,7 +153,7 @@
it "raises Discard::RecordNotDiscarded" do
expect {
post.discard!
}.to raise_error(Discard::RecordNotDiscarded)
}.to raise_error(Discard::RecordNotDiscarded, "A discarded record cannot be discarded")
end
end

Expand Down Expand Up @@ -616,7 +616,7 @@ def abort_callback
expect(post).to receive(:do_before_discard) { abort_callback }
expect {
post.discard!
}.to raise_error(Discard::RecordNotDiscarded)
}.to raise_error(Discard::RecordNotDiscarded, "Failed to discard the record")
end
end
end
Expand Down Expand Up @@ -677,11 +677,11 @@ def abort_callback
end

describe '#undiscard!' do
it "raises Discard::RecordNotDiscarded" do
it "raises Discard::RecordNotUndiscarded" do
expect(post).to receive(:do_before_undiscard) { abort_callback }
expect {
post.undiscard!
}.to raise_error(Discard::RecordNotUndiscarded)
}.to raise_error(Discard::RecordNotUndiscarded, "Failed to undiscard the record")
end
end
end
Expand Down

0 comments on commit b642d61

Please sign in to comment.