Add a new trait to help return detailed JSON error message

This commit is contained in:
2022-09-20 14:06:34 +00:00
committed by GitHub
parent cbcd319aaf
commit bee6f4e4ba
6 changed files with 73 additions and 18 deletions

1
Cargo.lock generated
View File

@@ -1371,6 +1371,7 @@ dependencies = [
"anyhow",
"async-trait",
"atty",
"base64",
"bindgen",
"bytes",
"c_fixed_string",

View File

@@ -9,6 +9,7 @@ edition = "2018"
anyhow = { version = "1.0", optional = true }
async-trait = { version = "0.1", optional = true }
atty = "0.2"
base64 = { version = "0.13", optional = true }
bytes = { version = "1.2", optional = true }
c_fixed_string = { version = "0.2", optional = true }
cfg-if = "1"
@@ -54,7 +55,7 @@ db = ["anyhow", "async-trait", "bytes"]
db_all = ["db", "db_sqlite"]
db_sqlite = ["rusqlite"]
exif = ["bindgen", "c_fixed_string", "cmake", "link-cplusplus", "utf16string"]
server = ["async-trait", "db", "hyper", "multipart", "openssl"]
server = ["async-trait", "base64", "db", "hyper", "multipart", "openssl"]
ugoira = ["avdict", "bindgen", "cmake", "link-cplusplus"]
[profile.release-with-debug]

View File

@@ -1,3 +1,6 @@
#[cfg(feature = "server")]
use crate::server::result::JSONError;
/// Try with custom error message
pub trait TryErr2<T, E> {
/// try with custom error message
@@ -10,6 +13,15 @@ pub trait TryErr<T, E> {
fn try_err(self, err: E) -> Result<T, E>;
}
#[cfg(feature = "server")]
/// A quick way to return detailed JSON error
pub trait TryErr3<T> {
/// A quick way to return detailed JSON error
/// * `code` - error code
/// * `msg` - error message
fn try_err3<S: AsRef<str> + ?Sized>(self, code: i32, msg: &S) -> Result<T, JSONError>;
}
impl<T: ToOwned + ToOwned<Owned = T>, E> TryErr2<T, E> for Option<T> {
fn try_err2(&self, v: E) -> Result<T, E> {
match self {
@@ -46,3 +58,20 @@ impl<E> TryErr<(), E> for bool {
}
}
}
#[cfg(feature = "server")]
impl<T, E> TryErr3<T> for Result<T, E>
where
E: std::fmt::Debug + std::fmt::Display,
{
fn try_err3<S: AsRef<str> + ?Sized>(self, code: i32, msg: &S) -> Result<T, JSONError> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(JSONError::from((
code,
format!("{} {}", msg.as_ref(), e),
format!("{:?}", e),
))),
}
}
}

View File

@@ -1,5 +1,5 @@
use super::super::preclude::*;
use crate::ext::{json::ToJson2, try_err::TryErr};
use crate::ext::{json::ToJson2, try_err::TryErr3};
use crate::gettext;
use bytes::BytesMut;
use chrono::{DateTime, Utc};
@@ -59,18 +59,17 @@ impl AuthPubkeyContext {
Some(key) => {
if key.is_too_old() {
*rsa_key =
Some(RSAKey::new().try_err((1, gettext("Failed to generate RSA key.")))?);
Some(RSAKey::new().try_err3(1, gettext("Failed to generate RSA key:"))?);
}
}
None => {
*rsa_key =
Some(RSAKey::new().try_err((1, gettext("Failed to generate RSA key.")))?);
*rsa_key = Some(RSAKey::new().try_err3(1, gettext("Failed to generate RSA key:"))?);
}
}
let rsa_key = rsa_key.as_ref().unwrap();
let key = rsa_key.key.public_key_to_pem().try_err((2, gettext("Failed to serializes the public key into a PEM-encoded SubjectPublicKeyInfo structure")))?;
let key = rsa_key.key.public_key_to_pem().try_err3(2, gettext("Failed to serializes the public key into a PEM-encoded SubjectPublicKeyInfo structure:"))?;
Ok(json::object! {
"key": String::from_utf8(key).try_err((3, gettext("Failed to encode pem with UTF-8.")))?,
"key": String::from_utf8(key).try_err3(3, gettext("Failed to encode pem with UTF-8:"))?,
"generated_time": rsa_key.generated_time.timestamp(),
})
}

View File

@@ -1,6 +1,6 @@
use super::super::preclude::*;
use crate::ext::json::ToJson2;
use crate::ext::try_err::TryErr;
use crate::ext::try_err::{TryErr, TryErr3};
use crate::gettext;
#[derive(Clone, Debug)]
@@ -40,6 +40,11 @@ impl AuthUserContext {
let username = params
.get("username")
.try_err((2, gettext("No username specified.")))?;
let password = params
.get("password")
.try_err((3, gettext("No password specified.")))?;
let password = base64::decode(password)
.try_err3(4, gettext("Failed to decode password with base64:"))?;
Ok(json::object! {})
}
},

View File

@@ -59,23 +59,43 @@ where
}
}
impl<S, V> From<(i32, &S, &V)> for JSONError
where
S: AsRef<str> + ?Sized,
V: AsRef<str> + ?Sized,
{
fn from((code, msg, debug_msg): (i32, &S, &V)) -> Self {
Self {
code,
msg: msg.as_ref().to_owned(),
debug_msg: Some(debug_msg.as_ref().to_json2()),
}
}
}
impl From<(i32, String, String)> for JSONError {
fn from((code, msg, debug_msg): (i32, String, String)) -> Self {
Self {
code,
msg,
debug_msg: Some(debug_msg.to_json2()),
}
}
}
impl From<crate::db::PixivDownloaderDbError> for JSONError {
fn from(e: crate::db::PixivDownloaderDbError) -> Self {
Self {
code: -1001,
msg: format!("{} {}", gettext("Failed to operate the database:"), e),
debug_msg: Some(format!("{:?}", e).to_json2()),
}
Self::from((
-1001,
format!("{} {}", gettext("Failed to operate the database:"), e),
format!("{:?}", e),
))
}
}
impl From<crate::error::PixivDownloaderError> for JSONError {
fn from(e: crate::error::PixivDownloaderError) -> Self {
Self {
code: -500,
msg: format!("{}", e),
debug_msg: Some(format!("{:?}", e).to_json2()),
}
Self::from((-500, format!("{}", e), format!("{:?}", e)))
}
}