1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
# frozen_string_literal: true
module OAuth2
class Error < StandardError
attr_reader :response, :body, :code, :description
# standard error codes include:
# 'invalid_request', 'invalid_client', 'invalid_token', 'invalid_grant', 'unsupported_grant_type', 'invalid_scope'
# response might be a Response object, or the response.parsed hash
def initialize(response)
@response = response
if response.respond_to?(:parsed)
if response.parsed.is_a?(Hash)
@code = response.parsed['error']
@description = response.parsed['error_description']
end
elsif response.is_a?(Hash)
@code = response['error']
@description = response['error_description']
end
@body = if response.respond_to?(:body)
response.body
else
@response
end
message_opts = parse_error_description(@code, @description)
super(error_message(@body, message_opts))
end
private
def error_message(response_body, opts = {})
lines = []
lines << opts[:error_description] if opts[:error_description]
error_string = if response_body.respond_to?(:encode) && 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
lines << error_string
lines.join("\n")
end
def parse_error_description(code, description)
return {} unless code || description
error_description = ''
error_description += "#{code}: " if code
error_description += description if description
{error_description: error_description}
end
end
end
|