Skip to content

Commit

Permalink
Error handling validated models
Browse files Browse the repository at this point in the history
  • Loading branch information
ztratify committed Dec 2, 2020
1 parent ac83901 commit 93697ca
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/graphql/mutations/create_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def resolve(description: nil, url: nil)
url: url,
user: context[:current_user]
)
rescue ActiveRecord::RecordInvalid => error
GraphQL::ExecutionError.new("Invalid input: #{error.record.errors.full_messages.join(', ')}")
end
end
end
3 changes: 3 additions & 0 deletions app/models/link.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Link < ApplicationRecord
belongs_to :user, optional: true # Prevent ActiveRecord::RecordInvalid

validates :url, presence: true, length: { minimum: 5 }
validates :description, presence: true, length: { minimum: 5 }

has_many :votes
end

0 comments on commit 93697ca

Please sign in to comment.