繁体   English   中英

如何处理 Rust-Diesel 中的多个错误?

[英]How to handle multiple errors in Rust-Diesel?

我的 Rust 项目使用柴油和 postgres。

我现在正在努力解决的问题是,当我 insert_into 表时,可能会发生不同的错误,我想对不同的错误类型采取不同的操作。

这些是错误: Diesel DatabaseErrorKind

我想做这样的事情( 来源):

use std::fs::File;
use std::io::ErrorKind;

fn main() {
    let f = File::open("hello.txt");

    let f = match f {
        Ok(file) => file,
        Err(error) => match error.kind() {
            ErrorKind::NotFound => match File::create("hello.txt") {
                Ok(fc) => fc,
                Err(e) => panic!("Problem creating the file: {:?}", e),
            },
            other_error => {
                panic!("Problem opening the file: {:?}", other_error)
            }
        },
    };
}

问题是柴油错误没有 error.kind() 。

“错误类型”模式在某些情况下很有用,在这些情况下,所有错误都基本相同,或者有很多共同的信息/有效载荷,并且除了错误类型之外没有任何个性。 Diesel 使用这种模式来处理数据库错误(因此DatabaseErrorKind ),因为数据库引擎通常只提供一个错误标识符/代码和一堆元数据,但是哪个错误得到哪个元数据通常是未记录的并且随着时间的推移而变化。

然而,在顶层 Diesel 更准确地知道错误是什么以及每个错误可以发出什么信号,因此它使用直接错误 enun

所以你只是......匹配而不是匹配错误类型:

match r {
    Ok(r) => ...,
    Err(e) => match e {
        NotFound => ...,
        InvalidCString(_) => ...,
        [etc...]

也应该可以将匹配变平,例如

match r {
    Ok(r) => ...,
    Err(NotFound) => ...,
    Err(InvalidCString(_)) => ...,
    [etc...]

Rust 如何实现 diesel::Insertable<div id="text_translate"><p> 我正在尝试在 Rust 中实现特性diesel::Insertable<table 。</p><p> 在mail.rs</p><pre> diesel::insert_into(mailing_list).values( email // This is a String ).execute(connection);</pre><p> 这是因为以下代码导致此错误:</p><pre> error[E0277]: the trait bound `std::string::String: diesel::Insertable<table>` is not satisfied --> src/mail.rs:18:10 | 17 | diesel::insert_into(mailing_list).values( | ------ required by a bound introduced by this call 18 | email | ^^^^^ the trait `diesel::Insertable<table>` is not implemented for `std::string::String`</pre><p> 多看一下 Diesel 的文档,我相信我应该让自己成为一个派生可插入的结构</p><p>在models.rs</p><pre> #[derive(Insertable)] pub struct NewSubscriber { pub email: String }</pre><p> 但后来我得到</p><pre>but im getting this error error[E0433]: failed to resolve: use of undeclared crate or module `new_subscribers` --> src/models.rs:13:12 | 13 | pub struct NewSubscriber { | ^^^^^^^^^^^^^ use of undeclared crate or module `new_subscribers`</pre><p> 我很困惑为什么编译器说它找不到包含我正在尝试定义的结构的板条箱或模块。</p></div><table></table>

[英]Rust how to implement diesel::Insertable<table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 rust-diesel 上没有缓存 Rust Diesel 在多个条件下左连接 如何使用锈柴油进行查询分组 Rust柴油Postgres如何添加分页查询 Rust 如果表格行存在于柴油中,如何 select? Rust 如何实现 diesel::Insertable<div id="text_translate"><p> 我正在尝试在 Rust 中实现特性diesel::Insertable<table 。</p><p> 在mail.rs</p><pre> diesel::insert_into(mailing_list).values( email // This is a String ).execute(connection);</pre><p> 这是因为以下代码导致此错误:</p><pre> error[E0277]: the trait bound `std::string::String: diesel::Insertable<table>` is not satisfied --> src/mail.rs:18:10 | 17 | diesel::insert_into(mailing_list).values( | ------ required by a bound introduced by this call 18 | email | ^^^^^ the trait `diesel::Insertable<table>` is not implemented for `std::string::String`</pre><p> 多看一下 Diesel 的文档,我相信我应该让自己成为一个派生可插入的结构</p><p>在models.rs</p><pre> #[derive(Insertable)] pub struct NewSubscriber { pub email: String }</pre><p> 但后来我得到</p><pre>but im getting this error error[E0433]: failed to resolve: use of undeclared crate or module `new_subscribers` --> src/models.rs:13:12 | 13 | pub struct NewSubscriber { | ^^^^^^^^^^^^^ use of undeclared crate or module `new_subscribers`</pre><p> 我很困惑为什么编译器说它找不到包含我正在尝试定义的结构的板条箱或模块。</p></div><table></table> 如何处理 Rust 中未找到密钥的错误 如何在 Rust Diesel 中使用时间戳和间隔进行算术运算 如何在红豆杉或任何前端使用生锈柴油? 如何使用生锈柴油获得柱的平均值?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM