error.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. use rusqlite;
  2. use std::fmt;
  3. use crate::state;
  4. use crate::vm::ZKVMError;
  5. pub type Result<T> = std::result::Result<T, Error>;
  6. #[derive(Debug, Clone)]
  7. pub enum Error {
  8. Io(std::io::ErrorKind),
  9. /// VarInt was encoded in a non-minimal way
  10. NonMinimalVarInt,
  11. /// Parsing error
  12. ParseFailed(&'static str),
  13. ParseIntError,
  14. AsyncChannelError,
  15. MalformedPacket,
  16. AddrParseError,
  17. BadVariableRefType,
  18. BadOperationType,
  19. BadConstraintType,
  20. InvalidParamName,
  21. MissingParams,
  22. VMError,
  23. BadContract,
  24. Groth16Error,
  25. RusqliteError,
  26. OperationFailed,
  27. ConnectFailed,
  28. ConnectTimeout,
  29. ChannelStopped,
  30. ChannelTimeout,
  31. ServiceStopped,
  32. Utf8Error,
  33. NoteDecryptionFailed,
  34. ServicesError(&'static str),
  35. ZMQError(String),
  36. VerifyFailed,
  37. TryIntoError,
  38. TryFromError,
  39. RocksdbError(String),
  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::Io(ref err) => write!(f, "io error:{:?}", err),
  46. Error::NonMinimalVarInt => f.write_str("non-minimal varint"),
  47. Error::ParseFailed(ref err) => write!(f, "parse failed: {}", err),
  48. Error::ParseIntError => f.write_str("Parse int error"),
  49. Error::AsyncChannelError => f.write_str("Async_channel error"),
  50. Error::MalformedPacket => f.write_str("Malformed packet"),
  51. Error::AddrParseError => f.write_str("Unable to parse address"),
  52. Error::BadVariableRefType => f.write_str("Bad variable ref type byte"),
  53. Error::BadOperationType => f.write_str("Bad operation type byte"),
  54. Error::BadConstraintType => f.write_str("Bad constraint type byte"),
  55. Error::InvalidParamName => f.write_str("Invalid param name"),
  56. Error::MissingParams => f.write_str("Missing params"),
  57. Error::VMError => f.write_str("VM error"),
  58. Error::BadContract => f.write_str("Contract is poorly defined"),
  59. Error::Groth16Error => f.write_str("Groth16 error"),
  60. Error::RusqliteError => f.write_str("Rusqlite error"),
  61. Error::OperationFailed => f.write_str("Operation failed"),
  62. Error::ConnectFailed => f.write_str("Connection failed"),
  63. Error::ConnectTimeout => f.write_str("Connection timed out"),
  64. Error::ChannelStopped => f.write_str("Channel stopped"),
  65. Error::ChannelTimeout => f.write_str("Channel timed out"),
  66. Error::ServiceStopped => f.write_str("Service stopped"),
  67. Error::Utf8Error => f.write_str("Malformed UTF8"),
  68. Error::NoteDecryptionFailed => f.write_str("Unable to decrypt mint note"),
  69. Error::ServicesError(ref err) => write!(f, "Services error: {}", err),
  70. Error::ZMQError(ref err) => write!(f, "ZMQError: {}", err),
  71. Error::VerifyFailed => f.write_str("Verify failed"),
  72. Error::TryIntoError => f.write_str("TryInto error"),
  73. Error::TryFromError => f.write_str("TryFrom error"),
  74. Error::RocksdbError(ref err) => write!(f, "Rocksdb Error: {}", err),
  75. }
  76. }
  77. }
  78. // TODO: Match statement to parse external errors into strings.
  79. impl From<zeromq::ZmqError> for Error {
  80. fn from(err: zeromq::ZmqError) -> Error {
  81. Error::ZMQError(err.to_string())
  82. }
  83. }
  84. impl From<rocksdb::Error> for Error {
  85. fn from(err: rocksdb::Error) -> Error {
  86. Error::RocksdbError(err.to_string())
  87. }
  88. }
  89. impl From<std::io::Error> for Error {
  90. fn from(err: std::io::Error) -> Error {
  91. Error::Io(err.kind())
  92. }
  93. }
  94. impl From<rusqlite::Error> for Error {
  95. fn from(_err: rusqlite::Error) -> Error {
  96. Error::RusqliteError
  97. }
  98. }
  99. impl From<ZKVMError> for Error {
  100. fn from(_err: ZKVMError) -> Error {
  101. Error::VMError
  102. }
  103. }
  104. impl From<bellman::SynthesisError> for Error {
  105. fn from(_err: bellman::SynthesisError) -> Error {
  106. Error::Groth16Error
  107. }
  108. }
  109. impl<T> From<async_channel::SendError<T>> for Error {
  110. fn from(_err: async_channel::SendError<T>) -> Error {
  111. Error::AsyncChannelError
  112. }
  113. }
  114. impl From<async_channel::RecvError> for Error {
  115. fn from(_err: async_channel::RecvError) -> Error {
  116. Error::AsyncChannelError
  117. }
  118. }
  119. impl From<std::net::AddrParseError> for Error {
  120. fn from(_err: std::net::AddrParseError) -> Error {
  121. Error::AddrParseError
  122. }
  123. }
  124. impl From<std::num::ParseIntError> for Error {
  125. fn from(_err: std::num::ParseIntError) -> Error {
  126. Error::ParseIntError
  127. }
  128. }
  129. impl From<std::string::FromUtf8Error> for Error {
  130. fn from(_err: std::string::FromUtf8Error) -> Error {
  131. Error::Utf8Error
  132. }
  133. }
  134. impl From<state::VerifyFailed> for Error {
  135. fn from(_err: state::VerifyFailed) -> Error {
  136. Error::VerifyFailed
  137. }
  138. }