diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-05-21 13:42:07 +0200 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-06-05 20:47:42 +0200 |
commit | c443133e779c4c508b9c6429dd4ba623d64f03f1 (patch) | |
tree | 390ec7649b0875f059c0962458319fe14ed8d34c /app/controllers/graphql_controller.rb | |
parent | aa4b1ae71260720b47695b8a256134f20280f61a (diff) | |
download | gitlab-ce-c443133e779c4c508b9c6429dd4ba623d64f03f1.tar.gz |
Handle exceptions outside the GraphQL schema
This allows us to report JSON parse exceptions to clients and ignore
them in sentry.
Diffstat (limited to 'app/controllers/graphql_controller.rb')
-rw-r--r-- | app/controllers/graphql_controller.rb | 38 |
1 files changed, 17 insertions, 21 deletions
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 |