From c443133e779c4c508b9c6429dd4ba623d64f03f1 Mon Sep 17 00:00:00 2001 From: Bob Van Landuyt Date: Mon, 21 May 2018 13:42:07 +0200 Subject: Handle exceptions outside the GraphQL schema This allows us to report JSON parse exceptions to clients and ignore them in sentry. --- app/controllers/graphql_controller.rb | 38 ++++++++++++++++------------------- 1 file changed, 17 insertions(+), 21 deletions(-) (limited to 'app/controllers/graphql_controller.rb') diff --git a/app/controllers/graphql_controller.rb b/app/controllers/graphql_controller.rb index ef258bf07cb..0a1cf169aca 100644 --- a/app/controllers/graphql_controller.rb +++ b/app/controllers/graphql_controller.rb @@ -5,7 +5,7 @@ class GraphqlController < ApplicationController before_action :check_graphql_feature_flag! def execute - variables = ensure_hash(params[:variables]) + variables = Gitlab::Graphql::Variables.new(params[:variables]).to_h query = params[:query] operation_name = params[:operationName] context = { @@ -15,35 +15,31 @@ class GraphqlController < ApplicationController render json: result end + rescue_from StandardError do |exception| + log_exception(exception) + + render_error("Internal server error") + end + + rescue_from Gitlab::Graphql::Variables::Invalid do |exception| + render_error(exception.message, status: :unprocessable_entity) + end + private # Overridden from the ApplicationController to make the response look like # a GraphQL response. That is nicely picked up in Graphiql. def render_404 - error = { errors: [ message: "Not found" ] } + render_error("Not found!", status: :not_found) + end + + def render_error(message, status: 500) + error = { errors: [message: message] } - render json: error, status: :not_found + render json: error, status: status end def check_graphql_feature_flag! render_404 unless Feature.enabled?(:graphql) end - - # 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 ArgumentError, "Unexpected parameter: #{ambiguous_param}" - end - end end -- cgit v1.2.3