Enrich external API failure audit metadata

This commit is contained in:
kdletters
2026-05-28 15:42:46 +08:00
parent 2cd2b9704b
commit f1fb92aa29
40 changed files with 315 additions and 152 deletions

View File

@@ -1,4 +1,9 @@
import { fetchJson, parseApiErrorMessage, saveJsonObject } from './jsonClient';
import {
appendApiErrorRequestId,
fetchJson,
parseApiErrorMessage,
saveJsonObject,
} from './jsonClient';
export const EDITOR_API_BASE_PATH = '/api/editor';
export const ASSETS_API_BASE_PATH = '/api/assets';
@@ -69,7 +74,7 @@ export async function postApiJson<T>(
const responseText = await response.text();
if (!response.ok) {
throw new Error(parseApiErrorMessage(responseText, fallbackMessage));
throw new Error(appendApiErrorRequestId(parseApiErrorMessage(responseText, fallbackMessage), response.headers.get('x-request-id')));
}
return responseText ? (JSON.parse(responseText) as T) : ({} as T);

View File

@@ -1,6 +1,7 @@
import {
API_RESPONSE_ENVELOPE_HEADER,
API_RESPONSE_ENVELOPE_VERSION,
appendApiErrorRequestId,
parseApiErrorMessage,
unwrapApiResponse,
} from '../../../packages/shared/src/http';
@@ -17,7 +18,15 @@ export async function fetchJson<T>(
const responseText = await response.text();
if (!response.ok) {
throw new Error(parseApiErrorMessage(responseText, `${fallbackMessage}: ${response.status}`));
throw new Error(
appendApiErrorRequestId(
parseApiErrorMessage(
responseText,
`${fallbackMessage}: ${response.status}`,
),
response.headers.get('x-request-id'),
),
);
}
return responseText
@@ -41,8 +50,13 @@ export async function saveJsonObject(
const responseText = await response.text();
if (!response.ok) {
throw new Error(parseApiErrorMessage(responseText, fallbackMessage));
throw new Error(
appendApiErrorRequestId(
parseApiErrorMessage(responseText, fallbackMessage),
response.headers.get('x-request-id'),
),
);
}
}
export { parseApiErrorMessage };
export { appendApiErrorRequestId, parseApiErrorMessage };