@@ -19,22 +19,28 @@ export class ReportsFetcher {
19
19
if ( ! isPlatformBrowser ( this . platformId ) ) {
20
20
return [ ] ;
21
21
}
22
- return fetch ( '/api/reports' )
23
- . then ( ( r ) => r . json ( ) as Promise < RunGroup [ ] > )
24
- . then ( ( groups ) =>
25
- groups . sort ( ( a , b ) => {
26
- return (
27
- new Date ( b . timestamp ) . getTime ( ) - new Date ( a . timestamp ) . getTime ( )
28
- ) ;
29
- } )
30
- ) ;
22
+
23
+ const response = await fetch ( '/api/reports' ) ;
24
+
25
+ if ( ! response . ok ) {
26
+ throw new Error ( `Response status: ${ response . status } ` ) ;
27
+ }
28
+
29
+ const groups = ( await response . json ( ) ) as RunGroup [ ] ;
30
+
31
+ return groups . sort (
32
+ ( a , b ) =>
33
+ new Date ( b . timestamp ) . getTime ( ) - new Date ( a . timestamp ) . getTime ( )
34
+ ) ;
31
35
} ,
32
36
} ) ;
33
37
34
38
readonly reportGroups = computed ( ( ) => {
35
39
return this . groupsResource . hasValue ( ) ? this . groupsResource . value ( ) : [ ] ;
36
40
} ) ;
37
41
42
+ readonly reportGroupsError = computed ( ( ) => this . groupsResource . error ( ) ) ;
43
+
38
44
readonly isLoadingSingleReport = computed ( ( ) => this . pendingFetches ( ) > 0 ) ;
39
45
readonly isLoadingReportsList = computed ( ( ) =>
40
46
this . groupsResource . isLoading ( )
@@ -44,19 +50,31 @@ export class ReportsFetcher {
44
50
if ( ! this . runCache . has ( groupId ) ) {
45
51
this . pendingFetches . update ( ( current ) => current + 1 ) ;
46
52
47
- const allRuns = await fetch ( `/api/reports/${ groupId } ` ) . then (
48
- ( r ) => r . json ( ) as Promise < RunInfo [ ] >
49
- ) ;
50
- const firstRun = allRuns [ 0 ] ;
51
- const combined = {
52
- id : firstRun . id ,
53
- group : firstRun . group ,
54
- details : firstRun . details ,
55
- results : allRuns . flatMap ( ( run ) => run . results ) ,
56
- } satisfies RunInfo ;
57
-
58
- this . runCache . set ( groupId , combined ) ;
59
- this . pendingFetches . update ( ( current ) => current - 1 ) ;
53
+ try {
54
+ const response = await fetch ( `/api/reports/${ groupId } ` ) ;
55
+
56
+ if ( ! response . ok ) {
57
+ throw new Error ( `Response status: ${ response . status } ` ) ;
58
+ }
59
+
60
+ const allRuns = ( await response . json ( ) ) as RunInfo [ ] ;
61
+
62
+ if ( ! Array . isArray ( allRuns ) || allRuns . length === 0 ) {
63
+ throw new Error ( `Could not find report with id: ${ groupId } ` ) ;
64
+ }
65
+
66
+ const firstRun = allRuns [ 0 ] ;
67
+ const combined = {
68
+ id : firstRun . id ,
69
+ group : firstRun . group ,
70
+ details : firstRun . details ,
71
+ results : allRuns . flatMap ( ( run ) => run . results ) ,
72
+ } satisfies RunInfo ;
73
+
74
+ this . runCache . set ( groupId , combined ) ;
75
+ } finally {
76
+ this . pendingFetches . update ( ( current ) => current - 1 ) ;
77
+ }
60
78
}
61
79
62
80
return this . runCache . get ( groupId ) ! ;
0 commit comments