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
File diff suppressed because it is too large.
Load diff
@@ -2,6 +2,10 @@ import { withAdmin } from "@/lib/api-handler";
|
|||||||
import { PERMS } from "@/lib/permissions";
|
import { PERMS } from "@/lib/permissions";
|
||||||
import { repairMissingIcons } from "@/lib/services/repair-icons";
|
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 () => {
|
export const POST = withAdmin({ permission: PERMS.ASSETS_IMPORT }, async () => {
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
@@ -9,17 +13,26 @@ export const POST = withAdmin({ permission: PERMS.ASSETS_IMPORT }, async () => {
|
|||||||
async start(controller) {
|
async start(controller) {
|
||||||
const send = (data: unknown) => {
|
const send = (data: unknown) => {
|
||||||
try {
|
try {
|
||||||
controller.enqueue(
|
controller.enqueue(encoder.encode(sseEvent(data)));
|
||||||
encoder.encode(`data: ${JSON.stringify(data)}\n\n`),
|
|
||||||
);
|
|
||||||
} catch {
|
} catch {
|
||||||
/* stream closed */
|
/* 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 */
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user