Last active
August 29, 2015 14:23
-
-
Save trevorrowe/8162733833c90491a9b3 to your computer and use it in GitHub Desktop.
Plugin that conditionally raises response errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConditionalRaisePlugin < Seahorse::Client::Plugin | |
class Handler < Seahorse::Client::Handler | |
def call(context) | |
response = @handler.call(context) | |
conditional_raise(response.error, context.config.errors_to_ignore) | |
response | |
end | |
private | |
def conditional_raise(error, ignored) | |
raise error unless ignored.any? { |error_class| error_class === error } | |
end | |
end | |
option(:errors_to_ignore, []) | |
handler(Handler, step: :validate, priority: 95) | |
end | |
# replace default plugin | |
Aws::DynamoDB::Client.remove_plugin(Seahorse::Client::Plugins::RaiseResponseErrors) | |
Aws::DynamoDB::Client.add_plugin(ConditionalRaisePlugin) | |
# create a white-list of error types that should not be raised | |
ddb = Aws::DynamoDB::Client.new(errors_to_ignore: [ | |
Aws::DynamoDB::Errors::ResourceNotFoundException, | |
]) | |
resp = ddb.describe_table(table_name: 'no-such-table') | |
resp.error | |
#=> #<Aws::DynamoDB::Errors::ResourceNotFoundException: Requested resource not found: Table: no-such-table not found> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment