Commit c0d9cb1e by Rocky Duan

use argument error

parent 43e3c350
class ValueError < Exception
end
......@@ -24,22 +24,21 @@ helpers do
when "other"
Commentable.find(params["source_id"])
else
raise ValueError, "Source type must be 'user', 'thread' or 'other'"
raise ArgumentError, "Source type must be 'user', 'thread' or 'other'"
end
end
def vote_for(obj)
raise ValueError, "User id is required" unless user
raise ValueError, "Value is required" unless params["value"]
raise ValueError, "Value is invalid" unless %w[up down].include? params["value"]
raise ArgumentError, "User id is required" unless user
raise ArgumentError, "Value is required" unless params["value"]
raise ArgumentError, "Value is invalid" unless %w[up down].include? params["value"]
user.vote(obj, params["value"].to_sym)
obj.reload.to_hash.to_json
end
def undo_vote_for(obj)
raise ValueError, "must provide user id" unless user
raise ArgumentError, "must provide user id" unless user
user.unvote(obj)
obj.reload.to_hash.to_json
end
end
......@@ -59,7 +59,7 @@ class User
def subscribe(source)
if source._id == self._id and source.class == self.class
raise ValueError, "Cannot follow oneself"
raise ArgumentError, "Cannot follow oneself"
else
Subscription.find_or_create_by(subscriber_id: self._id.to_s, source_id: source._id.to_s, source_type: source.class.to_s)
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment