tercul-frontend/server/lib/error.ts
Damir Mukimov 4a23f496fa
Major frontend development updates
- Enhanced annotation system with improved inline editing
- Updated author components with new card and header designs
- Improved reading view with enhanced line numbering and controls
- Added new blog management features and tag management
- Updated UI components with improved accessibility and styling
- Enhanced search functionality with better filtering
- Added new dashboard features and activity feeds
- Improved translation selector and work comparison tools
- Updated GraphQL integration and API hooks
- Enhanced responsive design and mobile experience
2025-11-27 03:44:09 +01:00

52 lines
1.5 KiB
TypeScript

import type { ClientError } from "graphql-request";
export interface ApiErrorShape {
message: string;
details?: unknown;
}
export function normalizeError(err: unknown): ApiErrorShape {
if (!err) return { message: "Unknown error" };
if (typeof err === "string") return { message: err };
if (err instanceof Error) {
return { message: err.message };
}
return { message: "Unexpected error", details: err };
}
// Minimal subset of Express' Response to avoid importing full types repeatedly
interface MinimalResponse {
status(code: number): this;
json(body: unknown): this;
}
export function respondWithError(
res: MinimalResponse,
err: unknown,
fallback = "Request failed"
) {
const normalized = normalizeError(err);
res.status(500).json({ message: fallback, error: normalized.message });
}
export function isClientError(err: unknown): err is ClientError {
return (
!!err && typeof err === "object" && "response" in err && "request" in err
);
}
// Enhanced normalize after adding ClientError helper (placed after respond function to reuse normalizeError definition order remains valid)
export function enhancedNormalize(err: unknown): ApiErrorShape {
if (isClientError(err)) {
const ce = err as ClientError;
interface GQLErrorLike {
message: string;
}
const messages = ce.response.errors
?.map((e: GQLErrorLike) => e.message)
.join("; ");
return { message: messages || ce.message, details: ce.response.errors };
}
return normalizeError(err);
}