Skip to content

Commit

Permalink
Merge pull request #85 from simplybusiness/fix-error-signature
Browse files Browse the repository at this point in the history
Fix error signature
  • Loading branch information
peaky76 authored Sep 27, 2023
2 parents 28925e7 + 122c9eb commit 713c699
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
5 changes: 4 additions & 1 deletion catalog-info.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: twiglet
name: twiglet-ruby
title: Twiglet
description: simple logging
annotations:
rubygems.org/name: "twiglet"
spec:
type: library
lifecycle: production
Expand Down
10 changes: 8 additions & 2 deletions lib/twiglet/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ def warn(message_or_error = nil, &block)
super(message, &block)
end

def error(message = nil, error = nil, &block)
message = error_message(error, message) if error
def error(message_or_error = nil, error = nil, &block)
message = if error
error_message(error, message_or_error)
elsif message_or_error.is_a?(Exception)
error_message(message_or_error)
else
message_or_error
end

super(message, &block)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twiglet/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Twiglet
VERSION = '3.9.1'
VERSION = '3.9.2'
end
41 changes: 30 additions & 11 deletions test/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@
actual_log = read_json(@buffer)

assert_equal 'Artificially raised exception', actual_log[:message]
assert_equal 'divided by 0', actual_log[:error][:message]
assert_equal 'ZeroDivisionError', actual_log[:error][:type]
assert_equal 'divided by 0', actual_log[:error][:message]
assert_match 'test/logger_test.rb', actual_log[:error][:stack_trace].first
end

Expand All @@ -353,35 +353,54 @@
end

it 'should log an error with string message' do
e = StandardError.new('Unknown error')
e = StandardError.new('Some error')
@logger.error('Artificially raised exception with string message', e)

actual_log = read_json(@buffer)

assert_equal 'Artificially raised exception with string message', actual_log[:message]
assert_equal 'StandardError', actual_log[:error][:type]
assert_equal 'Unknown error', actual_log[:error][:message]
assert_equal 'Some error', actual_log[:error][:message]
end

it 'should log error type properly even when active_support/json is required' do
require 'active_support/json'
e = StandardError.new('Unknown error')
@logger.error('Artificially raised exception with string message', e)
it 'should log an error if no message is given' do
e = StandardError.new('Some error')
@logger.error(e)

actual_log = read_json(@buffer)

assert_equal 'Some error', actual_log[:message]
assert_equal 'StandardError', actual_log[:error][:type]
assert_equal 'Some error', actual_log[:error][:message]
end

it 'can log just an error as "error", if no message is given' do
e = StandardError.new('some error')
it 'should log an error if nil message is given' do
e = StandardError.new('Some error')
@logger.error(nil, e)

actual_log = read_json(@buffer)

assert_equal 'some error', actual_log[:message]
assert_equal 'Some error', actual_log[:message]
assert_equal 'StandardError', actual_log[:error][:type]
assert_equal 'Some error', actual_log[:error][:message]
end

it 'should log a string if no error is given' do
@logger.error('Some error')

actual_log = read_json(@buffer)

assert_equal 'Some error', actual_log[:message]
end

it 'should log error type properly even when active_support/json is required' do
require 'active_support/json'
e = StandardError.new('Unknown error')
@logger.error('Artificially raised exception with string message', e)

actual_log = read_json(@buffer)

assert_equal 'StandardError', actual_log[:error][:type]
assert_equal 'some error', actual_log[:error][:message]
end

[:debug, :info, :warn].each do |level|
Expand Down

0 comments on commit 713c699

Please sign in to comment.