Sha256: 3847fda5cd17e1bd7c6a9cd733475eae31ec01c6a5cc5414874e32e981f331f4

Contents?: true

Size: 969 Bytes

Versions: 2

Compression:

Stored size: 969 Bytes

Contents

# frozen_string_literal: true

require 'faraday'

module Acumatica
  class ErrorHandler < Faraday::Middleware
    def initialize(app)
      super app
      @parser = nil
    end

    def call(request_env)
      @app.call(request_env).on_complete do |env|
        case env[:status]
        when 400
          raise Acumatica::BadRequest, env
        when 401
          raise Acumatica::Unauthorized, env
        when 500
          raise Acumatica::InternalServerError, env
        end
      end
    end
  end

  class Error < StandardError
    def initialize(env = {})
      super error_message(env)
    end

    def error_message(env)
      env[:body]["message"]
    end
  end

  class BadRequest < Error
    def error_message(env)
      super(env) + ": " + env[:body]["modelState"].values.join(",")
    end
  end

  class Unauthorized < Error
  end

  class InternalServerError < Error
    def error_message(env)
      env[:body]["exceptionMessage"]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acumatica-0.4.0 lib/acumatica/errors.rb
acumatica-0.3.0 lib/acumatica/errors.rb