Module: JsonApiServer::Controller::ErrorHandling
- Defined in:
- lib/json_api_server/controller/error_handling.rb
Overview
Handles common controller errors. Returns JsonApi errors jsonapi.org/format/#errors.
Usage
To use, include the JsonApiServer::Controller::ErrorHandling module in your base API controller class:
i.e.,
class Api::BaseController < ApplicationController
include JsonApiServer::Controller::ErrorHandling
end
I18n
Messages are defined in config/locales/en.yml.
Customize ActiveRecord::RecordNotFound and ActiveRecord::RecordNotUnique messages to reference a resource by name.
Example:
# Given a sandwiches controller...
class Api::V1::SandwichesController < Api::BaseController
...
end
# config/locales/en.yml
en:
json_api_server:
controller:
sandwiches:
name: 'sandwich'
# messages now become:
# 404 => "This sandwich does not exist."
# 409 => "This sandwich already exists."
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/json_api_server/controller/error_handling.rb', line 41 def self.included(base) # Overrides of exception handling. base.rescue_from StandardError, with: :render_500 base.rescue_from JsonApiServer::BadRequest, with: :render_400 base.rescue_from ActionController::BadRequest, with: :render_400 base.rescue_from ActiveRecord::RecordNotFound, with: :render_404 base.rescue_from ActiveRecord::RecordNotUnique, with: :render_409 base.rescue_from ActionController::RoutingError, with: :render_404 base.rescue_from ActionController::UrlGenerationError, with: :render_404 base.rescue_from ActionController::UnknownController, with: :render_404 base.rescue_from ActionController::UnknownFormat, with: :render_unknown_format end |