Skip to content

Commit dcdbaa4

Browse files
committed
Raises on INSUFFICIENT_PERMISSION error from NetSuite response
1 parent ec194a5 commit dcdbaa4

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

lib/netsuite/errors.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module NetSuite
22
class RecordNotFound < StandardError; end
33
class InitializationError < StandardError; end
44
class ConfigurationError < StandardError; end
5+
class PermissionError < StandardError; end
56

67
# NOTE not an exception, used as a wrapped around NetSuite SOAP error
78
class Error

lib/netsuite/response.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ def initialize(attributes = {})
77
@header = attributes[:header]
88
@body = attributes[:body]
99
@errors = attributes[:errors] || []
10+
11+
raise_on_response_errors
1012
end
1113

1214
def success!
@@ -16,5 +18,30 @@ def success!
1618
def success?
1719
@success
1820
end
21+
22+
private
23+
24+
def status_detail
25+
@body &&
26+
@body.is_a?(Hash) &&
27+
@body[:status] &&
28+
@body[:status][:status_detail]
29+
end
30+
31+
def response_error_code
32+
if success?
33+
nil
34+
else
35+
status_detail &&
36+
status_detail[:code]
37+
end
38+
end
39+
40+
def raise_on_response_errors
41+
case response_error_code
42+
when 'INSUFFICIENT_PERMISSION'
43+
raise NetSuite::PermissionError, status_detail[:message]
44+
end
45+
end
1946
end
2047
end

spec/netsuite/response_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@
1414
response = NetSuite::Response.new(:success => true)
1515
expect(response).to be_success
1616
end
17+
18+
it 'throws PermissionError when response failed to INSUFFICIENT_PERMISSION' do
19+
expect {
20+
NetSuite::Response.new(
21+
:success => false,
22+
:body => {
23+
status: {
24+
status_detail: {
25+
:@type => 'ERROR',
26+
:code => 'INSUFFICIENT_PERMISSION',
27+
:message => 'Permission Violation: The subsidiary restrictions on your role prevent you from seeing this record.'
28+
}
29+
}
30+
}
31+
)
32+
}.to raise_error(NetSuite::PermissionError)
33+
end
1734
end
1835

1936
describe '#body' do

0 commit comments

Comments
 (0)