Sha256: fc1a75002d5593bc28e57833a42e471713167be2b1ec9ecdc4dc755f87cf0021

Contents?: true

Size: 1.26 KB

Versions: 9

Compression:

Stored size: 1.26 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?

          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

9 entries across 9 versions & 1 rubygems

Version Path
ibrain-core-0.1.8 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.1.7 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.1.6 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.1.5 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.1.4 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.1.3 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.1.2 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.1.1 lib/ibrain/core/controller_helpers/response.rb
ibrain-core-0.1.0 lib/ibrain/core/controller_helpers/response.rb