REFACTOR: Rescue error at the specific spot that is raising the error.

This commit is contained in:
Guo Xiang Tan 2018-09-03 11:04:58 +08:00
parent 0fac6cdba9
commit 59c9051a2e
1 changed files with 6 additions and 4 deletions

View File

@ -118,10 +118,14 @@ class CategoriesController < ApplicationController
def create def create
guardian.ensure_can_create!(Category) guardian.ensure_can_create!(Category)
position = category_params.delete(:position) position = category_params.delete(:position)
@category = Category.create(category_params.merge(user: current_user)) @category =
begin
Category.new(category_params.merge(user: current_user))
rescue ArgumentError => e
return render json: { errors: [e.message] }, status: 422
end
if @category.save if @category.save
@category.move_to(position.to_i) if position @category.move_to(position.to_i) if position
@ -134,8 +138,6 @@ class CategoriesController < ApplicationController
else else
return render_json_error(@category) unless @category.save return render_json_error(@category) unless @category.save
end end
rescue ArgumentError => e
render json: { errors: [e.message] }, status: 422
end end
def update def update