gitlab-org--gitlab-foss/lib/gitlab/graphql/variables.rb
Bob Van Landuyt c443133e77 Handle exceptions outside the GraphQL schema
This allows us to report JSON parse exceptions to clients and ignore
them in sentry.
2018-06-05 20:47:42 +02:00

37 lines
815 B
Ruby

module Gitlab
module Graphql
class Variables
Invalid = Class.new(Gitlab::Graphql::StandardGraphqlError)
def initialize(param)
@param = param
end
def to_h
ensure_hash(@param)
end
private
# Handle form data, JSON body, or a blank value
def ensure_hash(ambiguous_param)
case ambiguous_param
when String
if ambiguous_param.present?
ensure_hash(JSON.parse(ambiguous_param))
else
{}
end
when Hash, ActionController::Parameters
ambiguous_param
when nil
{}
else
raise Invalid, "Unexpected parameter: #{ambiguous_param}"
end
rescue JSON::ParserError => e
raise Invalid.new(e)
end
end
end
end