etscrivner / rbsecp256k1

Compiled Ruby wrapper around libsecp256k1 for secp256k1 ECDSA.
The Unlicense
19 stars 13 forks source link

Create and use a library exception hierarchy #41

Closed etscrivner closed 5 years ago

etscrivner commented 5 years ago

Right now we throw a lot of TypeError, ArgumentError, and RuntimeError which are quite generic. It would instead be nice to have a full exception hierarchy used by the library itself to make errors more descriptive to end users and easier to catch. Perhaps something like the following:

BaseError < StandardError
├── SerializationError
├── DeserializationError

An example usage of each is shown below. The BaseError would serve as the catch-all for issues that are not related to serialization/deserialization (for example, context randomization failing).

SerializationError Raised whenever an attempt to serialize a value fails.

begin
  public_key.compressed
rescue Secp256k1::SerializationError => err
end

DeserializationError Raises whenever parsing of data fails for any reason.

begin
  Signature.from_der_encoded(value)
rescue Secp256k1::DeserializationError => err
end