Add new errors and fix handling in ApiController
This commit is contained in:
parent
b1c6930121
commit
4ee90f6c09
5 changed files with 134 additions and 75 deletions
|
|
@ -1,68 +1,76 @@
|
||||||
module Api::V1
|
module Api::V1
|
||||||
class ApiController < ActionController::API
|
class ApiController < ActionController::API
|
||||||
##### Doorkeeper
|
##### Doorkeeper
|
||||||
include Doorkeeper::Rails::Helpers
|
include Doorkeeper::Rails::Helpers
|
||||||
|
|
||||||
##### Errors
|
##### Errors
|
||||||
rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
|
rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
|
||||||
rescue_from ActiveRecord::RecordNotDestroyed, 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::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::SameFavoriteUserError, with: :render_unprocessable_entity_response
|
||||||
rescue_from Api::V1::FavoriteAlreadyExistsError, with: :render_unprocessable_entity_response
|
rescue_from Api::V1::FavoriteAlreadyExistsError, with: :render_unprocessable_entity_response
|
||||||
rescue_from Api::V1::NoJobProvidedError, with: :render_unprocessable_entity_response
|
rescue_from Api::V1::NoJobProvidedError, with: :render_unprocessable_entity_response
|
||||||
rescue_from Api::V1::TooManySkillsOfTypeError, with: :render_unprocessable_entity_response
|
rescue_from Api::V1::TooManySkillsOfTypeError, with: :render_unprocessable_entity_response
|
||||||
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|
|
||||||
|
render_error(e)
|
||||||
|
end
|
||||||
|
|
||||||
##### Hooks
|
##### Hooks
|
||||||
before_action :current_user
|
before_action :current_user
|
||||||
before_action :set_default_content_type
|
before_action :set_default_content_type
|
||||||
|
|
||||||
##### Responders
|
##### Responders
|
||||||
respond_to :json
|
respond_to :json
|
||||||
|
|
||||||
##### Methods
|
##### Methods
|
||||||
# Assign the current user if the Doorkeeper token isn't nil, then
|
# Assign the current user if the Doorkeeper token isn't nil, then
|
||||||
# update the current user's last seen datetime and last IP address
|
# update the current user's last seen datetime and last IP address
|
||||||
# before returning
|
# before returning
|
||||||
def current_user
|
def current_user
|
||||||
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
|
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
|
||||||
|
|
||||||
return @current_user
|
return @current_user
|
||||||
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
|
|
||||||
|
|
||||||
### 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_unauthorized_response
|
|
||||||
render action: 'errors', status: :unauthorized
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def restrict_access
|
|
||||||
raise UnauthorizedError unless current_user
|
|
||||||
end
|
|
||||||
end
|
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
|
||||||
|
|
||||||
|
### Error response methods
|
||||||
|
def render_error(error)
|
||||||
|
render action: 'errors', json: error.to_hash, status: error.http_status
|
||||||
|
end
|
||||||
|
|
||||||
|
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_unauthorized_response
|
||||||
|
render action: 'errors', status: :unauthorized
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def restrict_access
|
||||||
|
raise UnauthorizedError unless current_user
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
29
app/errors/api/v1/IncompatibleSkillError.rb
Normal file
29
app/errors/api/v1/IncompatibleSkillError.rb
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
module Api::V1
|
||||||
|
class IncompatibleSkillError < StandardError
|
||||||
|
def initialize(data)
|
||||||
|
@data = data
|
||||||
|
end
|
||||||
|
|
||||||
|
def http_status
|
||||||
|
422
|
||||||
|
end
|
||||||
|
|
||||||
|
def code
|
||||||
|
'incompatible_skill'
|
||||||
|
end
|
||||||
|
|
||||||
|
def message
|
||||||
|
'The selected skill cannot be added to the current job'
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_hash
|
||||||
|
ap @data
|
||||||
|
{
|
||||||
|
message: message,
|
||||||
|
code: code,
|
||||||
|
job: @data[:job],
|
||||||
|
skill: @data[:skill]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -1,22 +1,22 @@
|
||||||
module Api::V1
|
module Api::V1
|
||||||
class NoJobProvidedError < StandardError
|
class NoJobProvidedError < StandardError
|
||||||
def http_status
|
def http_status
|
||||||
422
|
422
|
||||||
end
|
|
||||||
|
|
||||||
def code
|
|
||||||
"no_job_provided"
|
|
||||||
end
|
|
||||||
|
|
||||||
def message
|
|
||||||
"A job ID must be provided to search for job skills"
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_hash
|
|
||||||
{
|
|
||||||
message: message,
|
|
||||||
code: code
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def code
|
||||||
|
"no_job_provided"
|
||||||
|
end
|
||||||
|
|
||||||
|
def message
|
||||||
|
"A job ID must be provided"
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_hash
|
||||||
|
{
|
||||||
|
message: message,
|
||||||
|
code: code
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
22
app/errors/api/v1/NoJobSkillProvidedError.rb
Normal file
22
app/errors/api/v1/NoJobSkillProvidedError.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
module Api::V1
|
||||||
|
class NoJobSkillProvidedError < StandardError
|
||||||
|
def http_status
|
||||||
|
422
|
||||||
|
end
|
||||||
|
|
||||||
|
def code
|
||||||
|
"no_job_skill_provided"
|
||||||
|
end
|
||||||
|
|
||||||
|
def message
|
||||||
|
"A job skill ID must be provided"
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_hash
|
||||||
|
{
|
||||||
|
message: message,
|
||||||
|
code: code
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -23,7 +23,7 @@ module Api
|
||||||
{
|
{
|
||||||
message: message,
|
message: message,
|
||||||
code: code,
|
code: code,
|
||||||
idea_id: @data[:skill_type]
|
skill_type: @data[:skill_type]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue