File size: 7,657 Bytes
4545bb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | // Plain-language errors: what happened and what to do, from the backend error model {detail, code, request_id, errors?}.
// The raw code, status and request id are kept for "Copy details for support", never shown as the message.
import { ApiError } from './client';
export interface ErrorAction { label: string; to: string }
export interface FieldProblem { field: string; message: string }
export interface ErrorInfo {
/** One or two plain sentences: what happened. */
message: string;
/** What to do next, when it is not a link. */
hint?: string;
/** A link that helps ("Check sign-in details" -> /pods). */
action?: ErrorAction;
/** VALIDATION: one line per field. */
fields?: FieldProblem[];
code: string;
status: number | null;
detail: string;
requestId: string | null;
}
const FIELD_WORDS: Record<string, string> = {
base_url: 'Address', name: 'Name', modules: 'Business areas', module: 'Business area', at: 'Time', every_minutes: 'Every',
day: 'Day of the month', month: 'Month', pod_id: 'Test environment', patch_id: 'Patch name', playwright_mode: 'Run type',
username: 'Username', password: 'Password', email: 'Email', comment: 'Comment', reason: 'Reason', environment: 'Environment type',
};
const fieldName = (loc: unknown[]): string => {
const parts = loc.filter((x) => x !== 'body' && x !== 'query' && x !== 'path' && typeof x === 'string') as string[];
const last = parts[parts.length - 1] ?? '';
return FIELD_WORDS[last] ?? (last ? last.replace(/_/g, ' ').replace(/^./, (c) => c.toUpperCase()) : 'Request');
};
/** "Value error, ..." and similar pydantic prefixes read better without the prefix. */
const cleanMsg = (m: unknown) => String(m ?? 'is not valid').replace(/^Value error,\s*/i, '').replace(/^String should/, 'Should');
function fieldProblems(body: unknown): FieldProblem[] {
const errs = (body as { errors?: { loc?: unknown[]; msg?: unknown }[] } | null)?.errors;
if (!Array.isArray(errs)) return [];
return errs.map((e) => ({ field: fieldName(Array.isArray(e.loc) ? e.loc : []), message: cleanMsg(e.msg) }));
}
/** Sentence case and a full stop for a backend detail that is shown as it is. */
const sentence = (s: string) => { const t = s.trim(); if (!t) return t; const u = t.charAt(0).toUpperCase() + t.slice(1); return /[.!?]$/.test(u) ? u : `${u}.`; };
export function explainError(e: unknown): ErrorInfo {
if (!(e instanceof ApiError)) {
const msg = e instanceof Error ? e.message : String(e ?? '');
// fetch() itself failed: no answer from the server at all
if (e instanceof TypeError || /failed to fetch|networkerror|load failed/i.test(msg)) {
return { message: 'The app cannot reach its server. Check your connection, then try again.', code: 'NETWORK', status: null, detail: msg, requestId: null };
}
return { message: sentence(msg) || 'Something went wrong.', code: 'CLIENT', status: null, detail: msg, requestId: null };
}
const base = { code: e.code, status: e.status, detail: e.message, requestId: e.requestId ?? null };
const detail = /^HTTP \d+$/.test(e.message) ? '' : sentence(e.message);
switch (e.code) {
case 'POD_CHECK_FAILED':
return { ...base, message: `The test environment did not accept its sign-in check.${detail ? ` ${detail}` : ''}`, action: { label: 'Check sign-in details', to: '/pods' } };
case 'NOTHING_APPROVED':
return { ...base, message: 'No scenario is approved, so there is nothing to test.', hint: 'Approve at least one scenario. To skip them all, confirm that you want to skip every scenario.' };
case 'KILL_SWITCH':
return { ...base, message: 'The emergency stop is on, so this is refused.', hint: 'An administrator can release it under Settings, Emergency stop.' };
case 'BROWSER_RUNS_OFF':
return { ...base, message: 'This server cannot open a real browser, so a full browser run is not possible here.', hint: 'Start a quick simulation instead: it checks the workflow without a browser.' };
case 'AREA_NOT_ON_FUSION':
return { ...base, message: detail || 'This business area has no tests for an Oracle Fusion pod yet.', hint: 'On an Oracle Fusion pod, choose Payables.' };
case 'DUPLICATE_RUN':
return { ...base, message: detail || 'A matching test run is already queued or running.', hint: 'Open that test run instead of starting another one.', action: { label: 'Open test runs', to: '/runs' } };
case 'EXECUTION_ERROR':
return { ...base, message: detail || 'The test run stopped because the test browser failed.', hint: 'Try again. If it keeps happening, start a quick simulation or ask an administrator.' };
case 'BROWSER_CRASHED':
return { ...base, message: detail || 'The test browser stopped unexpectedly, most likely because the server ran out of memory.', hint: 'Try again with fewer business areas, or start a quick simulation.' };
case 'EXECUTION_TIMEOUT':
return { ...base, message: detail || 'The tests took longer than allowed and were stopped.', hint: 'Try again. If the test environment is slow today, run fewer business areas at once.' };
case 'AI_UNAVAILABLE':
return { ...base, message: detail || 'The AI model did not answer.', hint: 'Try again in a few minutes. An administrator can check the model under Settings, AI models.' };
case 'NOT_AT_GATE':
return { ...base, message: 'This step was already answered, possibly in another tab or by a colleague.', hint: 'The page refreshes to show where the test run is now.' };
case 'ALREADY_SIGNED':
return { ...base, message: 'This report is already signed off.', hint: 'The sign-off is shown on the report.' };
case 'RATE_LIMITED':
return { ...base, message: 'Too many requests in a short time.', hint: 'Wait a minute, then try again.' };
case 'VALIDATION': {
const fields = fieldProblems(e.body);
if (fields.length) return { ...base, message: fields.length === 1 ? 'One field needs a change.' : `${fields.length} fields need a change.`, fields };
return { ...base, message: detail || 'Some of the details are not valid.' };
}
case 'INTERNAL':
return { ...base, message: 'Something went wrong on the server.', hint: 'Try again. If it keeps happening, copy the details for support.' };
case 'CSRF':
return { ...base, message: 'This page is out of date.', hint: 'Reload the page, then try again.' };
case 'FORBIDDEN':
return { ...base, message: 'Your role cannot do this.', hint: 'Ask an administrator if you need access.' };
case 'UNAUTHENTICATED':
return { ...base, message: detail && !/not signed in|expired/i.test(detail) ? detail : 'You are signed out. Sign in again.' };
default:
if (e.status >= 500) return { ...base, message: 'Something went wrong on the server.', hint: 'Try again. If it keeps happening, copy the details for support.' };
if (e.status === 429) return { ...base, message: 'Too many requests in a short time.', hint: 'Wait a minute, then try again.' };
return { ...base, message: detail || 'The request did not work.' };
}
}
/** The details a support person needs, as plain text for the clipboard. */
export function supportDetails(i: ErrorInfo, where = typeof window !== 'undefined' ? window.location.pathname : ''): string {
return [`Error: ${i.code}${i.status ? ` (HTTP ${i.status})` : ''}`, i.requestId ? `Request id: ${i.requestId}` : '', i.detail ? `Detail: ${i.detail}` : '',
where ? `Page: ${where}` : '', `Time: ${new Date().toISOString()}`].filter(Boolean).join('\n');
}
|