Skip to content

Commit

Permalink
remove httperror
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Apr 7, 2024
1 parent 28402b3 commit e5bbbd3
Show file tree
Hide file tree
Showing 19 changed files with 52 additions and 109 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions crates/kitsune-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ build = "build.rs"
[dependencies]
async-trait = "0.1.79"
const_format = "0.2.32"
http = "1.1.0"
kitsune-db = { path = "../kitsune-db" }
kitsune-error = { path = "../kitsune-error" }
serde = { version = "1.0.197", features = ["derive"] }
thiserror = "1.0.58"
typed-builder = "0.18.1"

[build-dependencies]
Expand Down
48 changes: 0 additions & 48 deletions crates/kitsune-core/src/error.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/kitsune-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod consts;
pub mod error;
pub mod traits;
7 changes: 2 additions & 5 deletions crates/kitsune-error/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ pub trait ResultExt<T>: sealed::Sealed {

impl<T, E> ResultExt<T> for Result<T, E>
where
E: Into<eyre::Report>,
E: Into<Error>,
{
#[inline]
fn with_error_type(self, ty: ErrorType) -> Result<T, Error> {
self.map_err(|err| Error {
ty,
inner: err.into(),
})
self.map_err(|err| err.into().with_error_type(ty))
}

Check warning on line 20 in crates/kitsune-error/src/ext.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/ext.rs#L18-L20

Added lines #L18 - L20 were not covered by tests
}
4 changes: 2 additions & 2 deletions kitsune/src/http/extractor/signed_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use diesel::{ExpressionMethods, QueryDsl, SelectableHelper};
use diesel_async::RunQueryDsl;
use http::StatusCode;
use http_body_util::BodyExt;
use kitsune_core::{error::HttpError, traits::fetcher::AccountFetchOptions};
use kitsune_core::traits::fetcher::AccountFetchOptions;
use kitsune_db::{model::account::Account, schema::accounts, with_connection, PgPool};
use kitsune_error::{bail, Error, ErrorType, Result};
use kitsune_type::ap::Activity;
Expand Down Expand Up @@ -123,7 +123,7 @@ async fn verify_signature(
// Otherwise a random person with a key that's known to the database could start signing activities willy-nilly and the server would accept it.
if let Some(expected_account) = expected_account {
if expected_account.url != remote_user.url {
return Err(HttpError::Unauthorised.into());
bail!(type = ErrorType::Unauthorized, "remote account isn't the author of the activity");

Check warning on line 126 in kitsune/src/http/extractor/signed_activity.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/extractor/signed_activity.rs#L126

Added line #L126 was not covered by tests
}
}

Expand Down
5 changes: 2 additions & 3 deletions kitsune/src/http/handler/mastodon/api/v1/accounts/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use axum::{
extract::{Path, State},
Json,
};
use kitsune_core::error::HttpError;
use kitsune_error::Result;
use kitsune_error::{bail, ErrorType, Result};
use kitsune_mastodon::MastodonMapper;
use kitsune_service::account::{AccountService, Follow};
use kitsune_type::mastodon::relationship::Relationship;
Expand Down Expand Up @@ -39,7 +38,7 @@ pub async fn post(
follow_body: Option<AgnosticForm<FollowBody>>,
) -> Result<Json<Relationship>> {
if user_data.account.id == id {
return Err(HttpError::BadRequest.into());
bail!(type = ErrorType::BadRequest(None), "user tried to follow themselves");

Check warning on line 41 in kitsune/src/http/handler/mastodon/api/v1/accounts/follow.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/accounts/follow.rs#L41

Added line #L41 was not covered by tests
}

let follow = Follow::builder()
Expand Down
5 changes: 2 additions & 3 deletions kitsune/src/http/handler/mastodon/api/v1/accounts/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use axum::{
extract::{Query, State},
Json,
};
use kitsune_core::error::HttpError;
use kitsune_error::Result;
use kitsune_error::{kitsune_error, ErrorType, Result};
use kitsune_mastodon::MastodonMapper;
use kitsune_service::account::{AccountService, GetUser};
use kitsune_type::mastodon::Account;
Expand Down Expand Up @@ -50,7 +49,7 @@ pub async fn get(
let account = account_service
.get(get_user)
.await?
.ok_or(HttpError::NotFound)?;
.ok_or_else(|| kitsune_error!(type = ErrorType::NotFound, "account not found"))?;

Check warning on line 52 in kitsune/src/http/handler/mastodon/api/v1/accounts/lookup.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/accounts/lookup.rs#L52

Added line #L52 was not covered by tests

Ok(Json(mastodon_mapper.map(account).await?))
}
5 changes: 2 additions & 3 deletions kitsune/src/http/handler/mastodon/api/v1/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use axum::{
extract::{Path, State},
routing, Json, Router,
};
use kitsune_core::error::HttpError;
use kitsune_error::Result;
use kitsune_error::{kitsune_error, ErrorType, Result};
use kitsune_mastodon::MastodonMapper;
use kitsune_service::account::AccountService;
use kitsune_type::mastodon;
Expand Down Expand Up @@ -34,7 +33,7 @@ async fn get(
let account = account_service
.get_by_id(id)
.await?
.ok_or(HttpError::NotFound)?;
.ok_or_else(|| kitsune_error!(type = ErrorType::NotFound, "account not found"))?;

Check warning on line 36 in kitsune/src/http/handler/mastodon/api/v1/accounts/mod.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/accounts/mod.rs#L36

Added line #L36 was not covered by tests

Ok(Json(mastodon_mapper.map(account).await?))
}
Expand Down
5 changes: 2 additions & 3 deletions kitsune/src/http/handler/mastodon/api/v1/accounts/unfollow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use axum::{
extract::{Path, State},
Json,
};
use kitsune_core::error::HttpError;
use kitsune_error::Result;
use kitsune_error::{bail, ErrorType, Result};
use kitsune_mastodon::MastodonMapper;
use kitsune_service::account::{AccountService, Unfollow};
use kitsune_type::mastodon::relationship::Relationship;
Expand All @@ -19,7 +18,7 @@ pub async fn post(
Path(id): Path<Uuid>,
) -> Result<Json<Relationship>> {
if user_data.account.id == id {
return Err(HttpError::BadRequest.into());
bail!(type = ErrorType::BadRequest(None), "user tried to unfollow themselves");

Check warning on line 21 in kitsune/src/http/handler/mastodon/api/v1/accounts/unfollow.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/accounts/unfollow.rs#L21

Added line #L21 was not covered by tests
}

let unfollow = Unfollow::builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use axum::{
extract::{Multipart, State},
Json,
};
use kitsune_core::error::HttpError;
use kitsune_error::Result;
use kitsune_error::{bail, kitsune_error, ErrorType, Result};
use kitsune_mastodon::MastodonMapper;
use kitsune_service::{
account::{AccountService, Update},
Expand Down Expand Up @@ -43,7 +42,7 @@ pub async fn patch(
"note" => update.note(field.text().await?),
"avatar" => {
let Some(content_type) = field.content_type().map(ToString::to_string) else {
return Err(HttpError::BadRequest.into());
bail!(type = ErrorType::BadRequest(None), "invalid content-type");

Check warning on line 45 in kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs#L45

Added line #L45 was not covered by tests
};
let stream = buffer_multipart_to_tempfile(&mut field).await?;

Expand All @@ -52,13 +51,13 @@ pub async fn patch(
.content_type(content_type)
.stream(stream)
.build()
.map_err(|_| HttpError::BadRequest)?;
.unwrap();

Check warning on line 54 in kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs#L54

Added line #L54 was not covered by tests

update.avatar(upload)
}
"header" => {
let Some(content_type) = field.content_type().map(ToString::to_string) else {
return Err(HttpError::BadRequest.into());
bail!(type = ErrorType::BadRequest(None), "invalid content-type");

Check warning on line 60 in kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs#L60

Added line #L60 was not covered by tests
};
let stream = buffer_multipart_to_tempfile(&mut field).await?;

Expand All @@ -67,7 +66,7 @@ pub async fn patch(
.content_type(content_type)
.stream(stream)
.build()
.map_err(|_| HttpError::BadRequest)?;
.unwrap();

Check warning on line 69 in kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs#L69

Added line #L69 was not covered by tests

update.header(upload)
}
Expand All @@ -76,7 +75,12 @@ pub async fn patch(
};
}

let update = update.build().map_err(|_| HttpError::BadRequest)?;
let update = update.build().map_err(|err| {
kitsune_error!(
type = ErrorType::BadRequest(Some(err.to_string())),
"missing upload field"
)
})?;

Check warning on line 83 in kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/accounts/update_credentials.rs#L78-L83

Added lines #L78 - L83 were not covered by tests
let account = account_service.update(update).await?;

Ok(Json(mastodon_mapper.map(account).await?))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use axum::{
extract::{Path, State},
Json,
};
use kitsune_core::error::HttpError;
use kitsune_error::Result;
use kitsune_error::{bail, ErrorType, Result};
use kitsune_mastodon::MastodonMapper;
use kitsune_service::account::{AccountService, FollowRequest};
use kitsune_type::mastodon::relationship::Relationship;
Expand All @@ -30,7 +29,7 @@ pub async fn post(
Path(id): Path<Uuid>,
) -> Result<Json<Relationship>> {
if user_data.account.id == id {
return Err(HttpError::BadRequest.into());
bail!(type = ErrorType::BadRequest(None), "user tried to accept a follow to themselves");

Check warning on line 32 in kitsune/src/http/handler/mastodon/api/v1/follow_requests/accept.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/follow_requests/accept.rs#L32

Added line #L32 was not covered by tests
}

let follow_request = FollowRequest::builder()
Expand All @@ -49,6 +48,6 @@ pub async fn post(
.await?,
))
} else {
Err(HttpError::BadRequest.into())
bail!(type = ErrorType::BadRequest(None), "follow request wasn't found in the database");

Check warning on line 51 in kitsune/src/http/handler/mastodon/api/v1/follow_requests/accept.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/follow_requests/accept.rs#L51

Added line #L51 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use axum::{
extract::{Path, State},
Json,
};
use kitsune_core::error::HttpError;
use kitsune_error::Result;
use kitsune_error::{bail, ErrorType, Result};
use kitsune_mastodon::MastodonMapper;
use kitsune_service::account::{AccountService, FollowRequest};
use kitsune_type::mastodon::relationship::Relationship;
Expand All @@ -30,7 +29,7 @@ pub async fn post(
Path(id): Path<Uuid>,
) -> Result<Json<Relationship>> {
if user_data.account.id == id {
return Err(HttpError::BadRequest.into());
bail!(type = ErrorType::BadRequest(None), "user tried to reject a follow to themselves");

Check warning on line 32 in kitsune/src/http/handler/mastodon/api/v1/follow_requests/reject.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/follow_requests/reject.rs#L32

Added line #L32 was not covered by tests
}

let follow_request = FollowRequest::builder()
Expand All @@ -49,6 +48,6 @@ pub async fn post(
.await?,
))
} else {
Err(HttpError::BadRequest.into())
bail!(type = ErrorType::BadRequest(None), "follow request wasn't found in the database");

Check warning on line 51 in kitsune/src/http/handler/mastodon/api/v1/follow_requests/reject.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/follow_requests/reject.rs#L51

Added line #L51 was not covered by tests
}
}
11 changes: 8 additions & 3 deletions kitsune/src/http/handler/mastodon/api/v1/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use axum::{
routing, Json, Router,
};
use futures_util::TryFutureExt;
use kitsune_core::error::HttpError;
use kitsune_error::{Error, Result};
use kitsune_error::{kitsune_error, Error, ErrorType, Result};
use kitsune_mastodon::MastodonMapper;
use kitsune_service::attachment::{AttachmentService, Update, Upload};
use kitsune_type::mastodon::MediaAttachment;
Expand Down Expand Up @@ -88,7 +87,13 @@ pub async fn post(
}
}

let upload = upload.build().map_err(|_| HttpError::BadRequest)?;
let upload = upload.build().map_err(|err| {
kitsune_error!(
type = ErrorType::BadRequest(Some(err.to_string())),
"not all fields were filled"
)
})?;

Check warning on line 95 in kitsune/src/http/handler/mastodon/api/v1/media.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/media.rs#L90-L95

Added lines #L90 - L95 were not covered by tests

let media_attachment = attachment_service.upload(upload).await?;
Ok(Json(mastodon_mapper.map(media_attachment).await?))
}
Expand Down
5 changes: 2 additions & 3 deletions kitsune/src/http/handler/mastodon/api/v1/notifications/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use axum::{
};
use axum_extra::extract::Query;
use futures_util::{TryFutureExt, TryStreamExt};
use kitsune_core::error::HttpError;
use kitsune_error::{Error, Result};
use kitsune_error::{kitsune_error, Error, ErrorType, Result};
use kitsune_mastodon::MastodonMapper;
use kitsune_service::notification::{GetNotifications, NotificationService};
use kitsune_type::mastodon::{notification::NotificationType, Notification};
Expand Down Expand Up @@ -121,7 +120,7 @@ pub async fn get_by_id(
let notification = notification_service
.get_notification_by_id(id, user_data.account.id)
.await?
.ok_or(HttpError::NotFound)?;
.ok_or_else(|| kitsune_error!(type = ErrorType::NotFound, "notification not found"))?;

Check warning on line 123 in kitsune/src/http/handler/mastodon/api/v1/notifications/mod.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/mastodon/api/v1/notifications/mod.rs#L123

Added line #L123 was not covered by tests

Ok(Json(mastodon_mapper.map(notification).await?))
}
Expand Down
5 changes: 2 additions & 3 deletions kitsune/src/http/handler/oidc/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use axum::{
};
use diesel::{ExpressionMethods, OptionalExtension, QueryDsl};
use diesel_async::RunQueryDsl;
use kitsune_core::error::HttpError;
use kitsune_db::{
schema::{oauth2_applications, users},
with_connection, PgPool,
};
use kitsune_error::Result;
use kitsune_error::{bail, ErrorType, Result};
use kitsune_oidc::OidcService;
use kitsune_service::user::{Register, UserService};
use serde::Deserialize;
Expand All @@ -29,7 +28,7 @@ pub async fn get(
Query(query): Query<CallbackQuery>,
) -> Result<Response> {
let Some(oidc_service) = oidc_service else {
return Err(HttpError::BadRequest.into());
bail!(type = ErrorType::BadRequest(None), "oidc not configured");

Check warning on line 31 in kitsune/src/http/handler/oidc/callback.rs

View check run for this annotation

Codecov / codecov/patch

kitsune/src/http/handler/oidc/callback.rs#L31

Added line #L31 was not covered by tests
};

let user_info = oidc_service.get_user_info(query.state, query.code).await?;
Expand Down
Loading

0 comments on commit e5bbbd3

Please sign in to comment.