Skip to content

Commit 4e5253e

Browse files
committed
better error handling
1 parent e0cf4fa commit 4e5253e

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

packages/migrate/utils.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,11 @@ export function update_svelte_file(file_path, transform_script_code, transform_s
312312
}
313313
);
314314
fs.writeFileSync(file_path, transform_svelte_code(updated, file_path), 'utf-8');
315-
} catch (e) {
316-
console.error(`Error updating ${file_path}:`, e);
315+
} catch (err) {
316+
// TODO: change to import('svelte/compiler').Warning after upgrading to Svelte 5
317+
const e = (/** @type {any} */ (err));
318+
console.warn(buildExtendedLogMessage(e), e.frame);
319+
console.info(e.stack);
317320
}
318321
}
319322

@@ -327,11 +330,34 @@ export function update_js_file(file_path, transform_code) {
327330
const content = fs.readFileSync(file_path, 'utf-8');
328331
const updated = transform_code(content, file_path.endsWith('.ts'), file_path);
329332
fs.writeFileSync(file_path, updated, 'utf-8');
330-
} catch (e) {
331-
console.error(`Error updating ${file_path}:`, e);
333+
} catch (err) {
334+
// TODO: change to import('svelte/compiler').Warning after upgrading to Svelte 5
335+
const e = (/** @type {any} */ (err));
336+
console.warn(buildExtendedLogMessage(e), e.frame);
337+
console.info(e.stack);
332338
}
333339
}
334340

341+
/**
342+
* @param {any} w
343+
*/
344+
export function buildExtendedLogMessage(w) {
345+
const parts = [];
346+
if (w.filename) {
347+
parts.push(w.filename);
348+
}
349+
if (w.start) {
350+
parts.push(':', w.start.line, ':', w.start.column);
351+
}
352+
if (w.message) {
353+
if (parts.length > 0) {
354+
parts.push(' ');
355+
}
356+
parts.push(w.message);
357+
}
358+
return parts.join('');
359+
}
360+
335361
/**
336362
* Updates the tsconfig/jsconfig.json file with the provided function.
337363
* @param {(content: string) => string} update_tsconfig_content

0 commit comments

Comments
 (0)