Fix errors with error handling
This commit is contained in:
parent
e91e1664a6
commit
041f8ee189
1 changed files with 13 additions and 5 deletions
|
|
@ -9,7 +9,7 @@ module Api
|
||||||
##### 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_without_object
|
||||||
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
|
||||||
|
|
@ -25,7 +25,7 @@ module Api
|
||||||
|
|
||||||
##### Hooks
|
##### Hooks
|
||||||
before_action :current_user
|
before_action :current_user
|
||||||
before_action :set_default_content_type
|
before_action :default_content_type
|
||||||
|
|
||||||
##### Responders
|
##### Responders
|
||||||
respond_to :json
|
respond_to :json
|
||||||
|
|
@ -41,14 +41,14 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
# Set the response content-type
|
# Set the response content-type
|
||||||
def set_content_type(content_type)
|
def content_type(content_type)
|
||||||
response.headers['Content-Type'] = content_type
|
response.headers['Content-Type'] = content_type
|
||||||
end
|
end
|
||||||
|
|
||||||
# Set the default response content-type to application/javascript
|
# Set the default response content-type to application/javascript
|
||||||
# with a UTF-8 charset
|
# with a UTF-8 charset
|
||||||
def set_default_content_type
|
def default_content_type
|
||||||
set_content_type('application/javascript; charset=utf-8')
|
content_type('application/javascript; charset=utf-8')
|
||||||
end
|
end
|
||||||
|
|
||||||
### Error response methods
|
### Error response methods
|
||||||
|
|
@ -70,6 +70,14 @@ module Api
|
||||||
status: :unprocessable_entity
|
status: :unprocessable_entity
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_not_found_response_without_object
|
||||||
|
render json: ErrorBlueprint.render(nil,
|
||||||
|
error: {
|
||||||
|
message: 'Object could not be found',
|
||||||
|
code: 'not_found'
|
||||||
|
}), status: :not_found
|
||||||
|
end
|
||||||
|
|
||||||
def render_not_found_response(object)
|
def render_not_found_response(object)
|
||||||
render json: ErrorBlueprint.render(nil, error: {
|
render json: ErrorBlueprint.render(nil, error: {
|
||||||
message: "#{object.capitalize} could not be found",
|
message: "#{object.capitalize} could not be found",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue