Skip to content

[FSSDK-9130] fix: invalid identifier error code #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions lib/optimizely/odp/odp_segment_api_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,47 +58,49 @@ def fetch_segments(api_key, api_host, user_key, user_value, segments_to_check)
)
rescue SocketError, Timeout::Error, Net::ProtocolError, Errno::ECONNRESET => e
@logger.log(Logger::DEBUG, "GraphQL download failed: #{e}")
log_failure('network error')
log_segments_failure('network error')
return nil
rescue Errno::EINVAL, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, HTTPUriError => e
log_failure(e)
log_segments_failure(e)
return nil
end

status = response.code.to_i
if status >= 400
log_failure(status)
log_segments_failure(status)
return nil
end

begin
response = JSON.parse(response.body)
rescue JSON::ParserError
log_failure('JSON decode error')
log_segments_failure('JSON decode error')
return nil
end

if response.include?('errors')
error_class = response['errors']&.first&.dig('extensions', 'classification') || 'decode error'
if error_class == 'InvalidIdentifierException'
log_failure('invalid identifier', Logger::WARN)
error = response['errors'].first if response['errors'].is_a? Array
error_code = extract_component(error, 'extensions', 'code')
if error_code == 'INVALID_IDENTIFIER_EXCEPTION'
log_segments_failure('invalid identifier', Logger::WARN)
else
log_failure(error_class)
error_class = extract_component(error, 'extensions', 'classification') || 'decode error'
log_segments_failure(error_class)
end
return nil
end

audiences = response.dig('data', 'customer', 'audiences', 'edges')
audiences = extract_component(response, 'data', 'customer', 'audiences', 'edges')
unless audiences
log_failure('decode error')
log_segments_failure('decode error')
return nil
end

audiences.filter_map do |edge|
name = edge.dig('node', 'name')
state = edge.dig('node', 'state')
name = extract_component(edge, 'node', 'name')
state = extract_component(edge, 'node', 'state')
unless name && state
log_failure('decode error')
log_segments_failure('decode error')
return nil
end
state == 'qualified' ? name : nil
Expand All @@ -107,8 +109,14 @@ def fetch_segments(api_key, api_host, user_key, user_value, segments_to_check)

private

def log_failure(message, level = Logger::ERROR)
def log_segments_failure(message, level = Logger::ERROR)
@logger.log(level, format(Optimizely::Helpers::Constants::ODP_LOGS[:FETCH_SEGMENTS_FAILED], message))
end

def extract_component(hash, *components)
hash.dig(*components) if hash.is_a? Hash
rescue TypeError
nil
end
end
end
3 changes: 2 additions & 1 deletion spec/odp/odp_segment_api_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
'customer'
],
extensions: {
classification: 'InvalidIdentifierException'
classification: 'DataFetchingException',
code: 'INVALID_IDENTIFIER_EXCEPTION'
}
}
],
Expand Down