Sha256: 5ebafd14a709295225c02fbf556e3f8ce017a8c8e7b114a007fa7270c49a9305

Contents?: true

Size: 1.74 KB

Versions: 105

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true
# test_via: ../backtrace.rb
module GraphQL
  class Backtrace
    # When {Backtrace} is enabled, raised errors are wrapped with {TracedError}.
    class TracedError < GraphQL::Error
      # @return [Array<String>] Printable backtrace of GraphQL error context
      attr_reader :graphql_backtrace

      # @return [GraphQL::Query::Context] The context at the field where the error was raised
      attr_reader :context

      MESSAGE_TEMPLATE = <<-MESSAGE
Unhandled error during GraphQL execution:

  %{cause_message}
    %{cause_backtrace}
    %{cause_backtrace_more}
Use #cause to access the original exception (including #cause.backtrace).

GraphQL Backtrace:
%{graphql_table}
MESSAGE

      # This many lines of the original Ruby backtrace
      # are included in the message
      CAUSE_BACKTRACE_PREVIEW_LENGTH = 10

      def initialize(err, current_ctx)
        @context = current_ctx
        backtrace = Backtrace.new(current_ctx, value: err)
        @graphql_backtrace = backtrace.to_a

        cause_backtrace_preview = err.backtrace.first(CAUSE_BACKTRACE_PREVIEW_LENGTH).join("\n    ")

        cause_backtrace_remainder_length = err.backtrace.length - CAUSE_BACKTRACE_PREVIEW_LENGTH
        cause_backtrace_more = if cause_backtrace_remainder_length < 0
          ""
        elsif cause_backtrace_remainder_length == 1
          "... and 1 more line\n"
        else
          "... and #{cause_backtrace_remainder_length} more lines\n"
        end

        message = MESSAGE_TEMPLATE % {
          cause_message: err.message,
          cause_backtrace: cause_backtrace_preview,
          cause_backtrace_more: cause_backtrace_more,
          graphql_table: backtrace.inspect,
        }
        super(message)
      end
    end
  end
end

Version data entries

105 entries across 105 versions & 1 rubygems

Version Path
graphql-1.9.15 lib/graphql/backtrace/traced_error.rb
graphql-1.9.14 lib/graphql/backtrace/traced_error.rb
graphql-1.10.0.pre1 lib/graphql/backtrace/traced_error.rb
graphql-1.9.13 lib/graphql/backtrace/traced_error.rb
graphql-1.9.12 lib/graphql/backtrace/traced_error.rb
graphql-1.9.11 lib/graphql/backtrace/traced_error.rb
graphql-1.9.10 lib/graphql/backtrace/traced_error.rb
graphql-1.9.9 lib/graphql/backtrace/traced_error.rb
graphql-1.9.8 lib/graphql/backtrace/traced_error.rb
graphql-1.9.7 lib/graphql/backtrace/traced_error.rb
graphql-1.9.6 lib/graphql/backtrace/traced_error.rb
graphql-1.9.5 lib/graphql/backtrace/traced_error.rb
graphql-1.9.4 lib/graphql/backtrace/traced_error.rb
graphql-1.9.3 lib/graphql/backtrace/traced_error.rb
graphql-1.9.2 lib/graphql/backtrace/traced_error.rb
graphql-1.8.17 lib/graphql/backtrace/traced_error.rb
graphql-1.8.16 lib/graphql/backtrace/traced_error.rb
graphql-1.9.1 lib/graphql/backtrace/traced_error.rb
graphql-1.9.0 lib/graphql/backtrace/traced_error.rb
graphql-1.8.15 lib/graphql/backtrace/traced_error.rb