Module: Platanus::ApiBoilerplate
- Defined in:
- lib/platanus/api_base.rb
Overview
# Boilerplate for platanus json api controllers
Provides base error handling and rendering methods. Also provides a couple of opt-in behaviours..
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/platanus/api_base.rb', line 14 def self.included(base) base.respond_to :json # Respond to json by default. ## EXCEPTION HANDLING base.rescue_from 'Exception' do |exc| logger.error exc. logger.error exc.backtrace.join("\n") api_respond_error(:internal_server_error, { msg: 'server_error', exception: { type: exc.class.to_s, msg: exc. } } ) end base.rescue_from 'ActiveRecord::RecordNotFound' do |exc| api_respond_error(:not_found, { msg: 'record_not_found', detail: exc. }) end base.rescue_from 'ActiveModel::MassAssignmentSecurity::Error' do |exc| api_respond_error(:bad_request, { msg: 'protected_attributes', detail: exc. } ) end base.rescue_from 'ActiveRecord::RecordInvalid' do |exc| api_respond_error(:bad_request, { msg: 'invalid_attributes', errors: exc.record.errors } ) end # Platanus error support base.rescue_from 'Platanus::StatusError' do |exc| api_respond_error(exc.status, { msg: exc. }) end # Canned error support base.rescue_from 'Platanus::Canned2::AuthError' do |exc| api_respond_error(:unauthorized, { msg: 'canned' }) end base.extend ClassMethods base.send :include, InstanceMethods end |