Skip to content

Commit

Permalink
Raises on INSUFFICIENT_PERMISSION error from NetSuite response
Browse files Browse the repository at this point in the history
  • Loading branch information
aom committed Aug 16, 2018
1 parent ec194a5 commit b0c8481
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/netsuite/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module NetSuite
class RecordNotFound < StandardError; end
class InitializationError < StandardError; end
class ConfigurationError < StandardError; end
class PermissionError < StandardError; end

# NOTE not an exception, used as a wrapped around NetSuite SOAP error
class Error
Expand Down
27 changes: 27 additions & 0 deletions lib/netsuite/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def initialize(attributes = {})
@header = attributes[:header]
@body = attributes[:body]
@errors = attributes[:errors] || []

raise_on_response_errors
end

def success!
Expand All @@ -16,5 +18,30 @@ def success!
def success?
@success
end

private

def status_detail
@body &&
@body.is_a?(Hash) &&
@body[:status] &&
@body[:status][:status_detail]
end

def response_error_code
if success?
nil
else
status_detail &&
status_detail[:code]
end
end

def validate_response
case raise_on_response_errors
when 'INSUFFICIENT_PERMISSION'
raise NetSuite::PermissionError, status_detail[:message]
end
end
end
end
17 changes: 17 additions & 0 deletions spec/netsuite/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
response = NetSuite::Response.new(:success => true)
expect(response).to be_success
end

it 'throws PermissionError when response failed to INSUFFICIENT_PERMISSION' do
expect {
NetSuite::Response.new(
:success => false,
:body => {
status: {
status_detail: {
:@type => 'ERROR',
:code => 'INSUFFICIENT_PERMISSION',
:message => 'Permission Violation: The subsidiary restrictions on your role prevent you from seeing this record.'
}
}
}
)
}.to raise_error(NetSuite::PermissionError)
end
end

describe '#body' do
Expand Down

0 comments on commit b0c8481

Please sign in to comment.