JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Error handling refactor

This refactor aims to improve error handling throughout the project by refining the overarching error types and increasing usage of proper error handling. Replaced existing networked operation error with `NetworkOperationError`. `NetworkOperationError` does not forward any internal error details, which allows `OperationError` to grow into a better error type. `OperationError` now has support for storing real typed errors inside of it for better debugging. `IntoInternalError` is a trait which allows for easy conversion of error types into `OperationError::internal`.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨e02c03d

⁨giterated-daemon/src/authorization.rs⁩ - ⁨3914⁩ bytes
Raw
1 use std::fmt::Debug;
2
3 use crate::connection::wrapper::ConnectionState;
4 use giterated_models::error::OperationError;
5
6 use giterated_models::object::GiteratedObject;
7
8 use giterated_models::repository::{
9 Repository, RepositoryFileInspectRequest, RepositoryIssueLabelsRequest,
10 RepositoryIssuesCountRequest, RepositoryIssuesRequest,
11 };
12
13 use giterated_models::user::User;
14
15 use giterated_models::value::GetValueTyped;
16 use giterated_models::{object::ObjectRequest, settings::SetSetting, value::GiteratedObjectValue};
17
18 #[derive(Debug, thiserror::Error)]
19 #[error("unauthorized")]
20 pub struct UnauthorizedError;
21
22 #[async_trait::async_trait]
23 pub trait AuthorizedOperation<O: GiteratedObject, S> {
24 /// Authorizes the operation, returning whether the operation was
25 /// authorized or not.
26 async fn authorize(
27 &self,
28 authenticating_user: Option<&User>,
29 object: &O,
30 state: &mut S,
31 ) -> Result<bool, OperationError<UnauthorizedError>>;
32 }
33
34 #[async_trait::async_trait]
35 impl AuthorizedOperation<User, ConnectionState> for SetSetting {
36 async fn authorize(
37 &self,
38 _authenticating_user: Option<&User>,
39 _object: &User,
40 _state: &mut ConnectionState,
41 ) -> Result<bool, OperationError<UnauthorizedError>> {
42 // TODO
43 Ok(true)
44 }
45 }
46
47 #[async_trait::async_trait]
48 impl AuthorizedOperation<Repository, ConnectionState> for SetSetting {
49 async fn authorize(
50 &self,
51 _authenticating_user: Option<&User>,
52 _object: &Repository,
53 _state: &mut ConnectionState,
54 ) -> Result<bool, OperationError<UnauthorizedError>> {
55 // TODO
56 Ok(true)
57 }
58 }
59
60 #[async_trait::async_trait]
61 impl<V: GiteratedObjectValue + Send + Sync + Debug + Clone>
62 AuthorizedOperation<Repository, ConnectionState> for GetValueTyped<V>
63 {
64 async fn authorize(
65 &self,
66 _authenticating_user: Option<&User>,
67 _object: &Repository,
68 _state: &mut ConnectionState,
69 ) -> Result<bool, OperationError<UnauthorizedError>> {
70 // TODO
71 Ok(true)
72 }
73 }
74
75 #[async_trait::async_trait]
76 impl AuthorizedOperation<Repository, ConnectionState> for ObjectRequest {
77 async fn authorize(
78 &self,
79 _authenticating_user: Option<&User>,
80 _object: &Repository,
81 _state: &mut ConnectionState,
82 ) -> Result<bool, OperationError<UnauthorizedError>> {
83 // TODO
84 Ok(true)
85 }
86 }
87
88 #[async_trait::async_trait]
89 impl AuthorizedOperation<Repository, ConnectionState> for RepositoryFileInspectRequest {
90 async fn authorize(
91 &self,
92 _authenticating_user: Option<&User>,
93 _object: &Repository,
94 _state: &mut ConnectionState,
95 ) -> Result<bool, OperationError<UnauthorizedError>> {
96 // TODO
97 Ok(true)
98 }
99 }
100
101 #[async_trait::async_trait]
102 impl AuthorizedOperation<Repository, ConnectionState> for RepositoryIssuesRequest {
103 async fn authorize(
104 &self,
105 _authenticating_user: Option<&User>,
106 _object: &Repository,
107 _state: &mut ConnectionState,
108 ) -> Result<bool, OperationError<UnauthorizedError>> {
109 // TODO
110 Ok(true)
111 }
112 }
113
114 #[async_trait::async_trait]
115 impl AuthorizedOperation<Repository, ConnectionState> for RepositoryIssueLabelsRequest {
116 async fn authorize(
117 &self,
118 _authenticating_user: Option<&User>,
119 _object: &Repository,
120 _state: &mut ConnectionState,
121 ) -> Result<bool, OperationError<UnauthorizedError>> {
122 // TODO
123 Ok(true)
124 }
125 }
126
127 #[async_trait::async_trait]
128 impl AuthorizedOperation<Repository, ConnectionState> for RepositoryIssuesCountRequest {
129 async fn authorize(
130 &self,
131 _authenticating_user: Option<&User>,
132 _object: &Repository,
133 _state: &mut ConnectionState,
134 ) -> Result<bool, OperationError<UnauthorizedError>> {
135 // TODO
136 Ok(true)
137 }
138 }
139