Skip to content

Commit

Permalink
handle exceptions in application controller
Browse files Browse the repository at this point in the history
  • Loading branch information
citin committed Jul 12, 2019
1 parent 1831433 commit d3c1a2a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
11 changes: 0 additions & 11 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ class Api::V1::UsersController < ApplicationController
before_action :set_user
before_action :set_purchase_option, only: [:purchase]

rescue_from User::AlreadyPurchasedError, with: :render_method_not_allowed
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response

def render_not_found_response(exception)
render json: { error: exception.message }, status: :not_found
end

def render_method_not_allowed(exception)
render json: { error: exception.message }, status: 405
end

# POST /api/v1/users/:id/purchase
# Params: { "purchase_option_id": 1 }
def purchase
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
class ApplicationController < ActionController::API
rescue_from User::AlreadyPurchasedError, with: :render_method_not_allowed
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response

def render_not_found_response(exception)
render json: { error: exception.message }, status: :not_found
end

def render_method_not_allowed(exception)
render json: { error: exception.message }, status: 405
end
end

0 comments on commit d3c1a2a

Please sign in to comment.