check if the url resolves before adding the monitor

This commit is contained in:
om-3004 committed 2024-10-13 00:04:59 +05:30
1 parent d3659d394a
commit 4a1b290416
1 file changed
+18 -6
@@ -124,13 +124,21 @@ const CreateMonitor = () => {
const handleCreateMonitor = async (event) => { const handleCreateMonitor = async (event) => {
event.preventDefault(); event.preventDefault();
const formattedUrl = monitor.type === "http"
? `http${https ? "s" : ""}://` + monitor.url
: monitor.url;
try {
// Check if the URL resolves by sending a request
const response = await fetch(formattedUrl, { method: 'GET', mode: 'no-cors' });
if (!response) {
throw new Error();
}
//obj to submit //obj to submit
let form = { let form = {
url: url:formattedUrl,
//preprending protocol for url
monitor.type === "http"
? `http${https ? "s" : ""}://` + monitor.url
: monitor.url,
name: monitor.name === "" ? monitor.url : monitor.name, name: monitor.name === "" ? monitor.url : monitor.name,
type: monitor.type, type: monitor.type,
interval: monitor.interval * MS_PER_MINUTE, interval: monitor.interval * MS_PER_MINUTE,
@@ -165,6 +173,10 @@ const CreateMonitor = () => {
createToast({ body: "Failed to create monitor." }); createToast({ body: "Failed to create monitor." });
} }
} }
} catch (error) {
createToast({ body: "The endpoint you entered doesn't resolve. Check the URL again." });
setErrors({ url: "The entered URL is not reachable." });
}
}; };
//select values //select values
@@ -375,7 +387,7 @@ const CreateMonitor = () => {
onClick={handleCreateMonitor} onClick={handleCreateMonitor}
disabled={Object.keys(errors).length !== 0 && true} disabled={Object.keys(errors).length !== 0 && true}
> >
Create monitor {monitor.type === "http" ? "Create Monitor" : "Check Endpoint"}
</Button> </Button>
</Stack> </Stack>
</Stack> </Stack>