🐛 fix(hosters): API JSON parse safety + URL-encode API key
- apiGet(): wrap res.json() in try-catch with descriptive error message when server returns HTML instead of JSON - URL-encode apiKey in upload server lookup URL template (prevents broken URLs if key contains +, &, = chars) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
ede5a192ea
commit
e22784cef8
@ -281,7 +281,13 @@ async function apiGet(url, signal) {
|
|||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
redirect: 'follow'
|
redirect: 'follow'
|
||||||
});
|
});
|
||||||
const data = await res.json();
|
let data;
|
||||||
|
try {
|
||||||
|
data = await res.json();
|
||||||
|
} catch {
|
||||||
|
const text = await res.text().catch(() => '');
|
||||||
|
throw new Error(`API-Antwort war kein JSON (HTTP ${res.status}): ${(text || '').slice(0, 200)}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (data.status && [401, 403, 429, 500].includes(data.status)) {
|
if (data.status && [401, 403, 429, 500].includes(data.status)) {
|
||||||
throw new Error(data.msg || data.message || JSON.stringify(data));
|
throw new Error(data.msg || data.message || JSON.stringify(data));
|
||||||
@ -300,7 +306,7 @@ async function getUploadServer(hosterName, hosterConfig, apiKey, signal) {
|
|||||||
|
|
||||||
for (let attempt = 1; attempt <= SERVER_RETRY_ATTEMPTS; attempt++) {
|
for (let attempt = 1; attempt <= SERVER_RETRY_ATTEMPTS; attempt++) {
|
||||||
for (const endpoint of hosterConfig.serverEndpoints) {
|
for (const endpoint of hosterConfig.serverEndpoints) {
|
||||||
const url = `${hosterConfig.apiBase}${endpoint}?key=${apiKey}`;
|
const url = `${hosterConfig.apiBase}${endpoint}?key=${encodeURIComponent(apiKey)}`;
|
||||||
try {
|
try {
|
||||||
const data = await apiGet(url, signal);
|
const data = await apiGet(url, signal);
|
||||||
const uploadUrl = extractUploadServerUrl(data, hosterConfig.apiBase);
|
const uploadUrl = extractUploadServerUrl(data, hosterConfig.apiBase);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user