Skip to content

Commit

Permalink
Rename Notice to Report
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrylo committed Jun 10, 2024
1 parent 68a10aa commit d24ce55
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/telebugs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
require_relative "telebugs/notifier"
require_relative "telebugs/sender"
require_relative "telebugs/wrapped_error"
require_relative "telebugs/notice"
require_relative "telebugs/report"
require_relative "telebugs/error_message"
require_relative "telebugs/backtrace"
require_relative "telebugs/file_cache"
Expand Down
4 changes: 2 additions & 2 deletions lib/telebugs/backtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module Telebugs
module Backtrace
module Patterns
# The pattern that matches standard Ruby stack frames, such as
# ./spec/notice_spec.rb:43:in `block (3 levels) in <top (required)>'
# ./spec/report_spec.rb:43:in `block (3 levels) in <top (required)>'
RUBY = %r{\A
(?<file>.+) # Matches './spec/notice_spec.rb'
(?<file>.+) # Matches './spec/report_spec.rb'
:
(?<line>\d+) # Matches '43'
:in\s
Expand Down
2 changes: 1 addition & 1 deletion lib/telebugs/notifier.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Telebugs
# Notifier is reponsible for sending notices to Telebugs.
# Notifier is reponsible for sending reports to Telebugs.
class Notifier
class << self
attr_writer :instance
Expand Down
27 changes: 6 additions & 21 deletions lib/telebugs/notice.rb → lib/telebugs/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Telebugs
# Represents a piece of information that will be sent to Telebugs API to
# report an error.
class Notice
class Report
# The list of possible exceptions that might be raised when an object is
# converted to JSON
JSON_EXCEPTIONS = [
Expand All @@ -13,32 +13,17 @@ class Notice
Encoding::UndefinedConversionError
].freeze

# The maxium size of the JSON payload in bytes
MAX_NOTICE_SIZE = 64000
# The maxium size of the JSON data in bytes
MAX_REPORT_SIZE = 64000

attr_reader :data

def initialize(error)
@payload = {
@data = {
errors: errors_as_json(error)
}
end

# Converts the notice to JSON. Calls +to_json+ on each object inside
# notice's payload. Truncates notices, JSON representation of which is
# bigger than {MAX_NOTICE_SIZE}.
def to_json(*_args)
loop do
begin
json = @payload.to_json
rescue *JSON_EXCEPTIONS
# TODO
else
return json if json && json.bytesize <= MAX_NOTICE_SIZE
end

break if truncate == 0
end
end

private

def errors_as_json(error)
Expand Down
50 changes: 25 additions & 25 deletions test/test_notice.rb → test/test_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "test_helper"

class TestNotice < Minitest::Test
class TestReport < Minitest::Test
def fixture_path(filename)
File.expand_path(File.join("test", "fixtures", filename))
end
Expand All @@ -21,63 +21,63 @@ def teardown
Telebugs::Config.instance.reset
end

def test_to_json_with_nested_errors
def test_as_json_with_nested_errors
begin
raise StandardError.new("error 1")
rescue => _
begin
raise StandardError.new("error 2")
rescue => e2
n = Telebugs::Notice.new(e2)
r = Telebugs::Report.new(e2)
end
end

json = JSON.parse(n.to_json)
error1 = json["errors"][0]
error2 = json["errors"][1]
d = r.data
error1 = d[:errors][0]
error2 = d[:errors][1]

assert_equal "StandardError", error1["type"]
assert_equal "error 2", error1["message"]
assert error1.key?("backtrace")
assert error1["backtrace"].size > 0
assert_nil error1["backtrace"][0]["code"]
assert_equal "StandardError", error1[:type]
assert_equal "error 2", error1[:message]
assert error1.key?(:backtrace)
assert error1[:backtrace].size > 0
assert_nil error1[:backtrace][0][:code]

assert_equal "StandardError", error2["type"]
assert_equal "error 1", error2["message"]
assert error2.key?("backtrace")
assert error2["backtrace"].size > 0
assert_nil error2["backtrace"][0]["code"]
assert_equal "StandardError", error2[:type]
assert_equal "error 1", error2[:message]
assert error2.key?(:backtrace)
assert error2[:backtrace].size > 0
assert_nil error2[:backtrace][0][:code]
end

def test_to_json_code
def test_as_json_code
error = RuntimeError.new
error.set_backtrace([
"#{project_root_path("code.rb")}:18:in `start'",
fixture_path("notroot.txt:3:in `pineapple'"),
"#{project_root_path("vendor/bundle/ignored_file.rb")}:2:in `ignore_me'"
])

n = Telebugs::Notice.new(error)
json = JSON.parse(n.to_json)
backtrace = json["errors"][0]["backtrace"]
r = Telebugs::Report.new(error)
d = r.data
backtrace = d[:errors][0][:backtrace]

assert_equal 3, backtrace.size

assert_equal(
{
"start_line" => 16,
"lines" => [
start_line: 16,
lines: [
" end",
"",
" def start",
" loop do",
" print \"You: \""
]
},
backtrace[0]["code"]
backtrace[0][:code]
)

assert_nil backtrace[1]["code"]
assert_nil backtrace[2]["code"]
assert_nil backtrace[1][:code]
assert_nil backtrace[2][:code]
end
end

0 comments on commit d24ce55

Please sign in to comment.