qbo fix
This commit is contained in:
@@ -101,6 +101,51 @@ function saveTokens() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extrahiert ein QBO Fault-Objekt unabhängig vom Case (Fault/fault)
|
||||
* und von der Error-Array-Struktur.
|
||||
* @param {object} data - Geparste QBO JSON-Response
|
||||
* @returns {object|null} { code, message, detail } oder null wenn kein Fault
|
||||
*/
|
||||
function extractQboFault(data) {
|
||||
if (!data || typeof data !== 'object') return null;
|
||||
|
||||
// Prüfe beide Case-Varianten: data.Fault und data.fault
|
||||
for (const key of ['Fault', 'fault']) {
|
||||
const fault = data[key];
|
||||
if (!fault) continue;
|
||||
|
||||
// Error-Array (QBO Standard)
|
||||
const errors = fault.Error || fault.error;
|
||||
if (Array.isArray(errors) && errors.length > 0) {
|
||||
const first = errors[0];
|
||||
return {
|
||||
code: first.code || first.Code || 'UNKNOWN',
|
||||
message: first.Message || first.message || 'Unknown QBO error',
|
||||
detail: first.Detail || first.detail || ''
|
||||
};
|
||||
}
|
||||
|
||||
// Einzelnes Error-Objekt
|
||||
if (errors && (errors.code || errors.Code || errors.Message || errors.message)) {
|
||||
return {
|
||||
code: errors.code || errors.Code || 'UNKNOWN',
|
||||
message: errors.Message || errors.message || 'Unknown QBO error',
|
||||
detail: errors.Detail || errors.detail || ''
|
||||
};
|
||||
}
|
||||
|
||||
// Unbekannte Fault-Struktur
|
||||
return {
|
||||
code: 'UNKNOWN',
|
||||
message: JSON.stringify(fault).substring(0, 500),
|
||||
detail: ''
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function makeQboApiCall(requestOptions) {
|
||||
const client = getOAuthClient();
|
||||
const ts = () => new Date().toISOString().replace('T',' ').substring(0,19);
|
||||
@@ -145,14 +190,14 @@ async function makeQboApiCall(requestOptions) {
|
||||
const response = await client.makeApiCall(requestOptions);
|
||||
const data = response.getJson ? response.getJson() : response.json;
|
||||
|
||||
if (data.fault && data.fault.error) {
|
||||
const errorCode = data.fault.error[0].code;
|
||||
if (errorCode === '3200' || errorCode === '3202' || errorCode === '3100') {
|
||||
console.log(`[${ts()}] ⚠️ QBO Token-Fehler (${errorCode}) – Refresh & Retry...`);
|
||||
const qboFault = extractQboFault(data);
|
||||
if (qboFault) {
|
||||
if (qboFault.code === '3200' || qboFault.code === '3202' || qboFault.code === '3100') {
|
||||
console.log(`[${ts()}] ⚠️ QBO Token-Fehler (${qboFault.code}) – Refresh & Retry...`);
|
||||
await doRefresh();
|
||||
return await client.makeApiCall(requestOptions);
|
||||
}
|
||||
throw new Error(`QBO API Error ${errorCode}: ${data.fault.error[0].message}`);
|
||||
throw new Error(`QBO API Error ${qboFault.code}: ${qboFault.message}${qboFault.detail ? ' - ' + qboFault.detail : ''}`);
|
||||
}
|
||||
|
||||
// ── Kein saveTokens() hier – Token hat sich nicht geändert ──
|
||||
@@ -177,5 +222,6 @@ module.exports = {
|
||||
getOAuthClient,
|
||||
makeQboApiCall,
|
||||
saveTokens,
|
||||
resetOAuthClient
|
||||
resetOAuthClient,
|
||||
extractQboFault
|
||||
};
|
||||
Reference in New Issue
Block a user