From 438fc7294d6ba5900510ccf6d55558995ce3f116 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sun, 27 Feb 2022 19:03:04 -0800 Subject: [PATCH] Add custom errors --- app/controllers/api/v1/api_controller.rb | 109 +++++++++--------- .../api/v1/FavoriteAlreadyExistsError.rb | 22 ++++ app/errors/api/v1/SameFavoriteUserError.rb | 22 ++++ app/errors/api/v1/UnauthorizedError.rb | 22 ++++ 4 files changed, 123 insertions(+), 52 deletions(-) create mode 100644 app/errors/api/v1/FavoriteAlreadyExistsError.rb create mode 100644 app/errors/api/v1/SameFavoriteUserError.rb create mode 100644 app/errors/api/v1/UnauthorizedError.rb diff --git a/app/controllers/api/v1/api_controller.rb b/app/controllers/api/v1/api_controller.rb index 871acbf..97cdd8a 100644 --- a/app/controllers/api/v1/api_controller.rb +++ b/app/controllers/api/v1/api_controller.rb @@ -1,68 +1,73 @@ -class Api::V1::ApiController < ActionController::API -##### Doorkeeper - include Doorkeeper::Rails::Helpers +module Api::V1 + class ApiController < ActionController::API + ##### Doorkeeper + include Doorkeeper::Rails::Helpers -##### Errors - rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response - rescue_from ActiveRecord::RecordNotDestroyed, with: :render_unprocessable_entity_response - rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response - rescue_from ActiveRecord::RecordNotSaved, with: :render_unprocessable_entity_response - rescue_from ActiveRecord::RecordNotUnique, with: :render_unprocessable_entity_response - rescue_from ActionController::ParameterMissing, with: :render_unprocessable_entity_response + ##### Errors + rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response + rescue_from ActiveRecord::RecordNotDestroyed, with: :render_unprocessable_entity_response + rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response + rescue_from ActiveRecord::RecordNotSaved, with: :render_unprocessable_entity_response + rescue_from ActiveRecord::RecordNotUnique, with: :render_unprocessable_entity_response + rescue_from Api::V1::SameFavoriteUserError, with: :render_unprocessable_entity_response + rescue_from Api::V1::FavoriteAlreadyExistsError, with: :render_unprocessable_entity_response + rescue_from Api::V1::UnauthorizedError, with: :render_unauthorized_response + rescue_from ActionController::ParameterMissing, with: :render_unprocessable_entity_response -##### Hooks - before_action :current_user - before_action :set_default_content_type + ##### Hooks + before_action :current_user + before_action :set_default_content_type -##### Responders - respond_to :json + ##### Responders + respond_to :json -##### Methods - # Assign the current user if the Doorkeeper token isn't nil, then - # update the current user's last seen datetime and last IP address - # before returning - def current_user - @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token - @current_user.update_last_ip_and_last_seen!(request.remote_ip) if @current_user + ##### Methods + # Assign the current user if the Doorkeeper token isn't nil, then + # update the current user's last seen datetime and last IP address + # before returning + def current_user + @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token + @current_user.update_last_ip_and_last_seen!(request.remote_ip) if @current_user - return @current_user - end + return @current_user + end - # Set the response content-type - def set_content_type(content_type) - response.headers["Content-Type"] = content_type - end + # Set the response content-type + def set_content_type(content_type) + response.headers["Content-Type"] = content_type + end - # Set the default response content-type to application/javascript - # with a UTF-8 charset - def set_default_content_type - set_content_type("application/javascript; charset=utf-8") - end + # Set the default response content-type to application/javascript + # with a UTF-8 charset + def set_default_content_type + set_content_type("application/javascript; charset=utf-8") + end - def current_user - @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token + def current_user + @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token - return @current_user - end + return @current_user + end - ### Error response methods - def render_unprocessable_entity_response(exception) - @exception = exception - render action: 'errors', status: :unprocessable_entity - end + ### Error response methods + def render_unprocessable_entity_response(exception) + @exception = exception + render action: 'errors', status: :unprocessable_entity + end - def render_not_found_response - response = { errors: [{ message: "Record could not be found.", code: "not_found" }]} - render 'not_found', status: :not_found - end + def render_not_found_response + response = { errors: [{ message: "Record could not be found.", code: "not_found" }]} + render 'not_found', status: :not_found + end - def render_unauthorized_response - render action: 'errors', status: :unauthorized - end + def render_unauthorized_response + render action: 'errors', status: :unauthorized + end - private + private - def restrict_access - raise UnauthorizedError unless current_user + def restrict_access + raise UnauthorizedError unless current_user + end end end \ No newline at end of file diff --git a/app/errors/api/v1/FavoriteAlreadyExistsError.rb b/app/errors/api/v1/FavoriteAlreadyExistsError.rb new file mode 100644 index 0000000..8c7ca73 --- /dev/null +++ b/app/errors/api/v1/FavoriteAlreadyExistsError.rb @@ -0,0 +1,22 @@ +module Api::V1 + class FavoriteAlreadyExistsError < StandardError + def http_status + 422 + end + + def code + "favorite_already_exists" + end + + def message + "This user has favorited this party already" + end + + def to_hash + { + message: message, + code: code + } + end + end +end diff --git a/app/errors/api/v1/SameFavoriteUserError.rb b/app/errors/api/v1/SameFavoriteUserError.rb new file mode 100644 index 0000000..b948c1a --- /dev/null +++ b/app/errors/api/v1/SameFavoriteUserError.rb @@ -0,0 +1,22 @@ +module Api::V1 + class SameFavoriteUserError < StandardError + def http_status + 422 + end + + def code + "same_favorite_user" + end + + def message + "Users cannot favorite their own parties" + end + + def to_hash + { + message: message, + code: code + } + end + end +end diff --git a/app/errors/api/v1/UnauthorizedError.rb b/app/errors/api/v1/UnauthorizedError.rb new file mode 100644 index 0000000..842c178 --- /dev/null +++ b/app/errors/api/v1/UnauthorizedError.rb @@ -0,0 +1,22 @@ +module Api::V1 + class UnauthorizedError < StandardError + def http_status + 401 + end + + def code + "unauthorized" + end + + def message + "User is not allowed to modify that resource" + end + + def to_hash + { + message: message, + code: code + } + end + end +end