Sha256: 08bc760ce6c25efa63a9032c26e301e62b03e4013a1b70a872a57fe7d1756b7a

Contents?: true

Size: 1.32 KB

Versions: 51

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Ibrain
  module Core
    module ControllerHelpers
      module Response
        extend ActiveSupport::Concern

        included do
          helper_method :render_json_error
          helper_method :render_json_ok
        end

        protected

        def render_json_error(error, status)
          e_message = error.try(:record).try(:errors).try(:full_messages).try(:first)
          e_message = error.try(:message) if e_message.blank?
          e_message = error.try(:details) if e_message.blank?

          backtrace = error.try(:backtrace).try(:join, "\n")

          Ibrain::Logger.error e_message
          Ibrain::Logger.error backtrace

          render json: {
            errors: [{
              message: e_message,
              extensions: {
                code: status,
                exception: {
                  stacktrace: [
                    backtrace
                  ]
                }
              }
            }],
            message: e_message,
            data: nil
          }, status: status
        end

        def render_json_ok(data, message, errors = [])
          render json: {
            errors: errors,
            message: message || I18n.t('ibrain.system.message.ok'),
            data: data.as_json
          }, status: :ok
        end
      end
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
ibrain-core-0.5.15 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.13 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.12 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.10 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.9 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.8 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.7 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.6 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.5 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.4 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.3 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.2 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.1 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.5.0 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.4.9 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.4.8 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.4.7 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.4.5 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.4.4 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.4.3 lib/ibrain/core/controller_helpers/response.rb