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`.
parent: tbd commit: e02c03d
1 | |
2 | |
3 | pub use ; |
4 | pub use *; |
5 | |
6 | |
7 | |
8 | use ; |
9 | |
10 | use Debug; |
11 | use FutureExt; |
12 | use |
13 | , |
14 | |
15 | AuthenticationTokenRequest, Instance, RegisterAccountRequest, RepositoryCreateRequest, |
16 | , |
17 | , | GiteratedObject
18 | , | ObjectBackend
19 | , | GiteratedOperation
20 | , |
21 | , |
22 | , | User
23 | , | GetValue
24 | ; |
25 | |
26 | |
27 | |
28 | pub object_name: String, |
29 | pub operation_name: String, |
30 | |
31 | |
32 | |
33 | |
34 | pub object_kind: String, |
35 | pub value_kind: String, |
36 | |
37 | |
38 | |
39 | |
40 | L, |
41 | O: GiteratedObject, |
42 | D: GiteratedOperation<O>, |
43 | S: Send + Sync + Clone, |
44 | > |
45 | |
46 | ; |
47 | ; |
48 | |
49 | async |
50 | &self, |
51 | object: &O, |
52 | operation: D, |
53 | state: S, |
54 | operation_state: &StackOperationState, |
55 | ; |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | F: FnMut( |
62 | &O, |
63 | D, |
64 | S, |
65 | ) |
66 | -> |
67 | + Send |
68 | + Sync |
69 | + Clone, |
70 | O: GiteratedObject + Send + Sync, |
71 | D: + 'static, |
72 | : Send, | Failure
73 | S: Send + Sync + Clone + 'static, |
74 | |
75 | |
76 | operation_name |
77 | |
78 | |
79 | |
80 | object_name |
81 | |
82 | |
83 | async |
84 | &self, |
85 | object: &O, |
86 | operation: D, |
87 | state: S, |
88 | _operation_state: &StackOperationState, |
89 | |
90 | self.clone .await |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | F: FnMut( |
98 | &O, |
99 | D, |
100 | S, |
101 | O1, |
102 | ) |
103 | -> |
104 | + Send |
105 | + Sync |
106 | + Clone, |
107 | O: GiteratedObject + Send + Sync, |
108 | D: + 'static + Send + Sync, |
109 | : Send, | Failure
110 | S: Send + Sync + Clone + 'static, |
111 | O1: , |
112 | : , |
113 | |
114 | |
115 | operation_name |
116 | |
117 | |
118 | |
119 | object_name |
120 | |
121 | |
122 | async |
123 | &self, |
124 | object: &O, |
125 | operation: D, |
126 | state: S, |
127 | operation_state: &StackOperationState, |
128 | |
129 | let o1 = O1 from_state |
130 | .await |
131 | .as_internal_error?; |
132 | self.clone .await |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | |
139 | F: FnMut( |
140 | &O, |
141 | D, |
142 | S, |
143 | O1, |
144 | O2, |
145 | ) |
146 | -> |
147 | + Send |
148 | + Sync |
149 | + Clone, |
150 | O: GiteratedObject + Send + Sync, |
151 | D: + 'static + Send + Sync, |
152 | : Send, | Failure
153 | S: Send + Sync + Clone + 'static, |
154 | O1: , |
155 | : , |
156 | O2: , |
157 | : , |
158 | |
159 | |
160 | operation_name |
161 | |
162 | |
163 | |
164 | object_name |
165 | |
166 | |
167 | async |
168 | &self, |
169 | object: &O, |
170 | operation: D, |
171 | state: S, |
172 | operation_state: &StackOperationState, |
173 | |
174 | let o1 = O1 from_state |
175 | .await |
176 | .as_internal_error?; |
177 | let o2 = O2 from_state |
178 | .await |
179 | .as_internal_error?; |
180 | self.clone .await |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | F: FnMut( |
188 | &O, |
189 | D, |
190 | S, |
191 | O1, |
192 | O2, |
193 | O3, |
194 | ) |
195 | -> |
196 | + Send |
197 | + Sync |
198 | + Clone, |
199 | O: GiteratedObject + Send + Sync, |
200 | D: + 'static + Send + Sync, |
201 | : Send, | Failure
202 | S: Send + Sync + Clone + 'static, |
203 | O1: , |
204 | : , |
205 | O2: , |
206 | : , |
207 | O3: , |
208 | : , |
209 | |
210 | |
211 | operation_name |
212 | |
213 | |
214 | |
215 | object_name |
216 | |
217 | |
218 | async |
219 | &self, |
220 | object: &O, |
221 | operation: D, |
222 | state: S, |
223 | operation_state: &StackOperationState, |
224 | |
225 | let o1 = O1 from_state |
226 | .await |
227 | .as_internal_error?; |
228 | let o2 = O2 from_state |
229 | .await |
230 | .as_internal_error?; |
231 | let o3 = O3 from_state |
232 | .await |
233 | .as_internal_error?; |
234 | self.clone .await |
235 | |
236 | |
237 | |
238 | |
239 | func: |
240 | dyn Fn( |
241 | &(dyn Any + Send + Sync), |
242 | &(dyn Any + Send + Sync), |
243 | &(dyn Any + Send + Sync), |
244 | StackOperationState, |
245 | ) -> |
246 | |
247 | dyn |
248 | Output = |
249 | , |
250 | , |
251 | >, |
252 | >, |
253 | >, |
254 | > + Send |
255 | + Sync, |
256 | >, |
257 | state: , |
258 | |
259 | |
260 | |
261 | |
262 | A, |
263 | O: GiteratedObject + Send + Sync + 'static, |
264 | D: + 'static + Clone, |
265 | F: + 'static + Send + Sync + Clone, |
266 | S: GiteratedStackState + 'static, |
267 | > |
268 | handler: F, |
269 | state: S, |
270 | |
271 | |
272 | : Send + Sync, | Failure
273 | : Send + Sync, | Success
274 | |
275 | Self |
276 | func: Box new |
277 | let handler = handler.clone; |
278 | let state = state. .unwrap .clone; |
279 | let object: &O = object.downcast_ref .unwrap; |
280 | let operation: &D = operation.downcast_ref .unwrap; |
281 | let object = object.clone; |
282 | let operation = operation.clone; |
283 | async move |
284 | let result = handler |
285 | .handle |
286 | .await; |
287 | result |
288 | .map |
289 | .map_err |
290 | => | Operation
291 | Operation |
292 | |
293 | => | Internal
294 | Internal |
295 | |
296 | => Unhandled, | Unhandled
297 | |
298 | |
299 | .boxed_local |
300 | , |
301 | state: Box new, |
302 | |
303 | |
304 | |
305 | async |
306 | &self, |
307 | object: & , |
308 | operation: & , |
309 | operation_state: &StackOperationState, |
310 | |
311 | |
312 | as_ref, | .
313 | as_ref, | .
314 | self.state.as_ref, |
315 | operation_state.clone, |
316 | |
317 | .await |
318 | |
319 | |
320 | |
321 | |
322 | |
323 | type Error: ; |
324 | |
325 | async |
326 | object: &O, |
327 | operation: &D, |
328 | state: &StackOperationState, |
329 | ; |
330 | |
331 | |
332 | |
333 | |
334 | for Arc |
335 | |
336 | type Error = Infallible; |
337 | |
338 | async |
339 | _object: &O, |
340 | _operation: &D, |
341 | state: &StackOperationState, |
342 | |
343 | Ok |
344 | |
345 | |
346 | |
347 | |
348 | |
349 | for StackOperationState |
350 | |
351 | type Error = Infallible; |
352 | |
353 | async |
354 | _object: &O, |
355 | _operation: &D, |
356 | state: &StackOperationState, |
357 | |
358 | Ok |
359 | |
360 | |
361 | |
362 | |
363 | |
364 | ; |
365 | |
366 | |
367 | |
368 | for AuthenticatedUser |
369 | |
370 | type Error = MissingValue; |
371 | |
372 | async |
373 | _object: &O, |
374 | _operation: &D, |
375 | state: &StackOperationState, |
376 | |
377 | state |
378 | .user |
379 | .clone |
380 | .ok_or_else |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | for AuthenticatedInstance |
387 | |
388 | type Error = MissingValue; |
389 | |
390 | async |
391 | _object: &O, |
392 | _operation: &D, |
393 | state: &StackOperationState, |
394 | |
395 | state |
396 | .instance |
397 | .clone |
398 | .ok_or_else |
399 | |
400 | |
401 | |
402 | |
403 | |
404 | T: + Send + Sync, |
405 | O: GiteratedObject + Sync, |
406 | D: + Send + Sync, |
407 | > |
408 | |
409 | type Error = Infallible; |
410 | |
411 | async |
412 | object: &O, |
413 | operation: &D, |
414 | state: &StackOperationState, |
415 | |
416 | Ok |
417 | |
418 | |
419 | |
420 | |
421 | ; |
422 | |
423 | |
424 | ; |
425 | |
426 | |
427 | |
428 | type Error: ; |
429 | |
430 | async |
431 | &self, |
432 | authorize_for: &O, |
433 | state: &StackOperationState, |
434 | ; |
435 | |
436 | |
437 | |
438 | |
439 | type Error = Error; |
440 | |
441 | async |
442 | &self, |
443 | authorize_for: &O, |
444 | operation_state: &StackOperationState, |
445 | |
446 | Ok |
447 | .runtime |
448 | . |
449 | .await |
450 | .is_ok |
451 | |
452 | |
453 | |
454 | |
455 | |
456 | type Error = MissingValue; |
457 | |
458 | async |
459 | &self, |
460 | authorize_for: &User, |
461 | operation_state: &StackOperationState, |
462 | |
463 | let authenticated_user = operation_state.user.as_ref .ok_or_else?; |
464 | |
465 | Ok |
466 | |
467 | |
468 | |
469 | |
470 | |
471 | type Error = MissingValue; |
472 | |
473 | async |
474 | &self, |
475 | authorize_for: &User, |
476 | operation_state: &StackOperationState, |
477 | |
478 | let authenticated_user = operation_state.user.as_ref .ok_or_else?; |
479 | |
480 | Ok |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | type Error = Error; |
487 | |
488 | async |
489 | &self, |
490 | authorize_for: &Repository, |
491 | operation_state: &StackOperationState, |
492 | |
493 | let authenticated_user = operation_state |
494 | .user |
495 | .as_ref |
496 | .ok_or_else?; |
497 | |
498 | let mut object = operation_state |
499 | .runtime |
500 | . |
501 | .await |
502 | .map_err?; |
503 | |
504 | let access_list = object |
505 | . |
506 | .await |
507 | .map_err?; |
508 | |
509 | if access_list |
510 | .0 |
511 | .iter |
512 | .find |
513 | .is_some |
514 | |
515 | Ok |
516 | else |
517 | Ok |
518 | |
519 | |
520 | |
521 | |
522 | |
523 | |
524 | type Error = Error; |
525 | |
526 | async |
527 | &self, |
528 | authorize_for: &Repository, |
529 | operation_state: &StackOperationState, |
530 | |
531 | let authenticated_user = operation_state |
532 | .user |
533 | .as_ref |
534 | .ok_or_else?; |
535 | |
536 | let mut object = operation_state |
537 | .runtime |
538 | . |
539 | .await |
540 | .map_err?; |
541 | |
542 | let access_list = object |
543 | . |
544 | .await |
545 | .map_err?; |
546 | |
547 | if access_list |
548 | .0 |
549 | .iter |
550 | .find |
551 | .is_some |
552 | |
553 | Ok |
554 | else |
555 | Ok |
556 | |
557 | |
558 | |
559 | |
560 | |
561 | |
562 | type Error = Infallible; |
563 | |
564 | async |
565 | &self, |
566 | authorize_for: &Instance, |
567 | state: &StackOperationState, |
568 | |
569 | if state.our_instance == *authorize_for |
570 | Ok |
571 | else |
572 | Ok |
573 | |
574 | |
575 | |
576 | |
577 | |
578 | |
579 | type Error = Infallible; |
580 | |
581 | async |
582 | &self, |
583 | authorize_for: &Instance, |
584 | state: &StackOperationState, |
585 | |
586 | if state.our_instance == *authorize_for |
587 | Ok |
588 | else |
589 | Ok |
590 | |
591 | |
592 | |
593 | |
594 | |
595 | |
596 | type Error = Infallible; |
597 | |
598 | async |
599 | &self, |
600 | authorize_for: &Instance, |
601 | state: &StackOperationState, |
602 | |
603 | if state.our_instance == *authorize_for |
604 | Ok |
605 | else |
606 | Ok |
607 | |
608 | |
609 | |
610 | |
611 | |
612 | |
613 | type Error = UnauthorizedError; |
614 | |
615 | async |
616 | object: &User, |
617 | operation: &A, |
618 | state: &StackOperationState, |
619 | |
620 | // TODO |
621 | let authenticated = from_state |
622 | .await |
623 | .map_err?; |
624 | |
625 | match operation.authorize .await |
626 | Ok => |
627 | assert!; |
628 | |
629 | Err => return Err, |
630 | ; |
631 | |
632 | Ok |
633 | |
634 | |
635 | |
636 | |
637 | |
638 | for AuthorizedInstance |
639 | |
640 | type Error = UnauthorizedError; |
641 | |
642 | async |
643 | object: &Instance, |
644 | operation: &A, |
645 | state: &StackOperationState, |
646 | |
647 | //TODO |
648 | let authenticated = from_state |
649 | .await |
650 | .map_err?; |
651 | |
652 | match operation.authorize .await |
653 | Ok => |
654 | assert!; |
655 | |
656 | Err => return Err, |
657 | ; |
658 | |
659 | Ok |
660 | |
661 | |
662 | |
663 | // #[async_trait::async_trait> FromOperationState for Option<T> { |
664 | // type Error = (); |
665 | |
666 | // async fn from_state(state: &StackOperationState) -> Result<Option<T>, OperationError<()>> { |
667 | // Ok(T::from_state(] |
668 | // impl<T: FromOperationStatestate).await.ok()) |
669 | // } |
670 | // } |
671 | |
672 | |
673 | |
674 | pub our_instance: Instance, |
675 | pub runtime: , |
676 | pub instance: , |
677 | pub user: , |
678 | |
679 | |
680 | |
681 | ; |
682 | |
683 | |
684 | |
685 | AuthenticatedInstance |
686 | |
687 | |
688 | |
689 | |
690 | type Target = Instance; |
691 | |
692 | |
693 | &self.0 |
694 | |
695 | |
696 | |
697 | |
698 | ; |
699 | |
700 | |
701 | |
702 | AuthenticatedUser |
703 | |
704 | |
705 | |
706 | |
707 | type Target = User; |
708 | |
709 | |
710 | &self.0 |
711 | |
712 | |
713 |