fix: rewrite repair-icons with proper error handling and progress
- Wrap all DB/file operations in try-catch, send error events via SSE - Report skipped/progress events for items that already have icons - Add 'started' event with total count so the UI shows real-time progress - Catch route-level errors and stream them instead of returning JSON - Use log line content as React key instead of array index
This commit is contained in:
1 parent
13cb5f8670
commit
90107c83d5
3 files changed
+160
-65
No files matched your search
@@ -2,6 +2,10 @@ import { withAdmin } from "@/lib/api-handler";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { repairMissingIcons } from "@/lib/services/repair-icons";
|
||||
|
||||
function sseEvent(data: unknown): string {
|
||||
return `data: ${JSON.stringify(data)}\n\n`;
|
||||
}
|
||||
|
||||
export const POST = withAdmin({ permission: PERMS.ASSETS_IMPORT }, async () => {
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
@@ -9,17 +13,26 @@ export const POST = withAdmin({ permission: PERMS.ASSETS_IMPORT }, async () => {
|
||||
async start(controller) {
|
||||
const send = (data: unknown) => {
|
||||
try {
|
||||
controller.enqueue(
|
||||
encoder.encode(`data: ${JSON.stringify(data)}\n\n`),
|
||||
);
|
||||
controller.enqueue(encoder.encode(sseEvent(data)));
|
||||
} catch {
|
||||
/* stream closed */
|
||||
}
|
||||
};
|
||||
|
||||
await repairMissingIcons(send);
|
||||
try {
|
||||
await repairMissingIcons(send);
|
||||
} catch (err) {
|
||||
send({
|
||||
type: "error",
|
||||
message: `repairMissingIcons threw: ${(err as Error).message}`,
|
||||
});
|
||||
}
|
||||
|
||||
controller.close();
|
||||
try {
|
||||
controller.close();
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user