Sha256: 4cc09c65b7012e1f6cc67157f504d2cf691c6607e43b68f21500050d7c6853d0

Contents?: true

Size: 985 Bytes

Versions: 1

Compression:

Stored size: 985 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, 412, 422
          raise Acumatica::BadRequest, env
        when 401, 403
          raise Acumatica::Unauthorized, env
        when 429
          raise Acumatica::TooManyRequests, 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]["error"]
    end
  end

  class BadRequest < Error
  end

  class Unauthorized < Error
  end

  class TooManyRequests < Error
  end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acumatica-0.7.0 lib/acumatica/errors.rb