当前位置: 首页 > news >正文

专门做试题的网站seo优化好做吗

专门做试题的网站,seo优化好做吗,用照片做视频的网站,在网站上做宣传随着项目的进展&#xff0c;关于Rust的故事又翻开了新的一页&#xff0c;今天来到了服务器端的开发场景&#xff0c;发现错误处理中的错误类型转换有必要分享一下。 Rust抽象出来了Result<T,E>&#xff0c;T是返回值的类型&#xff0c;E是错误类型。只要函数的返回值的类…

随着项目的进展,关于Rust的故事又翻开了新的一页,今天来到了服务器端的开发场景,发现错误处理中的错误类型转换有必要分享一下。
Rust抽象出来了Result<T,E>,T是返回值的类型,E是错误类型。只要函数的返回值的类型被定义为Resut<T,E>,那么作为开发人员就有责任来处理调用这个函数可能发生的错误。通过Result<T,E>,Rust其实给开发人员指明了一条错误处理的道路,使代码更加健壮。

场景

  1. 服务器端处理api请求的框架:Rocket
  2. 服务器端处理数据持久化的框架:tokio_postgres

在api请求的框架中,我把返回类型定义成了Result<T, rocket::response::status::Custom\<String>>,即错误类型是rocket::response::status::Custom\<String>
在tokio_postgres中,直接使用tokio_postgres::error::Error

即如果要处理错误,就必须将tokio_postgres::error::Error转换成rocket::response::status::Custom\<String>。那么我们从下面的原理开始,逐一领略Rust的错误处理方式,通过对比找到最合适的方式吧。

原理

对错误的处理,Rust有3种可选的方式

  1. 使用match
  2. 使用if let
  3. 使用map_err

下面我结合场景,逐一演示各种方式是如何处理错误的。
下面的代码中涉及到2个模块(文件)。/src/routes/notes.rs是路由层,负责将api请求导向合适的service。/src/services/note_books.rs是service层,负责业务逻辑和数据持久化的处理。这里的逻辑也很简单,就是route层调用service层,将数据写入到数据库中。

使用match

src/routes/notes.rs

#[post("/api/notes", format = "application/json", data = "<note>")]
pub async fn post_notes(note: Json<Note>) -> Result<(), rocket::response::status::Custom<String>> {insert_or_update_note(&note.into_inner()).await
}

/src/services/note_book.rs

pub async fn insert_or_update_note(note: &Note,
) -> Result<(), rocket::response::status::Custom<String>> {let (client, connection) = match connect("host=localhost dbname=notes_db user=postgres port=5432",NoTls,).await{Ok(res) => res,Err(err) => {return Err(rocket::response::status::Custom(rocket::http::Status::ExpectationFailed,format!("{}", err),));}};...match client.execute("insert into notes (id, title, content) values($1, $2, $3);",&[&get_system_seconds(), &note.title, &note.content],).await{Ok(res) => Ok(()),Err(err) => Err(rocket::response::status::Custom(rocket::http::Status::ExpectationFailed,format!("{}", err),)),}
}

通过上面的代码我们可以读出一下内容:

  1. 在service层定义了route层相同的错误类型
  2. 在service层将持久层的错误转换成了route层的错误类型
  3. 使用match的代码量还是比较大

使用if let

/src/services/note_book.rs

pub async fn insert_or_update_note(note: &Note,
) -> Result<(), rocket::response::status::Custom<String>> {if let Ok((client, connection)) = connect("host=localhost dbname=notes_db user=postgres port=5432",NoTls,).await{...if let Ok(res) = client.execute("insert into notes (id, title, content) values($1, $2, $3);",&[&get_system_seconds(), &note.title, &note.content],).await{Ok(())} else {Err(rocket::response::status::Custom(rocket::http::Status::ExpectationFailed,format!("{}", "unknown error"),))}} else {Err(rocket::response::status::Custom(rocket::http::Status::ExpectationFailed,format!("{}", "unknown error"),))}
}

src/routes/notes.rs

#[post("/api/notes", format = "application/json", data = "<note>")]
pub async fn post_notes(note: Json<Note>) -> Result<(), rocket::response::status::Custom<String>> {insert_or_update_note(&note.into_inner()).await
}

使用了if let ...,代码更加的别扭,并且在else分支中,拿不到具体的错误信息。

其实,不难看出,我们的目标是将api的请求,经过route层和service层,将数据写入到数据中。但这其中的错误处理代码的干扰就特别大,甚至要有逻辑嵌套现象。这种代码的已经离初衷比较远了,是否有更加简洁的方式,使代码能够最大限度的还原逻辑本身,把错误处理的噪音降到最低呢?
答案肯定是有的。那就是map_err

map_err

map_err是Result上的一个方法,专门用于错误的转换。下面的代码经过了map_err的改写,看上去是不是清爽了不少啊。
/src/services/note_book.rs

pub async fn insert_or_update_note(note: &Note,
) -> Result<(), rocket::response::status::Custom<String>> {let (client, connection) = connect("host=localhost dbname=notes_db user=postgres port=5432",NoTls,).await.map_err(|err| {rocket::response::status::Custom(rocket::http::Status::ExpectationFailed,format!("{}", err),)})?;...let _ = client.execute("insert into notes (id, title, content) values($1, $2, $3);",&[&get_system_seconds(), &note.title, &note.content],).await.map_err(|err| {rocket::response::status::Custom(rocket::http::Status::ExpectationFailed,format!("{}", err),)})?;Ok(())
}

