Fix: defensive frontend + panic recovery logging middleware

Frontend:
- JobDetail loadLogs: fallback to [] when API returns null
- JobDetail loadJob: pairs ?? [] guard on /api/sync-pairs 500
- JobHistory: Array.isArray guard on job list response
- api client: return undefined for null body instead of throwing

Backend:
- handlers_jobs GetLog: return [] instead of null when no log rows
- router: custom recoverer middleware that logs panics to slog
  with full stack trace, method, and path
This commit is contained in:
2026-07-08 01:28:12 -04:00
parent 6d16797c4f
commit e0e94bd518
5 changed files with 39 additions and 7 deletions
+8 -1
View File
@@ -5,6 +5,10 @@ interface ApiOptions {
body?: unknown;
}
function isArray(v: unknown): v is unknown[] {
return Array.isArray(v);
}
export async function api<T>(path: string, opts: ApiOptions = {}): Promise<T> {
const { method = 'GET', body } = opts;
const res = await fetch(`${BASE}${path}`, {
@@ -18,7 +22,10 @@ export async function api<T>(path: string, opts: ApiOptions = {}): Promise<T> {
throw new Error((err as { error?: string }).error || 'Request failed');
}
if (res.status === 204) return undefined as T;
return res.json();
const data = await res.json().catch(() => null);
if (data === null) return undefined as T;
if (isArray(data) && !data.length && data[0] === undefined) return [] as unknown as T;
return data as T;
}
export async function apiRaw(path: string): Promise<Response> {
+3 -3
View File
@@ -80,7 +80,7 @@ export default function JobDetail() {
try {
const j = await api<Job>(`/api/jobs/${id}`);
setJob(j);
const pairs = await api<SyncPair[]>('/api/sync-pairs');
const pairs = (await api<SyncPair[]>('/api/sync-pairs')) ?? [];
const p = pairs.find((sp: SyncPair) => sp.id === j.sync_pair_id);
setPair(p || null);
} catch {
@@ -91,9 +91,9 @@ export default function JobDetail() {
async function loadLogs(offset: number) {
try {
const ls = await api<LogLine[]>(
const ls = (await api<LogLine[]>(
`/api/jobs/${id}/log?offset=${offset}&limit=1000`
);
)) ?? [];
if (offset === 0) {
setLogs(ls);
} else {
+1 -1
View File
@@ -60,7 +60,7 @@ export default function JobHistory() {
const totalCount = res.headers.get('X-Total-Count');
if (totalCount) setTotal(Number(totalCount));
const data = await res.json();
setJobs(data);
setJobs(Array.isArray(data) ? data : []);
} catch {
} finally {
setLoading(false);