Sha256: c82ebadcee8e28e22a42ece73c6904d1f6b5e8c8d4b099fb95718ae6e54092b9

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module OAuth2
  class Error < StandardError
    attr_reader :response, :code, :description

    # standard error values include:
    # :invalid_request, :invalid_client, :invalid_token, :invalid_grant, :unsupported_grant_type, :invalid_scope
    def initialize(response)
      response.error = self
      @response = response

      if response.parsed.is_a?(Hash)
        @code = response.parsed['error']
        @description = response.parsed['error_description']
        error_description = "#{@code}: #{@description}"
      end

      super(error_message(response.body, :error_description => error_description))
    end

    # Makes a error message
    # @param [String] response_body response body of request
    # @param [String] opts :error_description error description to show first line
    def error_message(response_body, opts = {})
      message = []

      opts[:error_description] && (message << opts[:error_description])

      error_message = if opts[:error_description] && opts[:error_description].respond_to?(:encoding)
                        script_encoding = opts[:error_description].encoding
                        response_body.encode(script_encoding, :invalid => :replace, :undef => :replace)
                      else
                        response_body
                      end

      message << error_message

      message.join("\n")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
oauth2-1.4.11 lib/oauth2/error.rb
oauth2-1.4.10 lib/oauth2/error.rb
oauth2-1.4.9 lib/oauth2/error.rb