fix(magic): createAdGuardClient idempotent — 400 → clients/update
Verwaiste AdGuard-Clients (magic_<deviceId> existiert, aber DB-Row fehlt nach Crash zwischen clients/add und DB-Upsert) führten beim Re-Register zu 400 → 502. Jetzt: bei 400 auf clients/update zurückfallen und den bestehenden Client auf die frisch generierte clientId umbiegen. Behebt Magic-Register-502. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
579eb5b5e0
commit
547f86187b
@ -59,18 +59,45 @@ export async function createAdGuardClient(
|
||||
};
|
||||
|
||||
const authHeader = `Basic ${Buffer.from(`${user}:${password}`).toString("base64")}`;
|
||||
const headers = {
|
||||
Authorization: authHeader,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await $fetch(`${baseUrl}/control/clients/add`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: authHeader,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
headers,
|
||||
body: payload,
|
||||
});
|
||||
return response as void;
|
||||
} catch (err: any) {
|
||||
// AdGuard antwortet mit 400, wenn schon ein Client mit diesem Namen ODER
|
||||
// dieser Client-ID existiert. Das passiert bei Re-Registrierung wenn ein
|
||||
// früherer Versuch den Client anlegte, aber die DB-Row nie geschrieben wurde
|
||||
// (z.B. Prozess-Crash zwischen clients/add und DB-Upsert). Statt hart zu
|
||||
// failen → idempotent auf clients/update zurückfallen und den bestehenden
|
||||
// Client auf den frisch generierten clientId umbiegen.
|
||||
const status = err?.status ?? err?.response?.status ?? err?.statusCode;
|
||||
if (status === 400) {
|
||||
try {
|
||||
const response = await $fetch(`${baseUrl}/control/clients/update`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: { name, data: payload },
|
||||
});
|
||||
return response as void;
|
||||
} catch (updErr: any) {
|
||||
console.error(
|
||||
"[AdGuard] Client update (after add-conflict) failed:",
|
||||
updErr,
|
||||
);
|
||||
throw createError({
|
||||
statusCode: 502,
|
||||
message: `AdGuard API error (update): ${updErr.message || "unknown"}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
console.error("[AdGuard] Client creation failed:", err);
|
||||
throw createError({
|
||||
statusCode: 502,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user