Changeset View
Changeset View
Standalone View
Standalone View
src/api/replies.rs
//! # API standard and JSON responses. | //! # API standard and JSON responses. | ||||
//! | //! | ||||
//! This module provides useful traits and methods to craft API replies from an existing type. | //! This module provides useful traits and methods to craft API replies from an existing type. | ||||
use std::error::Error; | #[cfg(feature = "pgsql")] | ||||
use diesel::result::{DatabaseErrorInformation, DatabaseErrorKind, QueryResult}; | |||||
use diesel::result::DatabaseErrorInformation; | #[cfg(feature = "pgsql")] | ||||
use diesel::result::DatabaseErrorKind; | |||||
use diesel::result::Error as ResultError; | use diesel::result::Error as ResultError; | ||||
use diesel::result::QueryResult; | |||||
use rocket::http::Status; | use rocket::http::Status; | ||||
use rocket::response::Failure; | use rocket::response::Failure; | ||||
use rocket_contrib::Json; | use rocket_contrib::Json; | ||||
#[cfg(feature = "pgsql")] | |||||
use std::error::Error; | |||||
/* ------------------------------------------------------------- | /* ------------------------------------------------------------- | ||||
Custom types | Custom types | ||||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | ||||
pub type ApiJsonResponse<T> = Result<Json<T>, Failure>; | pub type ApiJsonResponse<T> = Result<Json<T>, Failure>; | ||||
/* ------------------------------------------------------------- | /* ------------------------------------------------------------- | ||||
API Response | API Response | ||||
:: Implementation for QueryResult (Diesel ORM) | :: Implementation for QueryResult (Diesel ORM) | ||||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | ||||
/// This trait allows to consume an object into an HTTP response. | /// This trait allows to consume an object into an HTTP response. | ||||
pub trait ApiResponse<T> { | pub trait ApiResponse<T> { | ||||
/// Consumes the value and creates a JSON or a Failure result response. | /// Consumes the value and creates a JSON or a Failure result response. | ||||
fn into_json_response(self) -> ApiJsonResponse<T>; | fn into_json_response(self) -> ApiJsonResponse<T>; | ||||
} | } | ||||
#[cfg(feature = "pgsql")] | |||||
impl<T> ApiResponse<T> for QueryResult<T> { | impl<T> ApiResponse<T> for QueryResult<T> { | ||||
/// Prepares an API response from a query result. | /// Prepares an API response from a query result. | ||||
/// | /// | ||||
/// The result is the data structure prepared by the Diesel ORM after a SELECT query | /// The result is the data structure prepared by the Diesel ORM after a SELECT query | ||||
/// with one result, for example using `first` method. You can also you use it to | /// with one result, for example using `first` method. You can also you use it to | ||||
/// parse the returning result (... RETURNING *), which is a default for Diesel after | /// parse the returning result (... RETURNING *), which is a default for Diesel after | ||||
/// an INSERT query. | /// an INSERT query. | ||||
/// | /// | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | /* ------------------------------------------------------------- | ||||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | ||||
/// This trait allows to consume an object into an HTTP failure response. | /// This trait allows to consume an object into an HTTP failure response. | ||||
pub trait FailureResponse { | pub trait FailureResponse { | ||||
/// Consumes the variable and creates a Failure response . | /// Consumes the variable and creates a Failure response . | ||||
fn into_failure_response(self) -> Failure; | fn into_failure_response(self) -> Failure; | ||||
} | } | ||||
#[cfg(feature = "pgsql")] | |||||
impl FailureResponse for ResultError { | impl FailureResponse for ResultError { | ||||
/// Consumes the error and creates a Failure 500 Internal server error response. | /// Consumes the error and creates a Failure 500 Internal server error response. | ||||
fn into_failure_response(self) -> Failure { | fn into_failure_response(self) -> Failure { | ||||
build_internal_server_error_response(self.description()) | build_internal_server_error_response(self.description()) | ||||
} | } | ||||
} | } | ||||
/* ------------------------------------------------------------- | /* ------------------------------------------------------------- | ||||
Helper methods to prepare API responses | Helper methods to prepare API responses | ||||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | ||||
pub fn build_internal_server_error_response(message: &str) -> Failure { | pub fn build_internal_server_error_response(message: &str) -> Failure { | ||||
warn!(target:"api", "{}", message); | warn!(target:"api", "{}", message); | ||||
Failure::from(Status::InternalServerError) | Failure::from(Status::InternalServerError) | ||||
} | } | ||||
#[cfg(feature = "pgsql")] | |||||
fn build_database_error_response(error_kind: DatabaseErrorKind, info: Box<dyn DatabaseErrorInformation>) -> Failure { | fn build_database_error_response(error_kind: DatabaseErrorKind, info: Box<dyn DatabaseErrorInformation>) -> Failure { | ||||
match error_kind { | match error_kind { | ||||
// Case IIIa - The query tries to do an INSERT violating an unique constraint | // Case IIIa - The query tries to do an INSERT violating an unique constraint | ||||
// e.g. two INSERT with the same unique value | // e.g. two INSERT with the same unique value | ||||
// We return a 409 Conflict | // We return a 409 Conflict | ||||
DatabaseErrorKind::UniqueViolation => Failure::from(Status::Conflict), | DatabaseErrorKind::UniqueViolation => Failure::from(Status::Conflict), | ||||
// Case IIIb - The query violated a foreign key constraint | // Case IIIb - The query violated a foreign key constraint | ||||
Show All 9 Lines |
Nasqueron DevCentral · If it had been much bigger the moon would have had a core of ice. · Powered by Phabricator