Spaces:
Paused
Paused
Hemang Thakur
commited on
Commit
·
a4986ed
1
Parent(s):
7cfcf77
added error handling for graph display
Browse files
frontend/src/Components/AiComponents/Graph.js
CHANGED
@@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react';
|
|
2 |
import { FaTimes } from 'react-icons/fa';
|
3 |
import './Graph.css';
|
4 |
|
5 |
-
export default function Graph({ open, onClose }) {
|
6 |
const [graphHtml, setGraphHtml] = useState("");
|
7 |
const [loading, setLoading] = useState(true);
|
8 |
const [error, setError] = useState("");
|
@@ -18,16 +18,25 @@ export default function Graph({ open, onClose }) {
|
|
18 |
})
|
19 |
.then(res => res.json())
|
20 |
.then(data => {
|
|
|
|
|
|
|
|
|
21 |
setGraphHtml(data.result);
|
22 |
setLoading(false);
|
23 |
})
|
24 |
.catch(err => {
|
25 |
console.error("Error fetching graph:", err);
|
26 |
-
|
|
|
27 |
setLoading(false);
|
|
|
|
|
|
|
|
|
28 |
});
|
29 |
}
|
30 |
-
}, [open]);
|
31 |
|
32 |
if (!open) return null;
|
33 |
|
|
|
2 |
import { FaTimes } from 'react-icons/fa';
|
3 |
import './Graph.css';
|
4 |
|
5 |
+
export default function Graph({ open, onClose, onError }) {
|
6 |
const [graphHtml, setGraphHtml] = useState("");
|
7 |
const [loading, setLoading] = useState(true);
|
8 |
const [error, setError] = useState("");
|
|
|
18 |
})
|
19 |
.then(res => res.json())
|
20 |
.then(data => {
|
21 |
+
// If the API returns an error, throw it to be caught below.
|
22 |
+
if (data.error) {
|
23 |
+
throw new Error(data.error);
|
24 |
+
}
|
25 |
setGraphHtml(data.result);
|
26 |
setLoading(false);
|
27 |
})
|
28 |
.catch(err => {
|
29 |
console.error("Error fetching graph:", err);
|
30 |
+
const errMsg = err.message || "Error fetching graph.";
|
31 |
+
setError(errMsg);
|
32 |
setLoading(false);
|
33 |
+
// Propagate error to parent using the onError callback.
|
34 |
+
if (onError && typeof onError === 'function') {
|
35 |
+
onError(errMsg);
|
36 |
+
}
|
37 |
});
|
38 |
}
|
39 |
+
}, [open, onError]);
|
40 |
|
41 |
if (!open) return null;
|
42 |
|