Class: X::RulesRejected

Inherits:
Streaming::Error show all
Defined in:
x-streaming/lib/x/streaming/rules_rejected.rb

Overview

Raised when the API left some of the rules of the filtered stream unchanged, and no block was given for them

The API adds or deletes the rules it can and reports the rest, such as a rule the app already has, as errors of a response that otherwise succeeds. StreamingClient#add_rules and StreamingClient#delete_rules yield each of them to a block, and, without one, raise this error rather than drop them, so that a rule that was not added, or a rule a dry run found invalid, is never passed over in silence. The rules that were changed stay changed, so the error holds what the method would have returned, the rules add_rules added in #added or the number of rules delete_rules deleted in #deleted_count, beside the #problems the API reported.

Examples:

Report the rules that were not added

begin
  streaming_client.add_rules(%w[ruby crystal])
rescue X::RulesRejected => e
  warn e.problems.map(&:title).join(", ")
  added = e.added
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, problems: [], added: nil, deleted_count: nil) ⇒ RulesRejected

Initialize a new RulesRejected

Public, so that code that rescues a RulesRejected can be tested with one built by hand, as StreamingClient builds one for the problems of a change of the rules, and raised with a message alone, as any exception is. The message is the one given, or else the title and detail of each problem.

Examples:

Create an error

error = X::RulesRejected.new(problems: X::Problem.all_from(body), added: [])

Raise the error with a message alone, as a test stub may

raise X::RulesRejected, "The rules were not added"

Parameters:

  • message (String, nil) (defaults to: nil) —

    the message, or nil for the one the problems give

  • problems (Array<Problem>) (defaults to: []) —

    the problems the API reported

  • added (Array<StreamRule>, nil) (defaults to: nil) —

    the rules add_rules added

  • deleted_count (Integer, nil) (defaults to: nil) —

    the number of rules delete_rules deleted



71
72
73
74
75
76
# File 'x-streaming/lib/x/streaming/rules_rejected.rb', line 71

def initialize(message = nil, problems: [], added: nil, deleted_count: nil)
  @problems = problems.dup.freeze
  @added = added.dup.freeze
  @deleted_count = deleted_count
  super(message || describe(problems))
end

Instance Attribute Details

#added ⇒ Array<StreamRule>? (readonly)

The rules add_rules added

They are what add_rules would have returned, had it been given a block.

Examples:

Read the rules that were added

error.added.map(&:value) # => ["crystal"]

Returns:

  • (Array<StreamRule>, nil) —

    the rules, frozen, or nil for an error delete_rules raised, or one built without them, such as one a test built



42
43
44
# File 'x-streaming/lib/x/streaming/rules_rejected.rb', line 42

def added
  @added
end

#deleted_count ⇒ Integer? (readonly)

The number of rules delete_rules deleted

It is what delete_rules would have returned, had it been given a block.

Examples:

Read the number of rules that were deleted

error.deleted_count # => 1

Returns:

  • (Integer, nil) —

    the number, or nil for an error add_rules raised, or one built without it, such as one a test built



53
54
55
# File 'x-streaming/lib/x/streaming/rules_rejected.rb', line 53

def deleted_count
  @deleted_count
end

#problems ⇒ Array<Problem> (readonly)

The problems the API reported of the rules it did not change

Examples:

Read the title of each problem

error.problems.map(&:title) # => ["DuplicateRule"]

Returns:

  • (Array<Problem>) —

    the problems, frozen, in the order the API reported them



31
32
33
# File 'x-streaming/lib/x/streaming/rules_rejected.rb', line 31

def problems
  @problems
end