mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
Add a new trait to help return detailed JSON error message
This commit is contained in:
@@ -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(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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! {})
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user