error.rs 5.2 KB

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