You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.4 KiB

3 years ago
3 years ago
  1. use actix_web::{HttpResponse, ResponseError};
  2. use bcrypt::BcryptError;
  3. use custom_error::custom_error;
  4. // use derive_more::Display;
  5. // use diesel::result::DatabaseErrorKind;
  6. use crate::models::api_response::*;
  7. use std::convert::From;
  8. // impl From<DBError> for DomainError {
  9. // fn from(error: DBError) -> DomainError {
  10. // // We only care about UniqueViolations
  11. // match error {
  12. // DBError::DatabaseError(kind, info) => {
  13. // let message = info.details().unwrap_or_else(|| info.message()).to_string();
  14. // match kind {
  15. // DatabaseErrorKind::UniqueViolation => DomainError::DuplicateValue(message),
  16. // _ => DomainError::GenericError(message),
  17. // }
  18. // }
  19. // _ => DomainError::GenericError(String::from("Some database error occured")),
  20. // }
  21. // }
  22. // }
  23. custom_error! { #[derive(new)] pub DomainError
  24. PwdHashError {source: BcryptError} = "Failed to hash password",
  25. FieldValidationError {message: String} = "Failed to validate one or more fields",
  26. DbError {source: diesel::result::Error} = "Database error",
  27. DbPoolError {source: r2d2::Error} = "Failed to get connection from pool",
  28. PasswordError {cause: String} = "Failed to validate password - {cause}",
  29. EntityDoesNotExistError {message: String} = "Entity does not exist - {message}",
  30. ThreadPoolError {message: String} = "Thread pool error - {message}",
  31. AuthError {message: String} = "Authentication Error - {message}"
  32. }
  33. impl ResponseError for DomainError {
  34. fn error_response(&self) -> HttpResponse {
  35. let err = self;
  36. match self {
  37. DomainError::PwdHashError { source: _ } => {
  38. HttpResponse::InternalServerError()
  39. .json(ApiResponse::failure(err.to_string()))
  40. }
  41. DomainError::DbError { source: _ } => {
  42. let _ = tracing::error!("{}", err);
  43. HttpResponse::InternalServerError()
  44. .json(ApiResponse::failure("Error in database".to_owned()))
  45. }
  46. DomainError::DbPoolError { source: _ } => {
  47. let _ = tracing::error!("{}", err);
  48. HttpResponse::InternalServerError().json(ApiResponse::failure(
  49. "Error getting database pool".to_owned(),
  50. ))
  51. }
  52. DomainError::PasswordError { cause: _ } => {
  53. HttpResponse::BadRequest()
  54. .json(ApiResponse::failure(err.to_string()))
  55. }
  56. DomainError::EntityDoesNotExistError { message: _ } => {
  57. HttpResponse::NotFound()
  58. .json(ApiResponse::failure(err.to_string()))
  59. }
  60. DomainError::ThreadPoolError { message: _ } => {
  61. let _ = tracing::error!("{}", err);
  62. HttpResponse::InternalServerError().json(ApiResponse::failure(
  63. "Thread pool error occurred".to_owned(),
  64. ))
  65. }
  66. DomainError::AuthError { message: _ } => HttpResponse::Forbidden()
  67. .json(ApiResponse::failure(err.to_string())),
  68. DomainError::FieldValidationError { message: _ } => {
  69. let _ = tracing::error!("{}", err);
  70. HttpResponse::BadRequest()
  71. .json(ApiResponse::failure(err.to_string()))
  72. }
  73. }
  74. }
  75. }