Explicit error http status codes (#1362)

* Trying to type specific errors.

* Using @asonix 's downcast method.
This commit is contained in:
Dessalines 2021-01-21 11:32:19 -05:00 committed by GitHub
parent a01af67948
commit ee03cf8ae9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 17 deletions

2
Cargo.lock generated
View file

@ -1920,7 +1920,9 @@ dependencies = [
"chrono", "chrono",
"comrak", "comrak",
"config", "config",
"diesel",
"futures", "futures",
"http",
"itertools", "itertools",
"lazy_static", "lazy_static",
"lettre", "lettre",

View file

@ -12,14 +12,14 @@
"api-test": "jest src/ -i --verbose" "api-test": "jest src/ -i --verbose"
}, },
"devDependencies": { "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", "jest": "^26.6.3",
"lemmy-js-client": "0.9.0-rc.12", "lemmy-js-client": "0.9.0-rc.12",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"ts-jest": "^26.4.4",
"prettier": "^2.1.2", "prettier": "^2.1.2",
"eslint": "^7.10.0", "ts-jest": "^26.4.4",
"eslint-plugin-jane": "^9.0.3",
"typescript": "^4.1.3" "typescript": "^4.1.3"
} }
} }

View file

@ -293,10 +293,10 @@
exec-sh "^0.3.2" exec-sh "^0.3.2"
minimist "^1.2.0" minimist "^1.2.0"
"@eslint/eslintrc@^0.2.2": "@eslint/eslintrc@^0.3.0":
version "0.2.2" version "0.3.0"
resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-0.2.2.tgz#d01fc791e2fc33e88a29d6f3dc7e93d0cd784b76" resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-0.3.0.tgz#d736d6963d7003b6514e6324bec9c602ac340318"
integrity sha512-EfB5OHNYp1F4px/LI/FEnGylop7nOqkQ1LRzCM0KccA2U8tvV8w01KBv37LbO7nW4H+YhKyo2LcJhRwjjV17QQ== integrity sha512-1JTKgrOKAHVivSvOYw+sJOunkBjUOvjqWk1DPja7ZFhIS2mX/4EgTT8M7eTK9jrKhL/FvXXEbQwIs3pg1xp3dg==
dependencies: dependencies:
ajv "^6.12.4" ajv "^6.12.4"
debug "^4.1.1" debug "^4.1.1"
@ -305,7 +305,7 @@
ignore "^4.0.6" ignore "^4.0.6"
import-fresh "^3.2.1" import-fresh "^3.2.1"
js-yaml "^3.13.1" js-yaml "^3.13.1"
lodash "^4.17.19" lodash "^4.17.20"
minimatch "^3.0.4" minimatch "^3.0.4"
strip-json-comments "^3.1.1" strip-json-comments "^3.1.1"
@ -590,7 +590,7 @@
dependencies: dependencies:
"@types/istanbul-lib-report" "*" "@types/istanbul-lib-report" "*"
"@types/jest@26.x", "@types/jest@^26.0.19": "@types/jest@26.x":
version "26.0.19" version "26.0.19"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.19.tgz#e6fa1e3def5842ec85045bd5210e9bb8289de790" resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.19.tgz#e6fa1e3def5842ec85045bd5210e9bb8289de790"
integrity sha512-jqHoirTG61fee6v6rwbnEuKhpSKih0tuhqeFbCmMmErhtu3BYlOZaXWjffgOstMM4S/3iQD31lI5bGLTrs97yQ== integrity sha512-jqHoirTG61fee6v6rwbnEuKhpSKih0tuhqeFbCmMmErhtu3BYlOZaXWjffgOstMM4S/3iQD31lI5bGLTrs97yQ==
@ -598,6 +598,14 @@
jest-diff "^26.0.0" jest-diff "^26.0.0"
pretty-format "^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": "@types/json-schema@^7.0.3":
version "7.0.6" version "7.0.6"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0" 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" resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.0.0.tgz#21fdc8fbcd9c795cc0321f0563702095751511a8"
integrity sha512-QudtT6av5WXels9WjIM7qz1XD1cWGvX4gGXvp/zBn9nXG02D0utdU3Em2m/QjTnrsk6bBjmCygl3rmj118msQQ== integrity sha512-QudtT6av5WXels9WjIM7qz1XD1cWGvX4gGXvp/zBn9nXG02D0utdU3Em2m/QjTnrsk6bBjmCygl3rmj118msQQ==
eslint@^7.10.0: eslint@^7.18.0:
version "7.17.0" version "7.18.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.17.0.tgz#4ccda5bf12572ad3bf760e6f195886f50569adb0" resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.18.0.tgz#7fdcd2f3715a41fe6295a16234bd69aed2c75e67"
integrity sha512-zJk08MiBgwuGoxes5sSQhOtibZ75pz0J35XTRlZOk9xMffhpA9BTbQZxoXZzOl5zMbleShbGwtw+1kGferfFwQ== integrity sha512-fbgTiE8BfUJZuBeq2Yi7J3RB3WGUQ9PNuNbmgi6jt9Iv8qrkxfy19Ds3OpL1Pm7zg3BtTVhvcUZbIRQ0wmSjAQ==
dependencies: dependencies:
"@babel/code-frame" "^7.0.0" "@babel/code-frame" "^7.0.0"
"@eslint/eslintrc" "^0.2.2" "@eslint/eslintrc" "^0.3.0"
ajv "^6.10.0" ajv "^6.10.0"
chalk "^4.0.0" chalk "^4.0.0"
cross-spawn "^7.0.2" cross-spawn "^7.0.2"
@ -1855,7 +1863,7 @@ eslint@^7.10.0:
js-yaml "^3.13.1" js-yaml "^3.13.1"
json-stable-stringify-without-jsonify "^1.0.1" json-stable-stringify-without-jsonify "^1.0.1"
levn "^0.4.1" levn "^0.4.1"
lodash "^4.17.19" lodash "^4.17.20"
minimatch "^3.0.4" minimatch "^3.0.4"
natural-compare "^1.4.0" natural-compare "^1.4.0"
optionator "^0.9.1" optionator "^0.9.1"

View file

@ -31,3 +31,5 @@ tokio = { version = "0.3.6", features = ["sync"] }
strum = "0.20.0" strum = "0.20.0"
strum_macros = "0.20.1" strum_macros = "0.20.1"
futures = "0.3.8" futures = "0.3.8"
diesel = "1.4.5"
http = "0.2.2"

View file

@ -13,6 +13,7 @@ mod test;
pub mod utils; pub mod utils;
use crate::settings::Settings; use crate::settings::Settings;
use http::StatusCode;
use regex::Regex; use regex::Regex;
use thiserror::Error; 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::<diesel::result::Error>() {
Some(diesel::result::Error::NotFound) => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
lazy_static! { lazy_static! {
pub static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!( pub static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!(