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

View file

@ -1,5 +1,5 @@
module Api::V1
class FavoriteAlreadyExistsError < StandardError
class FavoriteAlreadyExistsError < GranblueError
def http_status
422
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
class IncompatibleSkillError < StandardError
class IncompatibleSkillError < GranblueError
def initialize(data)
@data = data
end

View file

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

View file

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

View file

@ -1,15 +1,12 @@
module Api::V1
class SameFavoriteUserError < StandardError
def http_status
422
end
module Api
module V1
class SameFavoriteUserError < GranblueError
def code
"same_favorite_user"
'same_favorite_user'
end
def message
"Users cannot favorite their own parties"
'Users cannot favorite their own parties'
end
def to_hash
@ -20,3 +17,4 @@ module Api::V1
end
end
end
end

View file

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