Sha256: 6e703970cfab337408c338a7413b0f71fccb9a534dd07feb260a9ff741fcc014

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

require 'rack/utils'
require 'active_support/concern'

module JSONAPI
  # Helpers to handle some error responses
  #
  # Most of the exceptions are handled in Rails by [ActionDispatch] middleware
  # See: https://api.rubyonrails.org/classes/ActionDispatch/ExceptionWrapper.html
  module Errors
    extend ActiveSupport::Concern

    included do
      rescue_from StandardError do |exception|
        error = { status: '500', title: Rack::Utils::HTTP_STATUS_CODES[500] }
        render jsonapi_errors: [error], status: :internal_server_error
      end

      [
        ActiveRecord::RecordNotFound
      ].each do |exception_class|
        rescue_from exception_class do |exception|
          error = { status: '404', title: Rack::Utils::HTTP_STATUS_CODES[404] }
          render jsonapi_errors: [error], status: :not_found
        end
      end

      [
        ActionController::ParameterMissing
      ].each do |exception_class|
        rescue_from exception_class do |exception|
          source = { pointer: '' }

          if !%w{data attributes relationships}.include?(exception.param.to_s)
            source[:pointer] = "/data/attributes/#{exception.param}"
          end

          error = {
            status: '422',
            title: Rack::Utils::HTTP_STATUS_CODES[422],
            source: source
          }

          render jsonapi_errors: [error], status: :unprocessable_entity
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
jsonapi.rb-1.3.3 lib/jsonapi/errors.rb
jsonapi.rb-1.3.2 lib/jsonapi/errors.rb
jsonapi.rb-1.3.1 lib/jsonapi/errors.rb
jsonapi.rb-1.3.0 lib/jsonapi/errors.rb
jsonapi.rb-1.2.1 lib/jsonapi/errors.rb
jsonapi.rb-1.2.0 lib/jsonapi/errors.rb