Agreed with handling these specific errors within ```encryptor``` gem and
then handling ```encryptor``` errors within this gem.

Handling ```OpenSSL``` exceptions in the ```encryptor``` gem allows others
who may want to use the gem outside of ```attr_encrypted``` to benefit.



On Wed, Mar 5, 2014 at 11:44 AM, Tyler Pickett <notifications@github.com>wrote:

> In lib/attr_encrypted.rb:
>
> > @@ -193,7 +193,17 @@ def decrypt(attribute, encrypted_value, options = {})
> >      options = encrypted_attributes[attribute.to_sym].merge(options)
> >      if options[:if] && !options[:unless] && !encrypted_value.nil? && !(encrypted_value.is_a?(String) && encrypted_value.empty?)
> >        encrypted_value = encrypted_value.unpack(options[:encode]).first if options[:encode]
> > -      value = options[:encryptor].send(options[:decrypt_method], options.merge!(:value => encrypted_value))
> > +      begin
> > +        value = options[:encryptor].send(options[:decrypt_method], options.merge!(:value => encrypted_value))
> > +      rescue OpenSSL::Cipher::CipherError => ex
> > +        if ex.message == 'bad decrypt'
> > +          raise Errors::BadDecryptError, "Could not decrypt attribute #{ attribute }: key, iv, or salt may have changed or been corrupted."
> > +        elsif ex.message == 'wrong final block length'
> > +          raise Errors::BlockLengthError, "Could not decrypt attribute #{ attribute } the cyphertext has an invalid final block. It may have been truncated when stored to the database."
> > +        else
> > +          raise CipherError, "Could not decrypt attribute #{ attribute }"
>
> I added a test for the generic case in 69971f7<https://github.com/attr-encrypted/attr_encrypted/commit/69971f7>and I do think there is value in having some handling here because having
> attr_encrypted clients receive errors from anything other than
> attr_encrypted feels wrong. Perhaps the way forward is to rework this PR
> to handle encryptor specific exceptions and make the necessary changes to
> encryptor to capture these OpenSSL exceptions.
>
> --
> Reply to this email directly or view it on GitHub<https://github.com/attr-encrypted/attr_encrypted/pull/98/files#r10306883>
> .
>