diff --git a/app/controllers/api/v1/api_controller.rb b/app/controllers/api/v1/api_controller.rb index 1460919..79e13a9 100644 --- a/app/controllers/api/v1/api_controller.rb +++ b/app/controllers/api/v1/api_controller.rb @@ -16,7 +16,7 @@ module Api::V1 rescue_from Api::V1::UnauthorizedError, with: :render_unauthorized_response rescue_from ActionController::ParameterMissing, with: :render_unprocessable_entity_response - rescue_from StandardError do |e| + rescue_from GranblueError do |e| render_error(e) end @@ -50,7 +50,11 @@ module Api::V1 ### Error response methods def render_error(error) - render action: 'errors', json: error.to_hash, status: error.http_status + if error + render action: 'errors', json: error.to_hash, status: error.http_status + else + render action: 'errors' + end end def render_unprocessable_entity_response(exception) diff --git a/app/errors/api/v1/FavoriteAlreadyExistsError.rb b/app/errors/api/v1/FavoriteAlreadyExistsError.rb index 8c7ca73..e0ac7b7 100644 --- a/app/errors/api/v1/FavoriteAlreadyExistsError.rb +++ b/app/errors/api/v1/FavoriteAlreadyExistsError.rb @@ -1,22 +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 + class FavoriteAlreadyExistsError < GranblueError + 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/GranblueError.rb b/app/errors/api/v1/GranblueError.rb new file mode 100644 index 0000000..dec43a1 --- /dev/null +++ b/app/errors/api/v1/GranblueError.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Api + module V1 + # This is the base error that we inherit from for application errors + class GranblueError < StandardError + def initialize(data) + @data = data + end + + def http_status + 422 + end + + def code + 'granblue_error' + end + + def message + 'Something went wrong' + end + + def to_hash + { + message: message, + code: code + } + end + end + end +end diff --git a/app/errors/api/v1/IncompatibleSkillError.rb b/app/errors/api/v1/IncompatibleSkillError.rb index bd018d2..bd8c808 100644 --- a/app/errors/api/v1/IncompatibleSkillError.rb +++ b/app/errors/api/v1/IncompatibleSkillError.rb @@ -1,9 +1,9 @@ module Api::V1 - class IncompatibleSkillError < StandardError + class IncompatibleSkillError < GranblueError def initialize(data) @data = data end - + def http_status 422 end diff --git a/app/errors/api/v1/NoJobProvidedError.rb b/app/errors/api/v1/NoJobProvidedError.rb index 1e94592..c27f48a 100644 --- a/app/errors/api/v1/NoJobProvidedError.rb +++ b/app/errors/api/v1/NoJobProvidedError.rb @@ -1,5 +1,5 @@ module Api::V1 - class NoJobProvidedError < StandardError + class NoJobProvidedError < GranblueError def http_status 422 end diff --git a/app/errors/api/v1/NoJobSkillProvidedError.rb b/app/errors/api/v1/NoJobSkillProvidedError.rb index 1e66e2d..60ad888 100644 --- a/app/errors/api/v1/NoJobSkillProvidedError.rb +++ b/app/errors/api/v1/NoJobSkillProvidedError.rb @@ -1,5 +1,5 @@ module Api::V1 - class NoJobSkillProvidedError < StandardError + class NoJobSkillProvidedError < GranblueError def http_status 422 end diff --git a/app/errors/api/v1/SameFavoriteUserError.rb b/app/errors/api/v1/SameFavoriteUserError.rb index b948c1a..6fbcc79 100644 --- a/app/errors/api/v1/SameFavoriteUserError.rb +++ b/app/errors/api/v1/SameFavoriteUserError.rb @@ -1,22 +1,20 @@ -module Api::V1 - class SameFavoriteUserError < StandardError - def http_status - 422 - end +module Api + module V1 + class SameFavoriteUserError < GranblueError + def code + 'same_favorite_user' + end - def code - "same_favorite_user" - end + def message + 'Users cannot favorite their own parties' + end - def message - "Users cannot favorite their own parties" - end - - def to_hash - { - message: message, - code: code - } - end + def to_hash + { + message: message, + code: code + } + end end + end end diff --git a/app/errors/api/v1/TooManySkillsOfTypeError.rb b/app/errors/api/v1/TooManySkillsOfTypeError.rb index 2323192..3d0b4ab 100644 --- a/app/errors/api/v1/TooManySkillsOfTypeError.rb +++ b/app/errors/api/v1/TooManySkillsOfTypeError.rb @@ -2,15 +2,7 @@ module Api module V1 - class TooManySkillsOfTypeError < StandardError - def initialize(data) - @data = data - end - - def http_status - 422 - end - + class TooManySkillsOfTypeError < GranblueError def code 'too_many_skills_of_type' end