From ee03cf8ae9cf705b973086bc088308c97fa0b4ef Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 21 Jan 2021 11:32:19 -0500 Subject: [PATCH] Explicit error http status codes (#1362) * Trying to type specific errors. * Using @asonix 's downcast method. --- Cargo.lock | 2 ++ api_tests/package.json | 8 ++++---- api_tests/yarn.lock | 32 ++++++++++++++++++++------------ crates/utils/Cargo.toml | 2 ++ crates/utils/src/lib.rs | 10 +++++++++- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 02394a329..70f9ec5b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1920,7 +1920,9 @@ dependencies = [ "chrono", "comrak", "config", + "diesel", "futures", + "http", "itertools", "lazy_static", "lettre", diff --git a/api_tests/package.json b/api_tests/package.json index 4ec227d58..3e74a5acc 100644 --- a/api_tests/package.json +++ b/api_tests/package.json @@ -12,14 +12,14 @@ "api-test": "jest src/ -i --verbose" }, "devDependencies": { - "@types/jest": "^26.0.19", + "@types/jest": "^26.0.20", + "eslint": "^7.18.0", + "eslint-plugin-jane": "^9.0.3", "jest": "^26.6.3", "lemmy-js-client": "0.9.0-rc.12", "node-fetch": "^2.6.1", - "ts-jest": "^26.4.4", "prettier": "^2.1.2", - "eslint": "^7.10.0", - "eslint-plugin-jane": "^9.0.3", + "ts-jest": "^26.4.4", "typescript": "^4.1.3" } } diff --git a/api_tests/yarn.lock b/api_tests/yarn.lock index 8daa32132..8b7c8af50 100644 --- a/api_tests/yarn.lock +++ b/api_tests/yarn.lock @@ -293,10 +293,10 @@ exec-sh "^0.3.2" minimist "^1.2.0" -"@eslint/eslintrc@^0.2.2": - version "0.2.2" - resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-0.2.2.tgz#d01fc791e2fc33e88a29d6f3dc7e93d0cd784b76" - integrity sha512-EfB5OHNYp1F4px/LI/FEnGylop7nOqkQ1LRzCM0KccA2U8tvV8w01KBv37LbO7nW4H+YhKyo2LcJhRwjjV17QQ== +"@eslint/eslintrc@^0.3.0": + version "0.3.0" + resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-0.3.0.tgz#d736d6963d7003b6514e6324bec9c602ac340318" + integrity sha512-1JTKgrOKAHVivSvOYw+sJOunkBjUOvjqWk1DPja7ZFhIS2mX/4EgTT8M7eTK9jrKhL/FvXXEbQwIs3pg1xp3dg== dependencies: ajv "^6.12.4" debug "^4.1.1" @@ -305,7 +305,7 @@ ignore "^4.0.6" import-fresh "^3.2.1" js-yaml "^3.13.1" - lodash "^4.17.19" + lodash "^4.17.20" minimatch "^3.0.4" strip-json-comments "^3.1.1" @@ -590,7 +590,7 @@ dependencies: "@types/istanbul-lib-report" "*" -"@types/jest@26.x", "@types/jest@^26.0.19": +"@types/jest@26.x": version "26.0.19" resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.19.tgz#e6fa1e3def5842ec85045bd5210e9bb8289de790" integrity sha512-jqHoirTG61fee6v6rwbnEuKhpSKih0tuhqeFbCmMmErhtu3BYlOZaXWjffgOstMM4S/3iQD31lI5bGLTrs97yQ== @@ -598,6 +598,14 @@ jest-diff "^26.0.0" pretty-format "^26.0.0" +"@types/jest@^26.0.20": + version "26.0.20" + resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.20.tgz#cd2f2702ecf69e86b586e1f5223a60e454056307" + integrity sha512-9zi2Y+5USJRxd0FsahERhBwlcvFh6D2GLQnY2FH2BzK8J9s9omvNHIbvABwIluXa0fD8XVKMLTO0aOEuUfACAA== + dependencies: + jest-diff "^26.0.0" + pretty-format "^26.0.0" + "@types/json-schema@^7.0.3": version "7.0.6" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0" @@ -1825,13 +1833,13 @@ eslint-visitor-keys@^2.0.0: resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.0.0.tgz#21fdc8fbcd9c795cc0321f0563702095751511a8" integrity sha512-QudtT6av5WXels9WjIM7qz1XD1cWGvX4gGXvp/zBn9nXG02D0utdU3Em2m/QjTnrsk6bBjmCygl3rmj118msQQ== -eslint@^7.10.0: - version "7.17.0" - resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.17.0.tgz#4ccda5bf12572ad3bf760e6f195886f50569adb0" - integrity sha512-zJk08MiBgwuGoxes5sSQhOtibZ75pz0J35XTRlZOk9xMffhpA9BTbQZxoXZzOl5zMbleShbGwtw+1kGferfFwQ== +eslint@^7.18.0: + version "7.18.0" + resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.18.0.tgz#7fdcd2f3715a41fe6295a16234bd69aed2c75e67" + integrity sha512-fbgTiE8BfUJZuBeq2Yi7J3RB3WGUQ9PNuNbmgi6jt9Iv8qrkxfy19Ds3OpL1Pm7zg3BtTVhvcUZbIRQ0wmSjAQ== dependencies: "@babel/code-frame" "^7.0.0" - "@eslint/eslintrc" "^0.2.2" + "@eslint/eslintrc" "^0.3.0" ajv "^6.10.0" chalk "^4.0.0" cross-spawn "^7.0.2" @@ -1855,7 +1863,7 @@ eslint@^7.10.0: js-yaml "^3.13.1" json-stable-stringify-without-jsonify "^1.0.1" levn "^0.4.1" - lodash "^4.17.19" + lodash "^4.17.20" minimatch "^3.0.4" natural-compare "^1.4.0" optionator "^0.9.1" diff --git a/crates/utils/Cargo.toml b/crates/utils/Cargo.toml index df4ae1b2b..052ceb206 100644 --- a/crates/utils/Cargo.toml +++ b/crates/utils/Cargo.toml @@ -31,3 +31,5 @@ tokio = { version = "0.3.6", features = ["sync"] } strum = "0.20.0" strum_macros = "0.20.1" futures = "0.3.8" +diesel = "1.4.5" +http = "0.2.2" diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index e0b8e3aa1..2cc29131f 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -13,6 +13,7 @@ mod test; pub mod utils; use crate::settings::Settings; +use http::StatusCode; use regex::Regex; use thiserror::Error; @@ -68,7 +69,14 @@ impl std::fmt::Display for LemmyError { } } -impl actix_web::error::ResponseError for LemmyError {} +impl actix_web::error::ResponseError for LemmyError { + fn status_code(&self) -> StatusCode { + match self.inner.downcast_ref::() { + Some(diesel::result::Error::NotFound) => StatusCode::NOT_FOUND, + _ => StatusCode::INTERNAL_SERVER_ERROR, + } + } +} lazy_static! { pub static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!(