Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print out details of AggregateError originating from Spectral (@microsoft.azure/openapi-validator v2.1.4 -> v2.1.5) #595

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft.azure/openapi-validator",
"comment": "Add printing out of details of spectral run AggregateErrors. Addresses https://github.com/Azure/azure-sdk-tools/issues/6856",
"type": "patch"
}
],
"packageName": "@microsoft.azure/openapi-validator"
}
2 changes: 1 addition & 1 deletion packages/azure-openapi-validator/autorest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft.azure/openapi-validator",
"version": "2.1.4",
"version": "2.1.5",
"description": "Azure OpenAPI Validator",
"main": "./dist/index.js",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ export async function spectralPluginFunc(initiator: IAutoRestPluginInitiator): P
const normalizedFile = file.startsWith("file:///") ? fileURLToPath(file) : file
await runSpectral(openapiDefinitionObject, normalizedFile, initiator.Message.bind(initiator), spectral)
} catch (e) {
initiator.Message({
Channel: "fatal",
Text: `spectralPluginFunc: Failed validating: '${file}', error encountered: ` + e,
})
catchSpectralRunErrors(file, e, initiator)
}
}

Expand Down Expand Up @@ -233,3 +230,29 @@ export async function getRuleSet(openapiType: OpenApiTypes): Promise<[ruleset: R
return namesOfRulesInStagingOnly
}
}

/** Spectral (from "@stoplight/spectral-core") may throw https://www.npmjs.com/package/es-aggregate-error
* If so, we print out all the constituent errors.
* For additional context, see: https://github.com/Azure/azure-sdk-tools/issues/6856
*/
function catchSpectralRunErrors(file: string, e: any, initiator: any) {
// Initialize an array to collect error messages
const errorMessages: string[] = [e]
konrad-jamrozik marked this conversation as resolved.
Show resolved Hide resolved

// Check if "e" contains the "errors" property
if (e && e.errors && Array.isArray(e.errors)) {
e.errors.forEach((error: any, index: number) => {
// Push each error message into the array
errorMessages.push(`Error ${index + 1}: ${error.message}`)
})
}

// Combine all error messages with newlines
const combinedErrorMessages = errorMessages.join("\n")

// Call initiator.Message with the combined error message
initiator.Message({
Channel: "fatal",
Text: `spectralPluginFunc: Failed validating: '${file}'. Errors encountered:\n${combinedErrorMessages}`,
})
}