{"id":22353971,"url":"https://github.com/yesint/thiserror_string_context","last_synced_at":"2026-02-17T08:31:04.229Z","repository":{"id":254496857,"uuid":"846735400","full_name":"yesint/thiserror_string_context","owner":"yesint","description":"Repository for thiserror_string_context crate","archived":false,"fork":false,"pushed_at":"2024-09-12T12:43:00.000Z","size":16,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-20T13:07:43.683Z","etag":null,"topics":["error-handling","rust","thiserror"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/thiserror_string_context","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yesint.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-23T21:00:35.000Z","updated_at":"2024-09-12T12:43:03.000Z","dependencies_parsed_at":"2024-08-26T13:58:20.239Z","dependency_job_id":"640abf90-6854-40b1-84ff-d4a198af1188","html_url":"https://github.com/yesint/thiserror_string_context","commit_stats":null,"previous_names":["yesint/thiserror_string_context"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yesint/thiserror_string_context","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yesint%2Fthiserror_string_context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yesint%2Fthiserror_string_context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yesint%2Fthiserror_string_context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yesint%2Fthiserror_string_context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yesint","download_url":"https://codeload.github.com/yesint/thiserror_string_context/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yesint%2Fthiserror_string_context/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270108533,"owners_count":24528763,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-12T02:00:09.011Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["error-handling","rust","thiserror"],"created_at":"2024-12-04T13:10:37.649Z","updated_at":"2025-10-15T08:01:58.811Z","avatar_url":"https://github.com/yesint.png","language":"Rust","readme":"# thiserror_string_context\n\nThis crate extends [thiserror](https://crates.io/crates/thiserror/) with possibility to add string context to error enums.\nA typical usage is annotating `io::Error` with a file name, but it can be used in any case where a string annotation of the error is required.\n\n## Relation to other similar crates\nThis crate is not as flexible in adding the context as [anyhow](https://crates.io/crates/anyhow) or [snafu](https://crates.io/crates/snafu), but instead it has a neglegible overhead and is much more ergonomic to use. It only serves a single purpose: adding an explanatory string to the error enum generated with `thiserror`. It doesn't work for any error types other than enums and doesn't support any other context types than strings.\nIn contrast to the other crate with similar purpose - [thiserror-context](https://crates.io/crates/thiserror-context), this crate does not obliges the user to create two distinct error types with and without the context. Instead the hidded context variant is added to the error enum itself, which makes this solution more elegant and much easier to maintain.\n\n## Usage\nThe usage is very simple:\n ```rust\nuse thiserror::Error;\nuse thiserror_string_context::*;\n// Annotate your error enum with `string_context` attribute.\n// This will allow to use `MyError::with_context()` method\n// to add a string annotation to your errors.\n// You may add a custom error message where `{0}`\n// is your original error variant.\n#[string_context(\"Custom context message: {0}\")]\n#[derive(Error,Debug)]\nenum MyError {\n    #[error(\"Slight underflow happened!\")]\n    Underflow,\n    #[error(\"slight overflow happened!\")]\n    Overflow,\n    #[error(\"too far from correct!\")]\n    TooFar,\n}\nfn check_number(n: i32) -\u003e Result\u003c(),MyError\u003e {\n    match n {\n        42 =\u003e println!(\"Correct number!\"),\n        41 =\u003e return Err(MyError::Underflow),\n        43 =\u003e return Err(MyError::Overflow),\n        _ =\u003e return Err(MyError::TooFar),\n    }\n    Ok(())\n}\nfn initiate_error() -\u003e anyhow::Result\u003c()\u003e {\n    // Here we add a context message\n    check_number(41).with_context(|| \"Crashing with value 41\")?;\n    Ok(())\n}\n```\n\nThis crashes with the following message:\n```text\nCustom context message: Crashing with value 41\nCaused by:\n    Slight underflow happened!\n```\n\n## Matching on error enums with context\nWhen the context is added to the error enum a hidden variant is added to it, which makes matching on enum variants somewhat tedious. The method `unwrap_context` retuns a tuple where the first element is `Option\u003cString\u003e` containing the context (if there is any) and the second is the enum itself \"peeled\" from the context. This allows very simple matching:\n```rust\nif let Err(err) = initiate_error() {\n    // Run different actions on different error variants\n    match err.unwrap_context() {\n        // Different actions could be performed on the same\n        // variant with and without the context\n        (Some(ctx),MyError::Underflow) =\u003e {...},\n        (None,MyError::Underflow) =\u003e {...},\n        // The context could be ignored\n        (_,MyError::Overflow) =\u003e {...},\n        // The wildcard pattern is required\n        _ =\u003e {...},\n    }\n}\n```\n\nLicense: MIT OR Apache-2.0\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyesint%2Fthiserror_string_context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyesint%2Fthiserror_string_context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyesint%2Fthiserror_string_context/lists"}