refactor(graphql): add graphql_error helper

This commit is contained in:
Paul Chavard 2024-07-22 11:04:51 +02:00
parent 406d05a47a
commit 38243434d2
No known key found for this signature in database
2 changed files with 18 additions and 46 deletions

View file

@ -58,13 +58,25 @@ class API::V2::BaseController < ApplicationController
def ensure_authorized_network def ensure_authorized_network
if @api_token.forbidden_network?(request.remote_ip) if @api_token.forbidden_network?(request.remote_ip)
address = IPAddr.new(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
end end
def ensure_token_is_not_expired def ensure_token_is_not_expired
if @api_token.expired? if @api_token.expired?
render json: { errors: ['token expired'] }, status: :unauthorized render json: graphql_error('Token expired', :unauthorized), status: :unauthorized
end end
end end
def graphql_error(message, code, exception_id: nil, backtrace: nil)
{
errors: [
{
message:,
extensions: { code:, exception_id:, backtrace: }.compact
}
],
data: nil
}
end
end end

View file

@ -27,17 +27,7 @@ class API::V2::GraphqlController < API::V2::BaseController
def process_action(*args) def process_action(*args)
super super
rescue ActionDispatch::Http::Parameters::ParseError => exception rescue ActionDispatch::Http::Parameters::ParseError => exception
render json: { render json: graphql_error(exception.cause.message, :bad_request), status: :bad_request
errors: [
{
message: exception.cause.message,
extensions: {
code: :bad_request
}
}
],
data: nil
}, status: 400
end end
def query def query
@ -77,33 +67,14 @@ class API::V2::GraphqlController < API::V2::BaseController
end end
def handle_parse_error(exception, code) def handle_parse_error(exception, code)
render json: { render json: graphql_error(exception.message, code), status: :bad_request
errors: [
{
message: exception.message,
extensions: { code: }
}
],
data: nil
}, status: 400
end end
def handle_error_in_development(exception) def handle_error_in_development(exception)
logger.error exception.message logger.error exception.message
logger.error exception.backtrace.join("\n") logger.error exception.backtrace.join("\n")
render json: { render json: graphql_error(exception.message, :internal_server_error, backtrace: exception.backtrace), status: :internal_server_error
errors: [
{
message: exception.message,
extensions: {
code: :internal_server_error,
backtrace: exception.backtrace
}
}
],
data: nil
}, status: 500
end end
def handle_error_in_production(exception) def handle_error_in_production(exception)
@ -113,17 +84,6 @@ class API::V2::GraphqlController < API::V2::BaseController
Sentry.capture_exception(exception) Sentry.capture_exception(exception)
end end
render json: { render json: graphql_error("Internal Server Error", :internal_server_error, exception_id:), status: :internal_server_error
errors: [
{
message: "Internal Server Error",
extensions: {
code: :internal_server_error,
exception_id:
}
}
],
data: nil
}, status: 500
end end
end end