34. #denatechcon
graphql-errors + エラーカスタム
class ApiError < GraphQL::ExecutionError
def initialize(message, type = nil)
@type = type
super(message)
end
def to_h
super.merge("type" => @type)
end
end
graphql-errors https://github.com/exAspArk/graphql-errors
GraphQL::Errors.configure(schema) do
rescue_from ActiveRecord::RecordNotFound do |e|
raise ApiError.new(“#{e.model} not found”,
"NotFound")
end
# 独自エラークラス
rescue_from Errors::PermissionDenied do |e|
raise ApiError.new(e.message, e.class.to_s)
end
end