jsonerr

Crates.iojsonerr
lib.rsjsonerr
version0.0.3
sourcesrc
created_at2023-05-13 07:18:06.346647
updated_at2023-06-02 20:00:46.147142
descriptionjsonerr macro and builder
homepagehttps://codeberg.org/AlbanMinassian/errorjson
repositoryhttps://codeberg.org/AlbanMinassian/errorjson
max_upload_size
id863594
size16,980
Alban Minassian (AlbanMinassian)

documentation

https://docs.rs/jsonerr

README

README

crates.io docs.rs github

jsonerr help to crate json error

Usage

use jsonerr::*;
fn main() {
    let myjson = jsonerr!(code = "ERR001", message = "An error message");
    println!("{:#?}", myjson);
}

return

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

Embed caused error

use jsonerr::*;
fn main() {
    let error = "NaN".parse::<u32>().unwrap_err();
    let myjson = jsonerr!(
        code = "ERR002",
        message = "An error message",
        origin = error // <-- 'std::error::Error' only
    );
    println!("{:#?}", myjson);
}

return

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

If error message string like a json (example {"deep":"caused error","test":1})

{
    "iserr":true,
    "err":{
        "code":"ERR003",
        "message":"An error message",
        "origin": {
            "deep": "caused error",
            "test": 1
        }
    }
}

Embed payload

use jsonerr::*;
fn main() {
    let myjson = jsonerr!(
        code = "ERR002",
        message = "An error message",
        payload = serde_json::json!({ "more": "data" }) // <-- 'serde_json::Value' only
    );
    println!("{:#?}", myjson);
}

return

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

Examples

  • cargo run --example basic

 Why jsonerr ?

  • When you need a json with always .meta, .err and .iserr
  • When you need a .err with always .code, .message, .origin, payload
  • When you need embed quickly caused error
Commit count: 0

cargo fmt