errors.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. use std::{fmt, fmt::Debug, str::Utf8Error};
  2. #[derive(thiserror::Error, Clone, Debug)]
  3. pub struct OTError {
  4. pub code: OTErrorCode,
  5. pub msg: String,
  6. }
  7. macro_rules! static_ot_error {
  8. ($name:ident, $code:expr) => {
  9. #[allow(non_snake_case, missing_docs)]
  10. pub fn $name() -> OTError {
  11. $code.into()
  12. }
  13. };
  14. }
  15. impl std::convert::From<OTErrorCode> for OTError {
  16. fn from(code: OTErrorCode) -> Self {
  17. OTError {
  18. code: code.clone(),
  19. msg: format!("{:?}", code),
  20. }
  21. }
  22. }
  23. impl OTError {
  24. pub fn new(code: OTErrorCode, msg: String) -> OTError {
  25. Self { code, msg }
  26. }
  27. pub fn context<T: Debug>(mut self, error: T) -> Self {
  28. self.msg = format!("{:?}", error);
  29. self
  30. }
  31. static_ot_error!(duplicate_revision, OTErrorCode::DuplicatedRevision);
  32. static_ot_error!(revision_id_conflict, OTErrorCode::RevisionIDConflict);
  33. static_ot_error!(internal, OTErrorCode::Internal);
  34. static_ot_error!(serde, OTErrorCode::SerdeError);
  35. static_ot_error!(path_not_found, OTErrorCode::PathNotFound);
  36. static_ot_error!(compose, OTErrorCode::ComposeOperationFail);
  37. static_ot_error!(record_not_found, OTErrorCode::RecordNotFound);
  38. }
  39. impl fmt::Display for OTError {
  40. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  41. write!(f, "{:?}: {}", self.code, self.msg)
  42. }
  43. }
  44. impl std::convert::From<serde_json::Error> for OTError {
  45. fn from(error: serde_json::Error) -> Self {
  46. ErrorBuilder::new(OTErrorCode::SerdeError).error(error).build()
  47. }
  48. }
  49. impl std::convert::From<Utf8Error> for OTError {
  50. fn from(error: Utf8Error) -> Self {
  51. ErrorBuilder::new(OTErrorCode::SerdeError).error(error).build()
  52. }
  53. }
  54. #[derive(Debug, Clone, Eq, PartialEq)]
  55. pub enum OTErrorCode {
  56. IncompatibleLength,
  57. ApplyInsertFail,
  58. ApplyDeleteFail,
  59. ApplyFormatFail,
  60. ComposeOperationFail,
  61. IntervalOutOfBound,
  62. UndoFail,
  63. RedoFail,
  64. SerdeError,
  65. DuplicatedRevision,
  66. RevisionIDConflict,
  67. Internal,
  68. PathNotFound,
  69. PathIsEmpty,
  70. InvalidPath,
  71. RecordNotFound,
  72. }
  73. pub struct ErrorBuilder {
  74. pub code: OTErrorCode,
  75. pub msg: Option<String>,
  76. }
  77. impl ErrorBuilder {
  78. pub fn new(code: OTErrorCode) -> Self {
  79. ErrorBuilder { code, msg: None }
  80. }
  81. pub fn msg<T>(mut self, msg: T) -> Self
  82. where
  83. T: Into<String>,
  84. {
  85. self.msg = Some(msg.into());
  86. self
  87. }
  88. pub fn error<T>(mut self, msg: T) -> Self
  89. where
  90. T: std::fmt::Debug,
  91. {
  92. self.msg = Some(format!("{:?}", msg));
  93. self
  94. }
  95. pub fn build(mut self) -> OTError {
  96. OTError::new(self.code, self.msg.take().unwrap_or_else(|| "".to_owned()))
  97. }
  98. }