impl Error for error type; remove `Unknown` error variant

This commit is contained in:
Andrew Poelstra 2015-10-17 09:49:19 -05:00
parent 7c85199b92
commit d49db8167e
3 changed files with 22 additions and 8 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "secp256k1" name = "secp256k1"
version = "0.3.1" version = "0.3.2"
authors = [ "Dawid Ciężarkiewicz <dpc@ucore.info>", authors = [ "Dawid Ciężarkiewicz <dpc@ucore.info>",
"Andrew Poelstra <apoelstra@wpsoftware.net>" ] "Andrew Poelstra <apoelstra@wpsoftware.net>" ]
license = "CC0-1.0" license = "CC0-1.0"

View File

@ -23,7 +23,7 @@ use serialize::{Decoder, Decodable, Encoder, Encodable};
use serde::{Serialize, Deserialize, Serializer, Deserializer}; use serde::{Serialize, Deserialize, Serializer, Deserializer};
use super::{Secp256k1, ContextFlag}; use super::{Secp256k1, ContextFlag};
use super::Error::{self, IncapableContext, InvalidPublicKey, InvalidSecretKey, Unknown}; use super::Error::{self, IncapableContext, InvalidPublicKey, InvalidSecretKey};
use constants; use constants;
use ffi; use ffi;
@ -88,7 +88,7 @@ impl SecretKey {
-> Result<(), Error> { -> Result<(), Error> {
unsafe { unsafe {
if ffi::secp256k1_ec_privkey_tweak_add(secp.ctx, self.as_mut_ptr(), other.as_ptr()) != 1 { if ffi::secp256k1_ec_privkey_tweak_add(secp.ctx, self.as_mut_ptr(), other.as_ptr()) != 1 {
Err(Unknown) Err(InvalidSecretKey)
} else { } else {
Ok(()) Ok(())
} }
@ -180,7 +180,7 @@ impl PublicKey {
other.as_ptr()) == 1 { other.as_ptr()) == 1 {
Ok(()) Ok(())
} else { } else {
Err(Unknown) Err(InvalidSecretKey)
} }
} }
} }

View File

@ -44,7 +44,7 @@ extern crate libc;
extern crate rand; extern crate rand;
use libc::size_t; use libc::size_t;
use std::{fmt, ops, ptr}; use std::{error, fmt, ops, ptr};
use rand::Rng; use rand::Rng;
#[macro_use] #[macro_use]
@ -267,14 +267,28 @@ pub enum Error {
InvalidSecretKey, InvalidSecretKey,
/// Bad recovery id /// Bad recovery id
InvalidRecoveryId, InvalidRecoveryId,
/// Boolean-returning function returned the wrong boolean
Unknown
} }
// Passthrough Debug to Display, since errors should be user-visible // Passthrough Debug to Display, since errors should be user-visible
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
fmt::Debug::fmt(self, f) f.write_str(error::Error::description(self))
}
}
impl error::Error for Error {
fn cause(&self) -> Option<&error::Error> { None }
fn description(&self) -> &str {
match *self {
Error::IncapableContext => "secp: context does not have sufficient capabilities",
Error::IncorrectSignature => "secp: signature failed verification",
Error::InvalidMessage => "secp: message was not 32 bytes (do you need to hash?)",
Error::InvalidPublicKey => "secp: malformed public key",
Error::InvalidSignature => "secp: malformed signature",
Error::InvalidSecretKey => "secp: malformed or out-of-range secret key",
Error::InvalidRecoveryId => "secp: bad recovery id"
}
} }
} }