Skip to content

Commit

Permalink
Throw RstGeneratorError instead inside plugin generator functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinovantes committed Jul 3, 2024
1 parent 1482ee2 commit c60781a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/Plugins/Domains/Js/JsPropGroup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Brand } from '@/@types/Brand.js'
import { RstGeneratorError } from '@/Generator/RstGeneratorError.js'
import { RstGeneratorState } from '@/Generator/RstGeneratorState.js'
import { RstParagraph } from '@/RstNode/Block/Paragraph.js'
import { RstFieldList } from '@/RstNode/List/FieldList.js'
Expand Down Expand Up @@ -63,7 +64,7 @@ export function getJsPropGroups(generatorState: RstGeneratorState, fieldList: Rs
}

default:
throw new Error(`Unhandled fieldName:"${fieldName}"`)
throw new RstGeneratorError(generatorState, fieldListItem, `Unhandled fieldName:"${fieldName}"`)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Plugins/Image/ImagePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createRstCompilerPlugins } from '@/RstCompilerPlugin.js'
import { getImageInfo } from './getImageInfo.js'
import { createImgTagMd } from './createImgTagMd.js'
import { createImgTagHtml } from './createImgTagHtml.js'
import { RstGeneratorError } from '@/Generator/RstGeneratorError.js'

// ----------------------------------------------------------------------------
// MARK: Directive
Expand Down Expand Up @@ -36,7 +37,7 @@ export const imageDirectiveGenerators = createDirectiveGenerators(
}

default: {
throw new Error(`Unhandled directive:"${node.directive}"`)
throw new RstGeneratorError(generatorState, node, `Unhandled directive:"${node.directive}"`)
}
}
},
Expand Down
17 changes: 13 additions & 4 deletions src/Plugins/Only/OnlyPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RstCompilerPlugin } from '@/RstCompilerPlugin.js'
import { evaluatePythonExpr } from './parsePythonBinaryExpr.js'
import { RstGeneratorError } from '@/Generator/RstGeneratorError.js'

const ONLY_DIRECTIVE = 'only'

Expand All @@ -17,12 +18,20 @@ export const onlyDirectivePlugin: RstCompilerPlugin = {
generate: (generatorState, node) => {
const exprStr = node.initContentText
if (!exprStr) {
throw new Error(`Missing exprStr [${node.toShortString()}]`)
throw new RstGeneratorError(generatorState, node, 'Missing exprStr')
}

const shouldShow = evaluatePythonExpr(exprStr, generatorState.opts.outputEnv)
if (!shouldShow) {
return
try {
const shouldShow = evaluatePythonExpr(exprStr, generatorState.opts.outputEnv)
if (!shouldShow) {
return
}
} catch (err) {
if (err instanceof Error) {
throw new RstGeneratorError(generatorState, node, err.message)
} else {
throw err
}
}

generatorState.visitNodes(node.children)
Expand Down
7 changes: 4 additions & 3 deletions src/Plugins/Tabs/TabsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { renderCodeBlockHtml } from '../Code/renderCodeBlockHtml.js'
import { renderCodeBlockMd } from '../Code/renderCodeBlockMd.js'
import { bundledLanguagesInfo } from 'shiki'
import { assertNode } from '@/utils/assertNode.js'
import { RstGeneratorError } from '@/Generator/RstGeneratorError.js'

// ----------------------------------------------------------------------------
// MARK: Constants
Expand Down Expand Up @@ -93,14 +94,14 @@ function getTabsGroupKey(generatorState: RstGeneratorState, node: RstDirective):
assertNode(generatorState, child, RstNodeType.Directive)

if (childrenDirective !== null && childrenDirective !== child.directive) {
throw new Error(`Child [${child.toShortString()}] does not match other childrens' directive:"${childrenDirective}"`)
throw new RstGeneratorError(generatorState, child, `Expected "${childrenDirective}" but got "${child.directive}"`)
}

childrenDirective = child.directive
}

if (!childrenDirective) {
throw new Error('Failed to get childrenDirective')
throw new RstGeneratorError(generatorState, node, 'Failed to get childrenDirective')
}

switch (childrenDirective) {
Expand All @@ -114,7 +115,7 @@ function getTabsGroupKey(generatorState: RstGeneratorState, node: RstDirective):
return TAB_PANEL_CODEGROUP_DIRECTIVE

default:
throw new Error(`Unexpected directive:"${childrenDirective}"`)
throw new RstGeneratorError(generatorState, node, `Unexpected directive:"${childrenDirective}"`)
}
}

Expand Down

0 comments on commit c60781a

Please sign in to comment.