errors.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use std::{error::Error, fmt, fmt::Debug, str::Utf8Error};
  2. #[derive(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 { $code.into() }
  11. };
  12. }
  13. impl std::convert::From<OTErrorCode> for OTError {
  14. fn from(code: OTErrorCode) -> Self {
  15. OTError {
  16. code: code.clone(),
  17. msg: format!("{:?}", code),
  18. }
  19. }
  20. }
  21. impl OTError {
  22. pub fn new(code: OTErrorCode, msg: &str) -> OTError {
  23. Self {
  24. code,
  25. msg: msg.to_owned(),
  26. }
  27. }
  28. pub fn context<T: Debug>(mut self, error: T) -> Self {
  29. self.msg = format!("{:?}", error);
  30. self
  31. }
  32. static_ot_error!(duplicate_revision, OTErrorCode::DuplicatedRevision);
  33. static_ot_error!(revision_id_conflict, OTErrorCode::RevisionIDConflict);
  34. static_ot_error!(internal, OTErrorCode::Internal);
  35. }
  36. impl fmt::Display for OTError {
  37. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "incompatible lengths") }
  38. }
  39. impl Error for OTError {
  40. fn source(&self) -> Option<&(dyn Error + 'static)> { None }
  41. }
  42. impl std::convert::From<serde_json::Error> for OTError {
  43. fn from(error: serde_json::Error) -> Self { ErrorBuilder::new(OTErrorCode::SerdeError).error(error).build() }
  44. }
  45. impl std::convert::From<Utf8Error> for OTError {
  46. fn from(error: Utf8Error) -> Self { ErrorBuilder::new(OTErrorCode::SerdeError).error(error).build() }
  47. }
  48. #[derive(Debug, Clone)]
  49. pub enum OTErrorCode {
  50. IncompatibleLength,
  51. ApplyInsertFail,
  52. ApplyDeleteFail,
  53. ApplyFormatFail,
  54. ComposeOperationFail,
  55. IntervalOutOfBound,
  56. UndoFail,
  57. RedoFail,
  58. SerdeError,
  59. DuplicatedRevision,
  60. RevisionIDConflict,
  61. Internal,
  62. }
  63. pub struct ErrorBuilder {
  64. pub code: OTErrorCode,
  65. pub msg: Option<String>,
  66. }
  67. impl ErrorBuilder {
  68. pub fn new(code: OTErrorCode) -> Self { ErrorBuilder { code, msg: None } }
  69. pub fn msg<T>(mut self, msg: T) -> Self
  70. where
  71. T: Into<String>,
  72. {
  73. self.msg = Some(msg.into());
  74. self
  75. }
  76. pub fn error<T>(mut self, msg: T) -> Self
  77. where
  78. T: std::fmt::Debug,
  79. {
  80. self.msg = Some(format!("{:?}", msg));
  81. self
  82. }
  83. pub fn build(mut self) -> OTError { OTError::new(self.code, &self.msg.take().unwrap_or_else(|| "".to_owned())) }
  84. }
  85. pub(crate) fn internal_error<T>(e: T) -> OTError
  86. where
  87. T: std::fmt::Debug,
  88. {
  89. OTError::internal().context(e)
  90. }