-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
193 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Telebugs | ||
# Represents a middleware that can be used to filter out errors. | ||
# You must inherit from this class and implement the #call method. | ||
class Middleware | ||
DEFAULT_WEIGHT = 0 | ||
|
||
def weight | ||
DEFAULT_WEIGHT | ||
end | ||
|
||
def call(_report) | ||
raise NotImplementedError, "You must implement the #call method" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Telebugs | ||
# MiddlewareStack represents an ordered array of middleware. | ||
# | ||
# A middleware is an object that responds to <b>#call</b> (typically a Proc or a | ||
# class that implements the call method). The <b>#call</b> method must accept | ||
# exactly one argument: the report object. | ||
# | ||
# When you add a new middleware to the stack, it gets inserted according to its | ||
# <b>weight</b>. Smaller weight means the middleware will be somewhere in the | ||
# beginning of the array. Larger - in the end. | ||
class MiddlewareStack | ||
attr_reader :middlewares | ||
|
||
def initialize | ||
@middlewares = [] | ||
end | ||
|
||
def use(new_middleware) | ||
@middlewares = (@middlewares << new_middleware).sort_by(&:weight).reverse | ||
end | ||
|
||
def call(report) | ||
@middlewares.each do |middleware| | ||
middleware.call(report) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class TestMiddleware < Minitest::Test | ||
def test_call | ||
assert_raises(NotImplementedError) do | ||
Telebugs::Middleware.new.call(nil) | ||
end | ||
end | ||
|
||
def test_weight | ||
assert_equal 0, Telebugs::Middleware.new.weight | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class TestFilteringMiddleware < Telebugs::Middleware | ||
def call(report) | ||
report.data[:errors][0][:message] = "[FILTERED]" | ||
end | ||
|
||
def weight | ||
1 | ||
end | ||
end | ||
|
||
class TestStartLineMiddleware < Telebugs::Middleware | ||
def call(report) | ||
report.data[:errors][0][:backtrace] = {123 => 456} | ||
end | ||
|
||
def weight | ||
10 | ||
end | ||
end | ||
|
||
class TestMiddlewareStack < Minitest::Test | ||
def test_call | ||
stack = Telebugs::MiddlewareStack.new | ||
stack.use TestFilteringMiddleware.new | ||
stack.use TestStartLineMiddleware.new | ||
|
||
report = Telebugs::Report.new(StandardError.new("error message")) | ||
stack.call(report) | ||
|
||
assert_equal "[FILTERED]", report.data[:errors][0][:message] | ||
assert_equal({123 => 456}, report.data[:errors][0][:backtrace]) | ||
end | ||
|
||
def test_weight | ||
stack = Telebugs::MiddlewareStack.new | ||
stack.use TestFilteringMiddleware.new | ||
stack.use TestStartLineMiddleware.new | ||
|
||
assert_equal( | ||
[TestStartLineMiddleware, TestFilteringMiddleware], | ||
stack.middlewares.map(&:class) | ||
) | ||
|
||
stack = Telebugs::MiddlewareStack.new | ||
stack.use TestStartLineMiddleware.new | ||
stack.use TestFilteringMiddleware.new | ||
|
||
assert_equal( | ||
[TestStartLineMiddleware, TestFilteringMiddleware], | ||
stack.middlewares.map(&:class) | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters