Skip to content

Commit

Permalink
fix(twoslash): improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 12, 2024
1 parent b559cde commit 73fdbd1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
6 changes: 3 additions & 3 deletions packages/twoslash/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function createTransformerFactory(
const lineEl = this.lines[line]
index = codeEl.children.indexOf(lineEl)
if (index === -1) {
onShikiError(new ShikiTwoslashError(`Cannot find line ${line} in code element`))
onShikiError(new ShikiTwoslashError(`Cannot find line ${line} in code element`), this.source, this.options.lang)
return
}
}
Expand Down Expand Up @@ -183,7 +183,7 @@ export function createTransformerFactory(
const tokens = locateTextTokens(node.line, node.character, node.length)

if (!tokens.length && !(node.type === 'error' && renderer.nodesError)) {
onShikiError(new ShikiTwoslashError(`Cannot find tokens for node: ${JSON.stringify(node)}`))
onShikiError(new ShikiTwoslashError(`Cannot find tokens for node: ${JSON.stringify(node)}`), this.source, this.options.lang)
continue
}

Expand Down Expand Up @@ -279,7 +279,7 @@ export function createTransformerFactory(
break
}
default: {
onShikiError(new ShikiTwoslashError(`Unknown node type: ${(node as any)?.type}`))
onShikiError(new ShikiTwoslashError(`Unknown node type: ${(node as any)?.type}`), this.source, this.options.lang)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/twoslash/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface TransformerTwoslashOptions {
* Custom error handler for Shiki errors
* When specified, `throws` will be ignored
*/
onShikiError?: (error: unknown) => void
onShikiError?: (error: unknown, code: string, lang: string) => void
}

export interface TwoslashRenderer {
Expand Down
26 changes: 16 additions & 10 deletions packages/vitepress-twoslash/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ export function transformerTwoslash(options: VitePressPluginTwoslashOptions = {}
explicitTrigger = true,
} = options

const onError = (error: any, code: string) => {
const isCI = typeof process !== 'undefined' && process?.env?.CI
const isDev = typeof process !== 'undefined' && process?.env?.NODE_ENV === 'development'
const shouldThrow = (options.throws || isCI || !isDev) && options.throws !== false
console.error(`\n\n--------\nTwoslash error in code:\n--------\n${code.split(/\n/g).slice(0, 15).join('\n').trim()}\n--------\n`)
if (shouldThrow)
throw error
else
console.error(error)
if (typeof process !== 'undefined')
process.exitCode = 1
return removeTwoslashNotations(code)
}

const twoslash = createTransformerFactory(
createTwoslasher(),
)({
langs: ['ts', 'tsx', 'js', 'jsx', 'json', 'vue'],
renderer: rendererFloatingVue(options),
onTwoslashError: (error, code) => {
const isCI = typeof process !== 'undefined' && process?.env?.CI
const isDev = typeof process !== 'undefined' && process?.env?.NODE_ENV === 'development'
console.error(String(error), `\n\n--------\nTwoslash error in code:\n--------\n${code.split(/\n/g).slice(0, 15).join('\n').trim()}\n--------\n`)
if (isCI || !isDev || options.throws)
throw error
if (typeof process !== 'undefined')
process.exitCode = 1
return removeTwoslashNotations(code)
},
onTwoslashError: onError,
onShikiError: onError,
...options,
explicitTrigger,
})
Expand Down

0 comments on commit 73fdbd1

Please sign in to comment.