@@ -45,31 +45,38 @@ export async function fetchMetrics({
45
45
// we need some way to get the auth token from the userStore, so we pass a reference to the method in this way.
46
46
// ideally fetching metrics should be handled in its own mobx store rather than in the content models. See issue #560
47
47
const token = await rootStore ?. userStore . getToken ( ) ;
48
- const response = await fetch (
49
- `${ process . env . REACT_APP_API_URL } /api/${ tenantId } /public` ,
50
- {
51
- body : JSON . stringify ( {
52
- metrics : metricNames ,
53
- } ) ,
54
- headers : {
55
- Authorization : `Bearer ${ token } ` ,
56
- "Content-Type" : "application/json" ,
57
- } ,
58
- method : "POST" ,
48
+
49
+ try {
50
+ const response = await fetch (
51
+ `${ process . env . REACT_APP_API_URL } /api/${ tenantId } /public` ,
52
+ {
53
+ body : JSON . stringify ( {
54
+ metrics : metricNames ,
55
+ } ) ,
56
+ headers : {
57
+ Authorization : `Bearer ${ token } ` ,
58
+ "Content-Type" : "application/json" ,
59
+ } ,
60
+ method : "POST" ,
61
+ }
62
+ ) ;
63
+
64
+ if ( response . ok ) {
65
+ const responseData : MetricsApiResponse = await response . json ( ) ;
66
+ return responseData ;
59
67
}
60
- ) ;
61
68
62
- if ( response . ok ) {
63
- const responseData : MetricsApiResponse = await response . json ( ) ;
64
- return responseData ;
69
+ const errorResponse : ErrorAPIResponse = await response . json ( ) ;
70
+ throw new Error (
71
+ `Metrics API responded with status ${ response . status } . Error message: ${
72
+ errorResponse . error || "none"
73
+ } `
74
+ ) ;
75
+ } catch ( error ) {
76
+ throw new Error (
77
+ `There was a network error attempting to fetch metrics: \n${ error } `
78
+ ) ;
65
79
}
66
-
67
- const errorResponse : ErrorAPIResponse = await response . json ( ) ;
68
- throw new Error (
69
- `Metrics API responded with status ${ response . status } . Error message: ${
70
- errorResponse . error || "none"
71
- } `
72
- ) ;
73
80
}
74
81
75
82
/**
0 commit comments