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

上海公司网站建设哪家好网站建设网络推广公司

上海公司网站建设哪家好,网站建设网络推广公司,本地php网站搭建环境,如何做公司的网站建设随着项目的进展&#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.zhongyajixie.com/news/35887.html

相关文章:

  • 做ppt介绍网站百度一下你就知道了官网
  • 织梦做的网站图片显示不了济南网络推广
  • 广州网站建设加q.479185700河南网站建设定制
  • 黄浦网站建设网络营销策划书应该怎么写
  • 公司做网站一般用什么域名中国新闻发布
  • 东莞公司网站制作公司百度问答app下载
  • 长沙关键词优化推荐清理优化大师
  • 2级a做爰片免费网站杭州网站运营十年乐云seo
  • 做公司网站首页新闻软文范例大全
  • 盐城网站建设有限公司小广告图片
  • php做购物网站系统上海关键词优化公司哪家好
  • 竞馨门户网站开发国外外链平台
  • 常州网上教科院北京网站优化体验
  • 怎么给一个网站做搜索功能3小时百度收录新站方法
  • Mui框架做网站卖友情链接的哪来那么多网站
  • 博客网站源码带后台东莞seo
  • 烟台网站建设诚信臻动传媒营销渠道分为三种模式
  • 个人如果做网站赚钱sem竞价托管公司
  • 苏州网站开发找薇网站如何建立
  • php做的网站后台seo试用软件
  • 苍南龙港做网站店铺惠州seo计费管理
  • 互联网站建设维护是做什么的营销方案范文
  • 鹤壁做网站哪家好百度推广获客
  • 网站的开发工具和运行环境谷歌竞价广告
  • 景山网站建设公司网站综合查询工具
  • 网站理念google搜索引擎入口下载
  • 平面设计网课哪个机构好seo包年优化费用
  • h5如何做多页面网站开封网站推广
  • 做结婚请柬网站有那些一个网站推广
  • 网络网站建设电话天津百度关键词seo