Add custom errors
This commit is contained in:
parent
3841c4f76c
commit
438fc7294d
4 changed files with 123 additions and 52 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
class Api::V1::ApiController < ActionController::API
|
module Api::V1
|
||||||
|
class ApiController < ActionController::API
|
||||||
##### Doorkeeper
|
##### Doorkeeper
|
||||||
include Doorkeeper::Rails::Helpers
|
include Doorkeeper::Rails::Helpers
|
||||||
|
|
||||||
|
|
@ -8,6 +9,9 @@ class Api::V1::ApiController < ActionController::API
|
||||||
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response
|
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response
|
||||||
rescue_from ActiveRecord::RecordNotSaved, with: :render_unprocessable_entity_response
|
rescue_from ActiveRecord::RecordNotSaved, with: :render_unprocessable_entity_response
|
||||||
rescue_from ActiveRecord::RecordNotUnique, 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
|
rescue_from ActionController::ParameterMissing, with: :render_unprocessable_entity_response
|
||||||
|
|
||||||
##### Hooks
|
##### Hooks
|
||||||
|
|
@ -66,3 +70,4 @@ class Api::V1::ApiController < ActionController::API
|
||||||
raise UnauthorizedError unless current_user
|
raise UnauthorizedError unless current_user
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
22
app/errors/api/v1/FavoriteAlreadyExistsError.rb
Normal file
22
app/errors/api/v1/FavoriteAlreadyExistsError.rb
Normal file
|
|
@ -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
|
||||||
22
app/errors/api/v1/SameFavoriteUserError.rb
Normal file
22
app/errors/api/v1/SameFavoriteUserError.rb
Normal file
|
|
@ -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
|
||||||
22
app/errors/api/v1/UnauthorizedError.rb
Normal file
22
app/errors/api/v1/UnauthorizedError.rb
Normal file
|
|
@ -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
|
||||||
Loading…
Reference in a new issue