Skip to content

Commit ab166dd

Browse files
authored
fix(errors): provide stacktrace with errors (#705)
* fix(errors): provide stacktrace with errors * chore: remove non related files * fix: remove useless attr_reader * test(errors): assert that backtrace is handle properly * chore(tests): add emtpy space
1 parent 56b60f3 commit ab166dd

File tree

9 files changed

+57
-16
lines changed

9 files changed

+57
-16
lines changed

app/services/forest_liana/ability/exceptions/access_denied.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ module ForestLiana
22
module Ability
33
module Exceptions
44
class AccessDenied < ForestLiana::Errors::ExpectedError
5-
def initialize
5+
def initialize (backtrace = nil)
66
super(
77
403,
88
:forbidden,
99
'You don\'t have permission to access this resource',
10-
'AccessDenied'
10+
'AccessDenied',
11+
backtrace
1112
)
1213
end
1314
end

app/services/forest_liana/ability/exceptions/action_condition_error.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ module ForestLiana
22
module Ability
33
module Exceptions
44
class ActionConditionError < ForestLiana::Errors::ExpectedError
5-
def initialize
5+
def initialize (backtrace = nil)
66
super(
77
409,
88
:conflict,
99
'The conditions to trigger this action cannot be verified. Please contact an administrator.',
10-
'InvalidActionConditionError'
10+
'InvalidActionConditionError',
11+
backtrace
1112
)
1213
end
1314
end

app/services/forest_liana/ability/exceptions/require_approval.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ module Ability
33
module Exceptions
44
class RequireApproval < ForestLiana::Errors::ExpectedError
55
attr_reader :data
6-
def initialize(data)
6+
def initialize(data, backtrace = nil)
77
@data = data
88
super(
99
403,
1010
:forbidden,
1111
'This action requires to be approved.',
1212
'CustomActionRequiresApprovalError',
13+
backtrace,
1314
)
1415
end
1516
end

app/services/forest_liana/ability/exceptions/trigger_forbidden.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ module ForestLiana
22
module Ability
33
module Exceptions
44
class TriggerForbidden < ForestLiana::Errors::ExpectedError
5-
def initialize
5+
def initialize(backtrace = nil)
66
super(
77
403,
88
:forbidden,
99
'You don\'t have the permission to trigger this action',
10-
'CustomActionTriggerForbiddenError'
10+
'CustomActionTriggerForbiddenError',
11+
backtrace,
1112
)
1213
end
1314
end

app/services/forest_liana/ability/exceptions/unknown_collection.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ module ForestLiana
22
module Ability
33
module Exceptions
44
class UnknownCollection < ForestLiana::Errors::ExpectedError
5-
def initialize(collection_name)
5+
def initialize(collection_name, backtrace = nil)
66
super(
77
409,
88
:conflict,
99
"The collection #{collection_name} doesn't exist",
10-
'collection not found'
10+
'collection not found',
11+
backtrace
1112
)
1213
end
1314
end

app/services/forest_liana/ability/permission.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def is_crud_authorized?(action, user, collection)
2727
is_allowed
2828
rescue ForestLiana::Errors::ExpectedError => exception
2929
raise exception
30-
rescue
31-
raise ForestLiana::Ability::Exceptions::UnknownCollection.new(collection_name)
30+
rescue => exception
31+
raise ForestLiana::Ability::Exceptions::UnknownCollection.new(collection_name, exception.backtrace)
3232
end
3333
end
3434

@@ -45,8 +45,8 @@ def is_smart_action_authorized?(user, collection, parameters, endpoint, http_met
4545
smart_action_approval.can_execute?
4646
rescue ForestLiana::Errors::ExpectedError => exception
4747
raise exception
48-
rescue
49-
raise ForestLiana::Ability::Exceptions::UnknownCollection.new(collection_name)
48+
rescue => exception
49+
raise ForestLiana::Ability::Exceptions::UnknownCollection.new(collection_name, exception.backtrace)
5050
end
5151
end
5252

app/services/forest_liana/ability/permission/smart_action_checker.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def match_conditions(condition_name)
6565
end
6666

6767
records.select(@collection.table_name + '.id').count == attributes[:ids].count
68-
rescue
69-
raise ForestLiana::Ability::Exceptions::ActionConditionError.new
68+
rescue => exception
69+
raise ForestLiana::Ability::Exceptions::ActionConditionError.new(exception.backtrace)
7070
end
7171
end
7272

config/initializers/errors.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ def initialize(error, error_description, state)
4444
class ExpectedError < StandardError
4545
attr_reader :error_code, :status, :message, :name
4646

47-
def initialize(error_code, status, message, name = nil)
47+
def initialize(error_code, status, message, name = nil, backtrace = nil)
4848
@error_code = error_code
4949
@status = status
5050
@message = message
5151
@name = name
52+
53+
set_backtrace(backtrace) unless backtrace.nil?
5254
end
5355

5456
def display_error
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module ForestLiana
2+
describe Errors do
3+
describe 'ExpectedError' do
4+
describe 'when initializing' do
5+
describe 'when backtrace is added' do
6+
it 'should add the backtrace to the errors if passed' do
7+
err = nil
8+
9+
begin
10+
raise "This is an exception"
11+
rescue => error
12+
err = ForestLiana::Errors::ExpectedError.new(300, 300, error.message, nil, error.backtrace )
13+
end
14+
15+
expect(err.backtrace).to be_truthy
16+
end
17+
end
18+
describe 'when backtrace is not added' do
19+
it 'should not break nor add any backtrace' do
20+
err = nil
21+
22+
begin
23+
raise "This is an exception"
24+
rescue => error
25+
err = ForestLiana::Errors::ExpectedError.new(300, 300, error.message, nil)
26+
end
27+
28+
expect(err.backtrace).to be_falsy
29+
end
30+
end
31+
end
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)