257 lines
6.3 KiB
Rust
257 lines
6.3 KiB
Rust
use std::{error::Error as StdError, fmt::Display};
|
|
|
|
use axum::{
|
|
response::{IntoResponse, Response},
|
|
Json,
|
|
};
|
|
use serde::Serialize;
|
|
|
|
use super::status::Status;
|
|
|
|
pub struct ResData<T>(pub Option<T>)
|
|
where
|
|
T: Serialize;
|
|
|
|
impl<T> IntoResponse for ResData<T>
|
|
where
|
|
T: Serialize,
|
|
{
|
|
fn into_response(self) -> Response {
|
|
let ResData(data) = self;
|
|
let status = Status::OK(data);
|
|
|
|
Json(status.to_reply()).into_response()
|
|
}
|
|
}
|
|
|
|
impl<T: Serialize> ResData<T> {
|
|
pub fn none() -> Self {
|
|
ResData(None)
|
|
}
|
|
pub fn some(data: T) -> Self {
|
|
ResData(Some(data))
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum ResErr {
|
|
Error(i32, String),
|
|
ErrParams(Option<String>),
|
|
ErrAuth(Option<String>),
|
|
ErrPerm(Option<String>),
|
|
ErrNotFound(Option<String>),
|
|
ErrSystem(Option<String>),
|
|
ErrData(Option<String>),
|
|
ErrService(Option<String>),
|
|
ErrSocial(Option<String>),
|
|
ErrAccountDisable(Option<String>),
|
|
ErrSqlx(Option<String>),
|
|
}
|
|
|
|
use ResErr::*;
|
|
|
|
impl IntoResponse for ResErr {
|
|
fn into_response(self) -> Response {
|
|
let status = match self {
|
|
Error(code, msg) => Status::<()>::Err(code, msg),
|
|
ErrParams(msg) => {
|
|
let code = 10000;
|
|
|
|
match msg {
|
|
Some(v) => Status::<()>::Err(code, v),
|
|
None => Status::<()>::Err(code, String::from("参数错误")),
|
|
}
|
|
}
|
|
ErrAuth(msg) => {
|
|
let code = 20000;
|
|
|
|
match msg {
|
|
Some(v) => Status::<()>::Err(code, v),
|
|
None => Status::<()>::Err(code, String::from("未授权,请先登录")),
|
|
}
|
|
}
|
|
ErrPerm(msg) => {
|
|
let code = 30000;
|
|
|
|
match msg {
|
|
Some(v) => Status::<()>::Err(code, v),
|
|
None => Status::<()>::Err(code, String::from("权限不足")),
|
|
}
|
|
}
|
|
ErrNotFound(msg) => {
|
|
let code = 40000;
|
|
|
|
match msg {
|
|
Some(v) => Status::<()>::Err(code, v),
|
|
None => Status::<()>::Err(code, String::from("数据不存在")),
|
|
}
|
|
}
|
|
ErrSystem(msg) => {
|
|
let code = 50000;
|
|
|
|
match msg {
|
|
Some(v) => Status::<()>::Err(code, v),
|
|
None => Status::<()>::Err(code, String::from("内部服务器错误,请稍后重试")),
|
|
}
|
|
}
|
|
ErrData(msg) => {
|
|
let code = 60000;
|
|
|
|
match msg {
|
|
Some(v) => Status::<()>::Err(code, v),
|
|
None => Status::<()>::Err(code, String::from("数据异常")),
|
|
}
|
|
}
|
|
ErrService(msg) => {
|
|
let code = 70000;
|
|
|
|
match msg {
|
|
Some(v) => Status::<()>::Err(code, v),
|
|
None => Status::<()>::Err(code, String::from("服务异常")),
|
|
}
|
|
}
|
|
ErrSocial(msg) => {
|
|
let code = 80000;
|
|
match msg {
|
|
Some(v) => Status::<()>::Err(code, v),
|
|
None => Status::<()>::Err(code, String::from("社交业务异常")),
|
|
}
|
|
}
|
|
ErrAccountDisable(msg) => {
|
|
let code = 90000;
|
|
match msg {
|
|
Some(v) => Status::<()>::Err(code, v),
|
|
None => Status::<()>::Err(code, String::from("账户已禁用")),
|
|
}
|
|
}
|
|
ErrSqlx(msg) => {
|
|
let code = 90000;
|
|
match msg {
|
|
Some(v) => Status::<()>::Err(code, v),
|
|
None => Status::<()>::Err(code, String::from("账户已禁用")),
|
|
}
|
|
}
|
|
};
|
|
|
|
Json(status.to_reply()).into_response()
|
|
}
|
|
}
|
|
|
|
impl Display for ResErr {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
// write!(f, "{:?}", self.message)
|
|
match self {
|
|
Error(value, message) => write!(f, "{} - {:?}", value, message),
|
|
ErrParams(message)
|
|
| ErrAuth(message)
|
|
| ErrPerm(message)
|
|
| ErrNotFound(message)
|
|
| ErrSystem(message)
|
|
| ErrData(message)
|
|
| ErrService(message)
|
|
| ErrSocial(message)
|
|
| ErrAccountDisable(message)
|
|
| ErrSqlx(message) => write!(f, "{:?}", message),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl StdError for ResErr {}
|
|
|
|
impl From<String> for ResErr {
|
|
fn from(value: String) -> Self {
|
|
ErrSystem(Some(value))
|
|
}
|
|
}
|
|
|
|
impl From<&str> for ResErr {
|
|
fn from(value: &str) -> Self {
|
|
ResErr::from(value.to_string())
|
|
}
|
|
}
|
|
|
|
impl From<sqlx::Error> for ResErr {
|
|
fn from(value: sqlx::Error) -> Self {
|
|
ErrSqlx(Some(value.to_string()))
|
|
}
|
|
}
|
|
|
|
impl From<validator::ValidationErrors> for ResErr {
|
|
fn from(value: validator::ValidationErrors) -> Self {
|
|
ErrParams(Some(value.to_string()))
|
|
}
|
|
}
|
|
|
|
impl Default for ResErr {
|
|
fn default() -> Self {
|
|
ErrSystem(None)
|
|
}
|
|
}
|
|
|
|
impl ResErr {
|
|
pub fn error<T>(code: i32, msg: T) -> Self
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
Error(code, msg.into())
|
|
}
|
|
|
|
pub fn params<T>(msg: T) -> Self
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
ErrParams(Some(msg.into()))
|
|
}
|
|
|
|
pub fn auth<T>(msg: T) -> Self
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
ErrAuth(Some(msg.into()))
|
|
}
|
|
|
|
pub fn perm<T>(msg: T) -> Self
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
ErrPerm(Some(msg.into()))
|
|
}
|
|
|
|
pub fn not_found<T>(msg: T) -> Self
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
ErrNotFound(Some(msg.into()))
|
|
}
|
|
|
|
pub fn system<T>(msg: T) -> Self
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
ErrSystem(Some(msg.into()))
|
|
}
|
|
|
|
pub fn data<T>(msg: T) -> Self
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
ErrData(Some(msg.into()))
|
|
}
|
|
|
|
pub fn service<T>(msg: T) -> Self
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
ErrService(Some(msg.into()))
|
|
}
|
|
|
|
pub fn social<T>(msg: T) -> Self
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
ErrSocial(Some(msg.into()))
|
|
}
|
|
}
|
|
|
|
pub type ResResult<T> = std::result::Result<T, ResErr>;
|