src/routes/notes.rs

#[post("/api/notes", format = "application/json", data = "<note>")]
pub async fn post_notes(note: Json<Note>) -> Result<(), rocket::response::status::Custom<String>> {insert_or_update_note(&note.into_inner()).await
}

经过map_err改写后的代码,代码的逻辑流程基本上还原了逻辑本身,但是map_err要额外占4行代码,且错误对象的初始化代码存在重复。在实际的工程项目中,service层的处理函数可能是成百上千,如果再乘以4,那多出来的代码量也不少啊,这会给后期的维护带来不小的压力。

那是否还有改进的空间呢?答案是Yes。
Rust为我们提供了From<T> trait,用于类型转换。它定义了从一种类型T到另一种类型Self的转换方法。我觉得这是Rust语言设计亮点之一。
但是,Rust有一个显示,即实现From<T> trait的结构,必须有一个在当前的crate中,也就是说我们不能直接通过From<T>来实现从tokio_postgres::error::Errorrocket::response::status::Custom<String>。也就是说下面的代码编译器会报错。

impl From<tokio_postgres::Error> for rocket::response::status::Custom<String> {}

报错如下:

32 | impl From<tokio_postgres::Error> for rocket::response::status::Custom<String> {}| ^^^^^---------------------------^^^^^----------------------------------------| |    |                               || |    |                               `rocket::response::status::Custom` is not defined in the current crate| |    `tokio_postgres::Error` is not defined in the current crate| impl doesn't use only types from inside the current crate

因此,我们要定义一个类型MyError作为中间类型来转换一下。
/src/models.rs

pub struct MyError {pub message: String,
}
impl From<tokio_postgres::Error> for MyError {fn from(err: Error) -> Self {Self {message: format!("{}", err),}}
}
impl From<MyError> for rocket::response::status::Custom<String> {fn from(val: MyError) -> Self {status::Custom(Status::ExpectationFailed, val.message)}
}

/src/services/note_book.rs

pub async fn insert_or_update_note(note: &Note,
) -> Result<(), rocket::response::status::Custom<String>> {let (client, connection) = connect("host=localhost dbname=notes_db user=postgres port=5432",NoTls,).await.map_err(MyError::from)?;...let _ = client.execute("insert into notes (id, title, content) values($1, $2, $3);",&[&get_system_seconds(), &note.title, &note.content],).await.map_err(MyError::from)?;Ok(())
}

src/routes/notes.rs

#[post("/api/notes", format = "application/json", data = "<note>")]
pub async fn post_notes(note: Json<Note>) -> Result<(), rocket::response::status::Custom<String>> {insert_or_update_note(&note.into_inner()).await
}

MyErrorrocket::response::status::Custom<String>之间的转换是隐式的,由编译器来完成。因此我们的错误类型的转换最终缩短为map_err(|err|MyError::from(err)),再简写为map_err(MyError::from)

关于错误处理中的类型转换应用解析就到这里。通过分析这个过程,我们可以看到,在设计模块时,我们应该确定一种错误类型,就像tokio_postgres库一样,只暴露了tokio_postgress::error::Error一种错误类型。这种设计既方便我们在设计模块时处理错误转换,也方便其我们的模块在被调用时,其它代码进行错误处理。

http://www.hkea.cn/news/913323/

相关文章:

  • 响应式网站建设有利于seo网站发布与推广方案
  • 网页制作教程课件seo推广排名重要吗
  • 小规模纳税人企业所得税怎么征收广州seo招聘
  • 济南企业自助建站网络营销策划公司
  • iis 新建网站 要登录温州seo推广外包
  • 个人想做企业网站备案惠州seo代理商
  • 做公务员题的网站口红的推广软文
  • 福州网站建设 联系yanktcn 04上海百网优seo优化公司
  • 网站备案号如何获得网站建设营销推广
  • 物流网站开发公司西安 做网站
  • 商务信息网站怎么做网络视频营销策略有哪些
  • 社交做的最好的网站怎么开发一个网站
  • 教育品牌网站建设百度搜索推广和信息流推广
  • 虎门专业做网站对网络营销的认识有哪些
  • 投资理财培训网站建设抖音引流推广一个30元
  • 做景观设施的网站网络营销推广要求
  • 携程网站建设进度及实施过程网络营销的缺点及建议
  • 石家庄网站建设哪家专业中国联通腾讯
  • 能访问各种网站的浏览器百度一下网页搜索
  • 自己做网站花多少钱雅虎搜索
  • 哈尔滨招标信息网网站推广优化排名教程
  • 个人可以建论坛网站吗福清网络营销
  • 济南做网站优化价格百度推广网站一年多少钱
  • 做网上商城网站哪家好杭州seo靠谱
  • 做营销网站制作关键词优化课程
  • 网站移动终端建设口碑营销成功案例
  • 美国做试管婴儿 网站推广普通话宣传语
  • 网站备案信息查询系统软文发布平台媒体
  • 泊头哪给做网站的好制作网页的教程
  • 漳州建设银行网站首页在百度上打广告找谁