npx skills add ...
npx skills add cognitedata/builder-skills --skill correctness-and-error-handling
MUST be used whenever fixing correctness and error handling issues in a Flows app. This skill finds AND fixes bugs, missing error states, unhandled rejections, and edge-case failures — it does not just report them. Triggers: correctness, error handling, bug fix, edge case, crash, unhandled, null, undefined, empty state, loading state, error boundary, try catch, async error, useEffect cleanup, type guard, runtime error, robustness.
npx skills add cognitedata/builder-skills --skill correctness-and-error-handling
Find and fix correctness issues and missing error handling in $ARGUMENTS (or the whole app if no argument is given). Work through every step below. Each step searches for problems and then fixes them in place. Only report issues that cannot be auto-fixed.
Read these files before checking anything:
src/main.tsx / src/App.tsx — top-level error boundaries and auth flow**/hooks/*.ts, **/contexts/*.tsx — shared async state**/api/*.ts, **/services/*.ts — CDF SDK call sitesFor each async data source, note:
For each match in a critical path (data fetching, rendering, auth, navigation):
Do not leave TODOs in critical paths. Every one must be resolved or converted to a safe fallback.
Every Flows app must have at least one React Error Boundary wrapping the main content so that an unexpected render-time exception shows a user-friendly message instead of a blank screen.
If no error boundary exists, create the ErrorFallback component and add the ErrorBoundary wrapper to App.tsx. Install react-error-boundary if not present:
Then add to App.tsx:
Write the updated App.tsx with the ErrorBoundary in place. Do not just suggest it — make the edit.
Search for every async function and Promise chain that does not have error handling:
Fix each one:
async functions that lack try/catch: wrap the function body in try/catch. Log the error with context and re-throw so callers/query layers can handle it:.then() without .catch(): add .catch() to the chain:useQuery/useMutation) missing isError handling: add the error check and error UI to the component:Read each file, make the edit, and write it back.
For each component that fetches data, it must have three distinct UI states:
| State | Required UI |
|---|---|
| Loading | Spinner, skeleton, or loading indicator |
| Error | User-readable message (not a raw error object or blank space) |
| Empty | "No results" / "Nothing here yet" message (not a blank list) |
Search for components that render data without checking loading state:
For each hit, read the component and add the missing states directly:
.map():Insert these checks in the correct order (loading, then error, then empty) above the existing data render. Write each fixed file.
External data (CDF responses, URL params, localStorage, JSON.parse) must be validated before use. TypeScript types alone are not runtime guarantees.
Fix each one:
JSON.parse(x) as T — replace with Zod safeParse:searchParams.get("id") without null check — add nullish fallback:localStorage.getItem(key) used directly — add type guard and fallback:Do not cast external data with as MyType — that bypasses runtime safety. Read, fix, and write each file.
Read every component that accesses properties of data returned from CDF or passed via props.
Fix each unsafe pattern found:
.map() on possibly-undefined array — add nullish fallback:.at() with optional chaining:Read each file with a match, apply the fix, and write it back.
Every useEffect that sets up a subscription, timer, event listener, or async operation that can outlive the component must return a cleanup function.
For each useEffect, check whether cleanup is needed and add the cleanup function if missing:
| Pattern | Fix to add |
|---|---|
addEventListener | Add return () => removeEventListener(...) |
setInterval / setTimeout | Add return () => clearInterval(id) / clearTimeout(id) |
| CDF streaming / SSE | Add return () => stream.close() |
fetch / CDF SDK call | Add AbortController: const controller = new AbortController() at the top, pass controller.signal to fetch, add return () => controller.abort(), and guard state updates with if (!controller.signal.aborted) |
| Zustand / event emitter subscription | Add return () => unsubscribe() |
Reference pattern for async effects:
Read each effect, add the missing cleanup, and write the file.
For each feature, check and add guards for:
limit and there are more pages, ensure pagination is communicated to the user. Add a "Load more" or pagination indicator if missing.For Atlas tool execute functions, add argument validation at the top of every execute function:
Search for execute functions, read each one, add the validation, and write the file.
Produce a structured report covering:
| Severity | File | Line | Issue | Status |
|---|---|---|---|---|
| HIGH | src/hooks/useAssets.ts | 34 | Unhandled promise rejection | FIXED — wrapped in try/catch |
| MEDIUM | src/components/AssetList.tsx | 12 | No empty state | FIXED — added empty state check |
| MEDIUM | src/auth/flow.ts | 45 | Auth error handling needs product decision | UNFIXED — requires team input |
If no issues are found in a step, state "No issues found" for that step. Do not skip steps silently.
Summarize what was fixed by severity. Flag any remaining HIGH issues that could cause data loss, crashes in production, or misleading UI states, and list them first for immediate attention.
grep -rn --include="*.tsx" --include="*.ts" -E "ErrorBoundary|componentDidCatch|getDerivedStateFromError" src/pnpm add react-error-boundaryimport { ErrorBoundary } from "react-error-boundary";
function ErrorFallback({ error }: { error: Error }) {
return (
<div role="alert" className="p-8 text-center">
<p className="text-lg font-semibold">Something went wrong</p>
<pre className="mt-2 text-sm text-muted-foreground">{error.message}</pre>
</div>
);
}
// Wrap the main content:
<ErrorBoundary FallbackComponent={ErrorFallback}>
<MainContent />
</ErrorBoundary># Find async functions
grep -rn --include="*.ts" --include="*.tsx" -E "async\s+function|async\s+\(" src/
# Find .then() without .catch()
grep -rn --include="*.ts" --include="*.tsx" -E "\.then\(" src/ | grep -v "\.catch\("async function fetchAssets(sdk: CogniteClient) {
try {
const result = await sdk.assets.list({ limit: 100 });
return result.items;
} catch (error) {
console.error("Failed to fetch assets:", error);
throw error;
}
}somePromise.then(handleResult).catch((error) => {
console.error("Operation failed:", error);
});const { data, isLoading, isError, error } = useQuery({
queryKey: ["assets"],
queryFn: () => fetchAssets(sdk),
});
if (isError) return <ErrorMessage error={error} />;grep -rn --include="*.tsx" -E "\.(map|filter|find)\(" src/ | grep -v "isLoading\|isPending\|skeleton\|Skeleton"if (isLoading) {
return <div className="flex items-center justify-center p-8"><Spinner /></div>;
}if (isError) {
return (
<div role="alert" className="p-4 text-center text-destructive">
<p>Failed to load data. Please try again.</p>
</div>
);
}if (!data || data.length === 0) {
return (
<div className="p-8 text-center text-muted-foreground">
<p>No results found.</p>
</div>
);
}# Find JSON.parse without validation
grep -rn --include="*.ts" --include="*.tsx" -E "JSON\.parse\(" src/
# Find localStorage reads
grep -rn --include="*.ts" --include="*.tsx" -E "localStorage\.(get|set)Item" src/
# Find useSearchParams usage
grep -rn --include="*.ts" --include="*.tsx" -E "useSearchParams|searchParams\.get" src/import { z } from "zod";
const MySchema = z.object({ /* fields */ });
const parseResult = MySchema.safeParse(JSON.parse(raw));
if (!parseResult.success) {
console.warn("Invalid stored data, using defaults:", parseResult.error);
return defaultValue;
}
const validated = parseResult.data;const id = searchParams.get("id") ?? defaultId;const raw = localStorage.getItem(key);
if (raw === null) return defaultValue;
try {
const parsed = JSON.parse(raw);
// validate parsed shape
return isValidShape(parsed) ? parsed : defaultValue;
} catch {
return defaultValue;
}grep -rn --include="*.tsx" --include="*.ts" -E "\w+\[0\]\." src/// Before: asset.properties.space.Asset.name
// After:
const name = asset.properties?.["my-space"]?.["Asset"]?.name ?? "Unknown";// Before: items.map(renderItem)
// After:
(items ?? []).map(renderItem)// Before: items[0].name
// After:
const first = items.at(0)?.name ?? "—";grep -rn --include="*.tsx" --include="*.ts" -B 2 -A 15 "useEffect" src/useEffect(() => {
const controller = new AbortController();
async function load() {
try {
const data = await fetchWithSignal(controller.signal);
if (!controller.signal.aborted) setState(data);
} catch (err) {
if (err instanceof Error && err.name !== "AbortError") {
setError(err);
}
}
}
load();
return () => controller.abort();
}, [id]);execute: async (args) => {
if (!args.assetId || typeof args.assetId !== "string") {
return { output: "Missing or invalid assetId", details: null };
}
// ... safe to proceed
}