class GraphQL::Schema::RescueMiddleware
-
Store a table of errors & handlers
-
Rescue errors in a middleware chain, then check for a handler
-
If a handler is found, use it & return a {GraphQL::ExecutionError}
-
If no handler is found, re-raise the error
Attributes
rescue_table[R]
@return [Hash] `{class => proc}` pairs for handling errors
Public Class Methods
new()
click to toggle source
# File lib/graphql/schema/rescue_middleware.rb, line 11 def initialize @rescue_table = {} end
Public Instance Methods
call(*args) { || ... }
click to toggle source
Implement the requirement for {GraphQL::Schema::MiddlewareChain}
# File lib/graphql/schema/rescue_middleware.rb, line 33 def call(*args) begin yield rescue StandardError => err attempt_rescue(err) end end
remove_handler(*error_classes)
click to toggle source
Remove the handler for `error_classs` @param error_class [Class] the error class whose handler should be removed
# File lib/graphql/schema/rescue_middleware.rb, line 28 def remove_handler(*error_classes) error_classes.map{ |error_class| rescue_table.delete(error_class) } end
rescue_from(*error_classes, &block)
click to toggle source
@example Rescue from not-found by telling the user
MySchema.rescue_from(ActiveRecord::RecordNotFound) { "An item could not be found" }
@param error_classes [Class] one or more classes of errors to rescue from @yield [err] A handler to return a message for these error instances @yieldparam [Exception] an error that was rescued @yieldreturn [String] message to put in GraphQL
response
# File lib/graphql/schema/rescue_middleware.rb, line 22 def rescue_from(*error_classes, &block) error_classes.map{ |error_class| rescue_table[error_class] = block } end
Private Instance Methods
attempt_rescue(err)
click to toggle source
# File lib/graphql/schema/rescue_middleware.rb, line 43 def attempt_rescue(err) rescue_table.each { |klass, handler| if klass.is_a?(Class) && err.is_a?(klass) && handler message = handler.call(err) return GraphQL::ExecutionError.new(message) end } raise(err) end