feat(platform-web): include trace id in api errors
This commit is contained in:
@@ -34,6 +34,7 @@ type ApiErrorEnvelope = {
|
||||
message?: string;
|
||||
detail?: string;
|
||||
};
|
||||
traceId?: string;
|
||||
};
|
||||
|
||||
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
@@ -50,11 +51,12 @@ async function responseErrorMessage(response: Response) {
|
||||
const envelope = (await response.json()) as ApiErrorEnvelope;
|
||||
const message = envelope.error?.message?.trim();
|
||||
const detail = envelope.error?.detail?.trim();
|
||||
const traceID = envelope.traceId?.trim();
|
||||
if (message && detail) {
|
||||
return `${message}: ${detail}`;
|
||||
return withTraceID(`${message}: ${detail}`, traceID);
|
||||
}
|
||||
if (message) {
|
||||
return message;
|
||||
return withTraceID(message, traceID);
|
||||
}
|
||||
} catch {
|
||||
// Fall back to status when the server does not return the platform envelope.
|
||||
@@ -62,6 +64,10 @@ async function responseErrorMessage(response: Response) {
|
||||
return `request failed ${response.status}`;
|
||||
}
|
||||
|
||||
function withTraceID(message: string, traceID?: string) {
|
||||
return traceID ? `${message} (traceId: ${traceID})` : message;
|
||||
}
|
||||
|
||||
export const api = {
|
||||
dashboardSummary: () => request<DashboardSummary>('/api/dashboard/summary'),
|
||||
vehicles: (params = new URLSearchParams()) => request<Page<VehicleRow>>(`/api/vehicles?${params.toString()}`),
|
||||
|
||||
Reference in New Issue
Block a user