3 releases

0.0.3 Jun 2, 2023
0.0.2 May 13, 2023
0.0.1 May 13, 2023

#1410 in Development tools

Download history 1/week @ 2024-09-17 14/week @ 2024-09-24

62 downloads per month
Used in 2 crates

MIT/Apache

13KB
158 lines

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

Dependencies

~0.7–1.6MB
~34K SLoC