api: return error cause on parse error

Currently, when a query can't be parsed, the error is:
- logged to Sentry (which is useless to us),
- returned as a generic 'Internal Server Error' (which is useless to the
  user who made the query).

With this commit, the error is instead ignored from our logs (because it
is a user error), but the parse error details are returned to the user,
with the following format:

> {'errors': [{'message': 'Parse error on ")" (RPAREN) at [3, 23]'}]}
This commit is contained in:
Pierre de La Morinerie 2021-11-19 15:15:10 +01:00
parent 2e4635c786
commit 859a147c49

View file

@ -8,6 +8,8 @@ class API::V2::GraphqlController < API::V2::BaseController
operation_name: params[:operationName])
render json: result
rescue GraphQL::ParseError => exception
handle_parse_error(exception)
rescue => exception
if Rails.env.production?
handle_error_in_production(exception)
@ -88,6 +90,15 @@ class API::V2::GraphqlController < API::V2::BaseController
end
end
def handle_parse_error(exception)
render json: {
errors: [
{ message: exception.message }
],
data: nil
}, status: 400
end
def handle_error_in_development(exception)
logger.error exception.message
logger.error exception.backtrace.join("\n")