Add GranblueError and move app errors to extend it

This commit is contained in:
Justin Edmund 2022-12-03 18:21:01 -08:00
parent 6ba5335668
commit a08421722f
8 changed files with 77 additions and 52 deletions

View file

@ -16,7 +16,7 @@ module Api::V1
rescue_from Api::V1::UnauthorizedError, with: :render_unauthorized_response rescue_from Api::V1::UnauthorizedError, with: :render_unauthorized_response
rescue_from ActionController::ParameterMissing, with: :render_unprocessable_entity_response rescue_from ActionController::ParameterMissing, with: :render_unprocessable_entity_response
rescue_from StandardError do |e| rescue_from GranblueError do |e|
render_error(e) render_error(e)
end end
@ -50,7 +50,11 @@ module Api::V1
### Error response methods ### Error response methods
def render_error(error) 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 end
def render_unprocessable_entity_response(exception) def render_unprocessable_entity_response(exception)

View file

@ -1,22 +1,22 @@
module Api::V1 module Api::V1
class FavoriteAlreadyExistsError < StandardError class FavoriteAlreadyExistsError < GranblueError
def http_status def http_status
422 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
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 end

View file

@ -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

View file

@ -1,5 +1,5 @@
module Api::V1 module Api::V1
class IncompatibleSkillError < StandardError class IncompatibleSkillError < GranblueError
def initialize(data) def initialize(data)
@data = data @data = data
end end

View file

@ -1,5 +1,5 @@
module Api::V1 module Api::V1
class NoJobProvidedError < StandardError class NoJobProvidedError < GranblueError
def http_status def http_status
422 422
end end

View file

@ -1,5 +1,5 @@
module Api::V1 module Api::V1
class NoJobSkillProvidedError < StandardError class NoJobSkillProvidedError < GranblueError
def http_status def http_status
422 422
end end

View file

@ -1,22 +1,20 @@
module Api::V1 module Api
class SameFavoriteUserError < StandardError module V1
def http_status class SameFavoriteUserError < GranblueError
422 def code
end 'same_favorite_user'
end
def code def message
"same_favorite_user" 'Users cannot favorite their own parties'
end end
def message def to_hash
"Users cannot favorite their own parties" {
end message: message,
code: code
def to_hash }
{ end
message: message,
code: code
}
end
end end
end
end end

View file

@ -2,15 +2,7 @@
module Api module Api
module V1 module V1
class TooManySkillsOfTypeError < StandardError class TooManySkillsOfTypeError < GranblueError
def initialize(data)
@data = data
end
def http_status
422
end
def code def code
'too_many_skills_of_type' 'too_many_skills_of_type'
end end