docs-util: throw when schema parsing fails (#12846)

This commit is contained in:
Shahed Nasser
2025-06-27 12:52:30 +03:00
committed by GitHub
parent fcfd35a157
commit a23bbc1cbb
3 changed files with 29 additions and 10 deletions

View File

@@ -9,6 +9,7 @@ import { parse } from "yaml"
import formatOas from "../../utils/format-oas.js"
import pluralize from "pluralize"
import { capitalize, wordsToPascal } from "utils"
import chalk from "chalk"
import { OasArea } from "../kinds/oas.js"
import {
isLevelExceeded,
@@ -307,7 +308,7 @@ class OasSchemaHelper {
return
}
return this.parseSchema(schemaFileContent)
return this.parseSchema(schemaFileContent, true)
}
/**
@@ -316,7 +317,10 @@ class OasSchemaHelper {
* @param content - The schema comment string
* @returns If the schema is valid and parsed successfully, the schema and its prefix are retrieved.
*/
parseSchema(content: string): ParsedSchema | undefined {
parseSchema(
content: string,
failOnParseError = false
): ParsedSchema | undefined {
const schemaFileContent = content
.replace(`/**\n`, "")
.replaceAll(DOCBLOCK_LINE_ASTRIX, "")
@@ -334,8 +338,11 @@ class OasSchemaHelper {
try {
schema = parse(splitContent.slice(1).join("\n"))
} catch (e) {
// couldn't parse the OAS, so consider it
// not existent
if (failOnParseError) {
throw e
}
console.error(chalk.red(e))
}
return schema