Crate jsonerr

source ·
Expand description

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

Modules

Macros