errjson

Crates.ioerrjson
lib.rserrjson
version0.0.6
sourcesrc
created_at2023-05-08 09:50:46.715394
updated_at2023-06-04 16:36:39.105404
descriptionEasy complex and intuitive Rust Error json
homepagehttps://codeberg.org/AlbanMinassian/errorjson
repositoryhttps://codeberg.org/AlbanMinassian/errorjson
max_upload_size
id859645
size16,793
Alban Minassian (AlbanMinassian)

documentation

https://docs.rs/errjson

README

README

crates.io docs.rs github

ErrJson: easy complex and intuitive Rust Error json

Usage

use errjson::ErrJson;
fn myfnc() -> Result<i32, Box<dyn std::error::Error>> {
    ErrJson!(code = "ERR001", message = "An error message")
}

return a valid Rust Error with a json stringify

{
    "iserr":true,
    "err":{
        "code":"ERR001",
        "message":"An error message"
    }
}

Embed caused error

use errjson::ErrJson;
fn myfnc() -> Result<i32, Box<dyn std::error::Error>> {
    let error = "NaN".parse::<u32>().unwrap_err();
    ErrJson!(
        code = "ERR002",
        message = "An error message",
        origin = error // <-- 'std::error::Error' only
    )
}

return

{
    "iserr":true,
    "err":{
        "code":"ERR002",
        "message":"An error message",
        "origin": "invalid digit found in string"
    }
}

Deep error allowed (see example full example code)

{
    "meta": { "whoami": { "filename": "errjson/examples/full.rs", "function": "main", "line": 42 } },
    "iserr": true,
    "err": {
        "code": "ERR0001",
        "message": "Error when main() call myfnc()",
        "origin": { // rust 'caused' error 
            "meta": { "whoami": { "filename": "errjson/examples/full.rs", "function": "myfnc", "line": 26 } },
            "iserr": true,
            "err": {
                "code": "ERR0002",
                "message": "Error when myfnc() call mysubfnc()",
                "origin": { // rust 'caused' error 
                    "meta": { "whoami": { "filename": "errjson/examples/full.rs", "function": "mysubfnc", "line": 11 } },
                    "iserr": true,
                    "err": {
                        "code": "ERR003",
                        "message": "Error when mysubfnc() call mysubsubfnc()",
                        "origin": "No such file or directory (os error 2)", // native rust 'caused' error
                        "payload": { "more": { "complex": "data" } } // <-- add another data if you want

                    },
                }
            },
        }
    },
}

Embed payload

use errjson::ErrJson;
fn myfnc() -> Result<i32, Box<dyn std::error::Error>> {
    ErrJson!(
        code = "ERR003",
        message = "An error message",
        payload = serde_json::json!({ "more": "data" }) // <-- 'serde_json::Value' only
    )
}

return

{
    "iserr":true,
    "err":{
        "code":"ERR003",
        "message":"An error message",
        "payload": { "more": "data" }
    }
}

Examples

  • cargo run --example full
  • cargo run --example minimal

 Why ErrJson ?

Without ErrJson, you can can write basic Err like this, but hard with a complex json, not normalize and difficult to embed 'caused' error.

  • Err(r#"{"error":"json", ...}"#);
  • Err(serde_json::to_string(serde_json::json!({"error": "json", ...})));
Commit count: 0

cargo fmt