{"id":26366747,"url":"https://github.com/stifskere/actix_error_proc","last_synced_at":"2025-06-29T16:37:30.612Z","repository":{"id":275760316,"uuid":"927092075","full_name":"stifskere/actix_error_proc","owner":"stifskere","description":"A small crate to integrate thiserror with actix_web.","archived":false,"fork":false,"pushed_at":"2025-05-11T11:58:29.000Z","size":187,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-11T12:31:51.962Z","etag":null,"topics":["actix","actixweb","error","macros","thiserror"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/actix_error_proc","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/stifskere.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":"2025-02-04T11:47:11.000Z","updated_at":"2025-05-11T11:58:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"460a81e5-302c-4714-b9b7-b3e9d27da20b","html_url":"https://github.com/stifskere/actix_error_proc","commit_stats":null,"previous_names":["stifskere/actix_error_proc"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stifskere%2Factix_error_proc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stifskere%2Factix_error_proc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stifskere%2Factix_error_proc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stifskere%2Factix_error_proc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stifskere","download_url":"https://codeload.github.com/stifskere/actix_error_proc/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stifskere%2Factix_error_proc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259272355,"owners_count":22832118,"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","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":["actix","actixweb","error","macros","thiserror"],"created_at":"2025-03-16T20:46:13.118Z","updated_at":"2025-06-29T16:37:30.591Z","avatar_url":"https://github.com/stifskere.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# actix_error_proc\n`actix_error_proc` is a small library to integrate `thiserror` into `actix_web` routes\nwith procedural macros.\n\nThis library has two main macros as well as a `thiserror` re export under the `thiserror` feature.\n\n## `ActixError`\nThis macro is used together with `thiserror::Error` and it allows the user\nto add a few more attributes to the error enumerable. This macro in reality\nit simply implements `Into\u003cactix_web::HttpResponse\u003e` and `Into\u003cactix_web::Error\u003e`\nfor the sake of it being used in routes and collectors.\n\nA basic usage of this macro looks like this\n\n```rust\nuse actix_error_proc::{ActixError, Error}; // Error is a thiserror re export.\n\n#[derive(ActixError, Error, Debug)]\nenum SomeError {\n    #[error(\"Couldn't parse http body.\")]\n    #[http_status(BadRequest)]\n    InvalidBody,\n\n    // if no attribute is set the default is InternalServerError.\n    #[error(\"A database error occurred: {0:#}\")]\n    DatabaseError(#[from] /* ... */)\n}\n```\n\nBy default the response is simply the status code and the `#[error(\"...\")]` format\nas a body. But you can change that with the `transformer`.\n\nThere is another attribute you can add called `actix_error` at the enumerable level\nthat lets you change how the response will look, for now it only has the `transformer`\nvariable, but in a future it might have more things.\n\nAn example usage of the `transformer` variable looks like this\n\n```rust\nuse actix_error_proc::{ActixError, Error}; // Error is a thiserror re export.\n\n// This should not throw any error, the errors should be handled\n// at the request level.\nfn transform_error(mut res: HttpResponseBuilder, fmt: String) -\u003e HttpResponse {\n    res\n\t.insert_header((\"Test\", \"This is a test header\"))\n        .json(json!({\"error\": fmt})) // by default the response is the raw string.\n}\n\n#[derive(ActixError, Error, Debug)]\n#[actix_error(transformer = \"transform_error\")] // reference `transform_error` here.\nenum SomeError {\n\t// ...\n}\n```\n\nAll of this is to be used with the `proof_route` attribute.\n\n## `proof_route`\n\nThis attribute wraps an `actix_web` route changing it's result into a `Result\u003cHttpResponse, E: Into\u003cHttpResponse\u003e\u003e`\nwhere E is your custom enumerable that implements `Into\u003cHttpResponse\u003e` because of the `ActixError` derive macro.\n\nThere is a type alias for that Result which is `actix_error_proc::HttpResult\u003cE\u003e`.\n\nAn example usage of the `proof_route` procedural macro look like this\n\n```rust\nuse actix_error_proc::{ActixError, Error, HttpResult}; // Error is a thiserror re export.\nuse crate::models::user::User;\nuse actix_web::{main, App, HttpServer}\nuse serde_json::{from_slice, Error as JsonError};\nuse std::io::Error as IoError;\n\n// assuming we have a wrapped enum\n#[derive(ActixError, Error, Debug)]\nenum SomeError {\n    #[error(\"The body could not be parsed.\")]\n    #[status_code(BadRequest)]\n    InvalidBody(#[from] JsonError)\n}\n\n#[proof_route(post(\"/users\"))]\nasync fn some_route(body: Bytes) -\u003e HttpResult\u003cSomeError\u003e {\n    let user: User = from_slice(body)?; // notice the use of `?`.\n\n    // Do something with the user.\n\n    Ok(HttpResponse::NoContent()) // return Ok if everything went fine.\n}\n\nasync fn main() -\u003e IoError {\n    HttpServer::new(|| {\n        App::new()\n            .service(some_route) // we can register the route normally.\n    })\n        .bind((\"0.0.0.0\", 8080))?\n        .run()\n        .await\n}\n```\n\nThere is an extra attribute we can add to route collectors to override\nit's error status code, in the case we don't want the original status code\nor we didn't create the collector and the original error does not match our\nexpectations we can use `#[or]`, which lets us specify an error branch\nfrom any type instance that implements `Into\u003cHttpResponse\u003e`.\n\n```rust\n#[proof_route(post(\"/\"))]\nasync fn route(#[or(SomeError::InvalidUser)] user: Json\u003cUser\u003e) // ...\n```\n\nIn this case if `Json\u003cUser\u003e` fails while collecting from the http request\nwhatever `\u003cSomeError::InvalidUser as Into\u003cactix_web::HttpResponse\u003e\u003e.into()` returns\nwill be passed directly as a response for the route.\n\nIf you don't add the attribute, the request will be collected as normal and in the\ncase of any error the original error implementation for that collector will\nbe applied.\n## Contributing\n\nBefore making a blind pull request please, open an issue we can talk about it and\nthen if it's necessary you can make a pull request, I actively maintain this project\nso I'll read all the issues when possible.\n\nYou can also email me at `esteve@memw.es`. Thanks.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstifskere%2Factix_error_proc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstifskere%2Factix_error_proc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstifskere%2Factix_error_proc/lists"}