Skip to content

Commit

Permalink
fix: improve error log
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Dec 14, 2023
1 parent c5e59d6 commit df455eb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
8 changes: 2 additions & 6 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,7 @@ async function interactive({
s.start('...')

const timing = new Timing()
const pool = new FixedThreadPool<UnminifyWorkerParams, Measurement>(concurrency, unminifyWorkerFile, {
errorHandler: e => console.error(e),
})
const pool = new FixedThreadPool<UnminifyWorkerParams, Measurement>(concurrency, unminifyWorkerFile)
const unminify = async (inputPath: string) => {
const outputPath = path.join(outputDir, path.relative(commonBaseDir, inputPath))
const result = await pool.execute({ inputPath, outputPath, moduleMeta, moduleMapping })
Expand Down Expand Up @@ -547,9 +545,7 @@ async function nonInteractive(features: Feature[], {

const timing = new Timing()

const pool = new FixedThreadPool<UnminifyWorkerParams, Measurement>(concurrency, unminifyWorkerFile, {
errorHandler: e => console.error(e),
})
const pool = new FixedThreadPool<UnminifyWorkerParams, Measurement>(concurrency, unminifyWorkerFile)
const unminify = async (inputPath: string) => {
const outputPath = path.join(outputDir, path.relative(commonBaseDir, inputPath))
const result = await pool.execute({ inputPath, outputPath, moduleMeta, moduleMapping })
Expand Down
13 changes: 9 additions & 4 deletions packages/cli/src/unminify.worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import path from 'node:path'
import process from 'node:process'
import { runTransformations, transformationRules } from '@wakaru/unminify'
Expand All @@ -11,9 +12,8 @@ import type { Transform } from 'jscodeshift'
export async function unminify(data?: UnminifyWorkerParams) {
if (!data) throw new Error('No data received')

const { inputPath, outputPath, moduleMeta, moduleMapping } = data
try {
const { inputPath, outputPath, moduleMeta, moduleMapping } = data

const cwd = process.cwd()
const filename = path.relative(cwd, inputPath)
const source = await fsa.readFile(inputPath, 'utf-8')
Expand All @@ -22,7 +22,10 @@ export async function unminify(data?: UnminifyWorkerParams) {
const timing = new Timing()
const transformations = transformationRules.map<Transform>((rule) => {
const { id, transform } = rule
return (...args: Parameters<Transform>) => timing.collect(filename, id, () => transform(...args))
const fn = (...args: Parameters<Transform>) => timing.collect(filename, id, () => transform(...args))
// Set the name of the function for better debugging
Object.defineProperty(fn, 'name', { value: id })
return fn
})

const { code } = runTransformations(fileInfo, transformations, { moduleMeta, moduleMapping })
Expand All @@ -33,8 +36,10 @@ export async function unminify(data?: UnminifyWorkerParams) {
}
catch (e) {
// We print the error here because it will lose the stack trace after being sent to the main thread
console.log()
console.error(e)
throw e

return []
}
}

Expand Down
7 changes: 2 additions & 5 deletions packages/unminify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export function runTransformations<P extends Record<string, any>>(
if (newResult) code = newResult
}
catch (err: any) {
if ('loc' in err) {
console.error(err)
console.error(`\nError running transformation ${transform.name} on ${path}`, err)

if ('loc' in err) {
const padLeft = (str: string, len: number, char: string) => {
const count = len > str.length ? len - str.length : 0
return `${char.repeat(count)}${str}`
Expand All @@ -68,9 +68,6 @@ export function runTransformations<P extends Record<string, any>>(
printLine(loc.line + 1)
printLine(loc.line + 2)
}
else {
console.error(err)
}

break
}
Expand Down
2 changes: 2 additions & 0 deletions packages/unminify/src/transformations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,7 @@ export const transformationRules: TransformationRule[] = _transformationRules.ma
const occurrence = occurrenceMap.get(rule.name) ?? 0
occurrenceMap.set(rule.name, occurrence + 1)
const id = occurrence === 0 ? rule.name : `${rule.name}-${occurrence}`
// Set the name of the function for better debugging
Object.defineProperty(rule.transform, 'name', { value: id })
return { ...rule, id }
})

0 comments on commit df455eb

Please sign in to comment.