mirror of https://github.com/zino-rs/zino
Remove all unwrap()
This commit is contained in:
parent
4e5e995e42
commit
975c641f4c
|
@ -7,7 +7,7 @@ pub(crate) async fn new(mut req: Request) -> zino::Result {
|
|||
let mut user = User::new();
|
||||
let mut res = req.model_validation(&mut user).await?;
|
||||
|
||||
let rows = user.upsert().await.unwrap();
|
||||
let rows = user.upsert().await.map_err(Rejection::from)?;
|
||||
let data = json!({
|
||||
"method": req.request_method(),
|
||||
"path": req.request_path(),
|
||||
|
@ -28,9 +28,7 @@ pub(crate) async fn list(req: Request) -> zino::Result {
|
|||
let mut query = Query::new();
|
||||
let mut res = req.query_validation(&mut query)?;
|
||||
|
||||
let users = User::find(query)
|
||||
.await
|
||||
.map_err(Rejection::internal_server_error)?;
|
||||
let users = User::find(query).await.map_err(Rejection::from)?;
|
||||
let data = json!({
|
||||
"users": users,
|
||||
});
|
||||
|
@ -50,9 +48,7 @@ pub(crate) async fn view(mut req: Request) -> zino::Result {
|
|||
let event = req.cloud_event("message", message);
|
||||
req.try_send(event)?;
|
||||
|
||||
let user = User::find_one(query)
|
||||
.await
|
||||
.map_err(Rejection::internal_server_error)?;
|
||||
let user = User::find_one(query).await.map_err(Rejection::from)?;
|
||||
|
||||
let state_data = req.state_data_mut();
|
||||
let counter = state_data
|
||||
|
|
|
@ -28,6 +28,8 @@ pub(super) fn every_20s(job_id: Uuid, job_data: &mut Map, _last_tick: DateTime)
|
|||
}
|
||||
|
||||
pub(super) fn every_30s(job_id: Uuid, job_data: &mut Map, _last_tick: DateTime) -> BoxFuture {
|
||||
tracing::info_span!("count_users", %job_id);
|
||||
|
||||
let counter = job_data
|
||||
.get("counter")
|
||||
.map(|c| c.as_u64().unwrap_or_default() + 1)
|
||||
|
@ -42,7 +44,9 @@ pub(super) fn every_30s(job_id: Uuid, job_data: &mut Map, _last_tick: DateTime)
|
|||
Box::pin(async {
|
||||
let query = Query::new();
|
||||
let columns = [("*", true), ("roles", true)];
|
||||
let mut map = User::count(query, columns).await.unwrap();
|
||||
job_data.append(&mut map);
|
||||
match User::count(query, columns).await {
|
||||
Ok(mut map) => job_data.append(&mut map),
|
||||
Err(err) => tracing::error!("failed to count users: {err}"),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -81,13 +81,12 @@ impl CloudEvent {
|
|||
self.data.to_string()
|
||||
}
|
||||
|
||||
/// Consumes the event and returns as a json object. Panics if it fails.
|
||||
/// Consumes the event and returns as a json object.
|
||||
#[must_use]
|
||||
pub fn into_map(self) -> Map {
|
||||
if let Value::Object(map) = serde_json::to_value(self).unwrap() {
|
||||
map
|
||||
} else {
|
||||
panic!("the cloud event cann't be converted to a json object");
|
||||
match serde_json::to_value(self) {
|
||||
Ok(Value::Object(map)) => map,
|
||||
_ => panic!("the cloud event cann't be converted to a json object"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,13 +16,12 @@ pub trait Model: Default + Serialize + DeserializeOwned {
|
|||
serde_json::from_value(Value::from(data))
|
||||
}
|
||||
|
||||
/// Consumes the model and returns as a json object. Panics if it fails.
|
||||
/// Consumes the model and returns as a json object.
|
||||
#[must_use]
|
||||
fn into_map(self) -> Map {
|
||||
if let Value::Object(map) = serde_json::to_value(self).unwrap() {
|
||||
map
|
||||
} else {
|
||||
panic!("the model cann't be converted to a json object");
|
||||
match serde_json::to_value(self) {
|
||||
Ok(Value::Object(map)) => map,
|
||||
_ => panic!("the model cann't be converted to a json object"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,6 +74,13 @@ impl From<Validation> for Rejection {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<sqlx::Error> for Rejection {
|
||||
/// Converts to this type from the input type `sqlx::Error`.
|
||||
fn from(err: sqlx::Error) -> Self {
|
||||
InternalServerError(Box::new(err))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Rejection {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
|
|
|
@ -48,9 +48,9 @@ pub fn schema_macro(item: TokenStream) -> TokenStream {
|
|||
let mut columns = Vec::new();
|
||||
if let Data::Struct(data) = input.data && let Fields::Named(fields) = data.fields {
|
||||
for field in fields.named.into_iter() {
|
||||
let name = field.ident.unwrap().to_string();
|
||||
let mut type_name = parser::get_type_name(&field.ty);
|
||||
if !type_name.is_empty() {
|
||||
if let Some(ident) = field.ident && !type_name.is_empty() {
|
||||
let name = ident.to_string();
|
||||
let mut default_value = None;
|
||||
let mut not_null = false;
|
||||
let mut index_type = None;
|
||||
|
|
|
@ -2,8 +2,7 @@ use syn::{Attribute, GenericArgument, Lit, Meta, NestedMeta, PathArguments, Type
|
|||
|
||||
/// Returns the Postgres type name as a str.
|
||||
pub(crate) fn get_type_name(ty: &Type) -> String {
|
||||
if let Type::Path(ty) = ty {
|
||||
let segment = ty.path.segments.last().unwrap();
|
||||
if let Type::Path(ty) = ty && let Some(segment) = ty.path.segments.last() {
|
||||
let type_name = segment.ident.to_string();
|
||||
if let PathArguments::AngleBracketed(ref generics) = segment.arguments {
|
||||
if let Some(GenericArgument::Type(ref ty)) = generics.args.first() {
|
||||
|
|
|
@ -19,7 +19,7 @@ axum-server = ["dep:axum", "dep:tokio", "dep:tokio-stream", "dep:tower", "dep:to
|
|||
[dependencies]
|
||||
async-trait = { version = "0.1.60" }
|
||||
axum = { version = "0.6.1", features = ["ws"], optional = true }
|
||||
cfg_if = { version = "1.0.0" }
|
||||
cfg-if = { version = "1.0.0" }
|
||||
futures = { version = "0.3.25" }
|
||||
http-types = { version = "2.12.0" }
|
||||
hyper = { version = "0.14.23" }
|
||||
|
|
|
@ -3,4 +3,4 @@ cfg_if::cfg_if! {
|
|||
pub(crate) mod axum_sse;
|
||||
pub(crate) mod axum_websocket;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,4 +4,4 @@ cfg_if::cfg_if! {
|
|||
pub(crate) mod tower_cors;
|
||||
pub(crate) mod tower_tracing;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue