Catch fetch errors

This commit is contained in:
Adria Navarro 2025-03-19 16:05:59 +01:00
parent 7f08f0d023
commit ccddac9010
1 changed files with 22 additions and 18 deletions

View File

@ -37,24 +37,28 @@ export async function validateConfig(config: {
clientId: string clientId: string
clientSecret: string clientSecret: string
}): Promise<{ valid: boolean; message?: string }> { }): Promise<{ valid: boolean; message?: string }> {
const resp = await fetch(config.url, { try {
method: "POST", const resp = await fetch(config.url, {
headers: { method: "POST",
"Content-Type": "application/x-www-form-urlencoded", headers: {
}, "Content-Type": "application/x-www-form-urlencoded",
body: new URLSearchParams({ },
grant_type: "client_credentials", body: new URLSearchParams({
client_id: config.clientId, grant_type: "client_credentials",
client_secret: config.clientSecret, client_id: config.clientId,
}), client_secret: config.clientSecret,
redirect: "follow", }),
}) redirect: "follow",
})
const jsonResponse = await resp.json() const jsonResponse = await resp.json()
if (!resp.ok) { if (!resp.ok) {
const message = jsonResponse.error_description ?? resp.statusText const message = jsonResponse.error_description ?? resp.statusText
return { valid: false, message } return { valid: false, message }
}
return { valid: true }
} catch (e: any) {
return { valid: false, message: e.message }
} }
return { valid: true }
} }