diff --git a/packages/core/package.json b/packages/core/package.json index b24297007..c8a2649f9 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -40,6 +40,7 @@ "lodash.topath": "^4.5.2", "minimatch": "3.0.4", "nimma": "0.1.8", + "pony-cause": "^1.0.0", "simple-eval": "1.0.0", "tslib": "^2.3.0" }, diff --git a/packages/core/src/runner/lintNode.ts b/packages/core/src/runner/lintNode.ts index f8cd38066..6059f5973 100644 --- a/packages/core/src/runner/lintNode.ts +++ b/packages/core/src/runner/lintNode.ts @@ -1,6 +1,7 @@ import { JsonPath } from '@stoplight/types'; import { decodeSegmentFragment, getClosestJsonPath, printPath, PrintStyle } from '@stoplight/spectral-runtime'; -import { get } from 'lodash'; +import { get, isError } from 'lodash'; +import { ErrorWithCause } from 'pony-cause'; import { Document } from '../document'; import { IFunctionResult, IGivenNode, RulesetFunctionContext } from '../types'; @@ -24,10 +25,20 @@ export const lintNode = (context: IRunnerInternalContext, node: IGivenNode, rule for (const target of targets) { const path = target.path.length > 0 ? [...givenPath, ...target.path] : givenPath; - const targetResults = then.function(target.value, then.functionOptions ?? null, { - ...fnContext, - path, - }); + let targetResults; + try { + targetResults = then.function(target.value, then.functionOptions ?? null, { + ...fnContext, + path, + }); + } catch (e) { + throw new ErrorWithCause( + `Function "${then.function.name}" threw an exception${isError(e) ? `: ${e.message}` : ''}`, + { + cause: e, + }, + ); + } if (targetResults === void 0) continue; diff --git a/yarn.lock b/yarn.lock index 7759bdfc7..626ed16d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2378,6 +2378,7 @@ __metadata: minimatch: 3.0.4 nimma: 0.1.8 nock: ^13.1.0 + pony-cause: ^1.0.0 simple-eval: 1.0.0 treeify: ^1.1.0 tslib: ^2.3.0