error.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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,
  36. VerifyFailed,
  37. }
  38. impl std::error::Error for Error {}
  39. impl fmt::Display for Error {
  40. fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
  41. match *self {
  42. Error::Io(ref err) => write!(f, "io error:{:?}", err),
  43. Error::NonMinimalVarInt => f.write_str("non-minimal varint"),
  44. Error::ParseFailed(ref err) => write!(f, "parse failed: {}", err),
  45. Error::ParseIntError => f.write_str("Parse int error"),
  46. Error::AsyncChannelError => f.write_str("Async_channel error"),
  47. Error::MalformedPacket => f.write_str("Malformed packet"),
  48. Error::AddrParseError => f.write_str("Unable to parse address"),
  49. Error::BadVariableRefType => f.write_str("Bad variable ref type byte"),
  50. Error::BadOperationType => f.write_str("Bad operation type byte"),
  51. Error::BadConstraintType => f.write_str("Bad constraint type byte"),
  52. Error::InvalidParamName => f.write_str("Invalid param name"),
  53. Error::MissingParams => f.write_str("Missing params"),
  54. Error::VMError => f.write_str("VM error"),
  55. Error::BadContract => f.write_str("Contract is poorly defined"),
  56. Error::Groth16Error => f.write_str("Groth16 error"),
  57. Error::RusqliteError => f.write_str("Rusqlite error"),
  58. Error::OperationFailed => f.write_str("Operation failed"),
  59. Error::ConnectFailed => f.write_str("Connection failed"),
  60. Error::ConnectTimeout => f.write_str("Connection timed out"),
  61. Error::ChannelStopped => f.write_str("Channel stopped"),
  62. Error::ChannelTimeout => f.write_str("Channel timed out"),
  63. Error::ServiceStopped => f.write_str("Service stopped"),
  64. Error::Utf8Error => f.write_str("Malformed UTF8"),
  65. Error::NoteDecryptionFailed => f.write_str("Unable to decrypt mint note"),
  66. Error::ServicesError(ref err) => write!(f, "Services error: {}", err),
  67. Error::ZMQError => f.write_str("ZMQ error"),
  68. Error::VerifyFailed => f.write_str("Verify failed"),
  69. }
  70. }
  71. }
  72. // TODO: Match statement to parse external errors into strings.
  73. impl From<zeromq::ZmqError> for Error {
  74. fn from(_err: zeromq::ZmqError) -> Error {
  75. Error::ZMQError
  76. }
  77. }
  78. impl From<std::io::Error> for Error {
  79. fn from(err: std::io::Error) -> Error {
  80. Error::Io(err.kind())
  81. }
  82. }
  83. impl From<rusqlite::Error> for Error {
  84. fn from(_err: rusqlite::Error) -> Error {
  85. Error::RusqliteError
  86. }
  87. }
  88. impl From<ZKVMError> for Error {
  89. fn from(_err: ZKVMError) -> Error {
  90. Error::VMError
  91. }
  92. }
  93. impl From<bellman::SynthesisError> for Error {
  94. fn from(_err: bellman::SynthesisError) -> Error {
  95. Error::Groth16Error
  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<std::string::FromUtf8Error> for Error {
  119. fn from(_err: std::string::FromUtf8Error) -> Error {
  120. Error::Utf8Error
  121. }
  122. }
  123. impl From<state::VerifyFailed> for Error {
  124. fn from(_err: state::VerifyFailed) -> Error {
  125. Error::VerifyFailed
  126. }
  127. }