error.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. use std::fmt;
  2. use crate::net::error::NetError;
  3. use crate::vm::ZKVMError;
  4. pub type Result<T> = std::result::Result<T, Error>;
  5. #[derive(Debug)]
  6. pub enum Error {
  7. Foo,
  8. CommitsDontAdd,
  9. InvalidCredential,
  10. TransactionPedersenCheckFailed,
  11. TokenAlreadySpent,
  12. InputTokenVerifyFailed,
  13. RangeproofPedersenMatchFailed,
  14. ProofsFailed,
  15. MissingProofs,
  16. Io(std::io::Error),
  17. /// VarInt was encoded in a non-minimal way
  18. NonMinimalVarInt,
  19. /// Parsing error
  20. ParseFailed(&'static str),
  21. ParseIntError,
  22. AsyncChannelError,
  23. MalformedPacket,
  24. AddrParseError,
  25. BadVariableRefType,
  26. BadOperationType,
  27. BadConstraintType,
  28. InvalidParamName,
  29. MissingParams,
  30. VMError(ZKVMError),
  31. BadContract,
  32. Groth16Error(bellman::SynthesisError),
  33. OperationFailed,
  34. ConnectFailed,
  35. ConnectTimeout,
  36. ChannelStopped,
  37. ChannelTimeout,
  38. ServiceStopped,
  39. Utf8Error,
  40. }
  41. impl std::error::Error for Error {}
  42. impl fmt::Display for Error {
  43. fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
  44. match *self {
  45. Error::Foo => f.write_str("foo"),
  46. Error::CommitsDontAdd => f.write_str("Commits don't add up properly"),
  47. Error::InvalidCredential => f.write_str("Credential is invalid"),
  48. Error::TransactionPedersenCheckFailed => {
  49. f.write_str("Transaction pedersens for input and output don't sum up")
  50. }
  51. Error::TokenAlreadySpent => f.write_str("This input token is already spent"),
  52. Error::InputTokenVerifyFailed => f.write_str("Input token verify of credential failed"),
  53. Error::RangeproofPedersenMatchFailed => {
  54. f.write_str("Rangeproof pedersen check for match failed")
  55. }
  56. Error::ProofsFailed => f.write_str("Proof validation failed"),
  57. Error::MissingProofs => f.write_str("Missing proofs"),
  58. Error::Io(ref err) => fmt::Display::fmt(err, f),
  59. Error::NonMinimalVarInt => f.write_str("non-minimal varint"),
  60. Error::ParseFailed(ref err) => write!(f, "parse failed: {}", err),
  61. Error::ParseIntError => f.write_str("Parse int error"),
  62. Error::AsyncChannelError => f.write_str("async_channel error"),
  63. Error::MalformedPacket => f.write_str("Malformed packet"),
  64. Error::AddrParseError => f.write_str("Unable to parse address"),
  65. Error::BadVariableRefType => f.write_str("Bad variable ref type byte"),
  66. Error::BadOperationType => f.write_str("Bad operation type byte"),
  67. Error::BadConstraintType => f.write_str("Bad constraint type byte"),
  68. Error::InvalidParamName => f.write_str("Invalid param name"),
  69. Error::MissingParams => f.write_str("Missing params"),
  70. Error::VMError(_) => f.write_str("VM error"),
  71. Error::BadContract => f.write_str("Contract is poorly defined"),
  72. Error::Groth16Error(ref err) => write!(f, "groth16 error: {}", err),
  73. Error::OperationFailed => f.write_str("Operation failed"),
  74. Error::ConnectFailed => f.write_str("Connection failed"),
  75. Error::ConnectTimeout => f.write_str("Connection timed out"),
  76. Error::ChannelStopped => f.write_str("Channel stopped"),
  77. Error::ChannelTimeout => f.write_str("Channel timed out"),
  78. Error::ServiceStopped => f.write_str("Service stopped"),
  79. Error::Utf8Error => f.write_str("Malformed UTF8"),
  80. }
  81. }
  82. }
  83. impl From<std::io::Error> for Error {
  84. fn from(err: std::io::Error) -> Error {
  85. Error::Io(err)
  86. }
  87. }
  88. impl From<ZKVMError> for Error {
  89. fn from(err: ZKVMError) -> Error {
  90. Error::VMError(err)
  91. }
  92. }
  93. impl From<bellman::SynthesisError> for Error {
  94. fn from(err: bellman::SynthesisError) -> Error {
  95. Error::Groth16Error(err)
  96. }
  97. }
  98. impl<T> From<async_channel::SendError<T>> for Error {
  99. fn from(_err: async_channel::SendError<T>) -> Error {
  100. Error::AsyncChannelError
  101. }
  102. }
  103. impl From<async_channel::RecvError> for Error {
  104. fn from(_err: async_channel::RecvError) -> Error {
  105. Error::AsyncChannelError
  106. }
  107. }
  108. impl From<std::net::AddrParseError> for Error {
  109. fn from(_err: std::net::AddrParseError) -> Error {
  110. Error::AddrParseError
  111. }
  112. }
  113. impl From<std::num::ParseIntError> for Error {
  114. fn from(_err: std::num::ParseIntError) -> Error {
  115. Error::ParseIntError
  116. }
  117. }
  118. impl From<NetError> for Error {
  119. fn from(err: NetError) -> Error {
  120. match err {
  121. NetError::OperationFailed => Error::OperationFailed,
  122. NetError::ConnectFailed => Error::ConnectFailed,
  123. NetError::ConnectTimeout => Error::ConnectTimeout,
  124. NetError::ChannelStopped => Error::ChannelStopped,
  125. NetError::ChannelTimeout => Error::ChannelTimeout,
  126. NetError::ServiceStopped => Error::ServiceStopped,
  127. }
  128. }
  129. }
  130. impl From<std::string::FromUtf8Error> for Error {
  131. fn from(_err: std::string::FromUtf8Error) -> Error {
  132. Error::Utf8Error
  133. }
  134. }