diff --git a/app/controllers/api/v2/base_controller.rb b/app/controllers/api/v2/base_controller.rb index f247e2a12..70f850e89 100644 --- a/app/controllers/api/v2/base_controller.rb +++ b/app/controllers/api/v2/base_controller.rb @@ -58,13 +58,25 @@ class API::V2::BaseController < ApplicationController def ensure_authorized_network if @api_token.forbidden_network?(request.remote_ip) address = IPAddr.new(request.remote_ip) - render json: { errors: ["request issued from a forbidden network. Add #{address.to_string}/#{address.prefix} to your allowed adresses in your /profil"] }, status: :forbidden + render json: graphql_error("Request issued from a forbidden network. Add #{address.to_string}/#{address.prefix} to your allowed adresses in your /profil", :forbidden), status: :forbidden end end def ensure_token_is_not_expired if @api_token.expired? - render json: { errors: ['token expired'] }, status: :unauthorized + render json: graphql_error('Token expired', :unauthorized), status: :unauthorized end end + + def graphql_error(message, code, exception_id: nil, backtrace: nil) + { + errors: [ + { + message:, + extensions: { code:, exception_id:, backtrace: }.compact + } + ], + data: nil + } + end end diff --git a/app/controllers/api/v2/graphql_controller.rb b/app/controllers/api/v2/graphql_controller.rb index c258613cf..678f4c62d 100644 --- a/app/controllers/api/v2/graphql_controller.rb +++ b/app/controllers/api/v2/graphql_controller.rb @@ -27,17 +27,7 @@ class API::V2::GraphqlController < API::V2::BaseController def process_action(*args) super rescue ActionDispatch::Http::Parameters::ParseError => exception - render json: { - errors: [ - { - message: exception.cause.message, - extensions: { - code: :bad_request - } - } - ], - data: nil - }, status: 400 + render json: graphql_error(exception.cause.message, :bad_request), status: :bad_request end def query @@ -77,33 +67,14 @@ class API::V2::GraphqlController < API::V2::BaseController end def handle_parse_error(exception, code) - render json: { - errors: [ - { - message: exception.message, - extensions: { code: } - } - ], - data: nil - }, status: 400 + render json: graphql_error(exception.message, code), status: :bad_request end def handle_error_in_development(exception) logger.error exception.message logger.error exception.backtrace.join("\n") - render json: { - errors: [ - { - message: exception.message, - extensions: { - code: :internal_server_error, - backtrace: exception.backtrace - } - } - ], - data: nil - }, status: 500 + render json: graphql_error(exception.message, :internal_server_error, backtrace: exception.backtrace), status: :internal_server_error end def handle_error_in_production(exception) @@ -113,17 +84,6 @@ class API::V2::GraphqlController < API::V2::BaseController Sentry.capture_exception(exception) end - render json: { - errors: [ - { - message: "Internal Server Error", - extensions: { - code: :internal_server_error, - exception_id: - } - } - ], - data: nil - }, status: 500 + render json: graphql_error("Internal Server Error", :internal_server_error, exception_id:), status: :internal_server_error end end