-
Notifications
You must be signed in to change notification settings - Fork 565
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
fix: extra error handling and debugging [IAC-3138] #5580
Merged
sandor-trombitas
merged 1 commit into
main
from
fix/IAC-3138/extra-debug-and-error-handling
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import pick = require('lodash.pick'); | ||
import { CustomError } from '../errors'; | ||
import { BasicResultData, SEVERITY, TestDepGraphMeta } from './legacy'; | ||
import * as debugLib from 'debug'; | ||
|
||
const debug = debugLib('snyk-iac'); | ||
|
||
export interface AnnotatedIacIssue { | ||
id: string; | ||
|
@@ -58,10 +61,24 @@ const IAC_ISSUES_KEY = 'infrastructureAsCodeIssues'; | |
export function mapIacTestResult( | ||
iacTest: IacTestResponse, | ||
): MappedIacTestResponse | IacTestError { | ||
if (iacTest instanceof CustomError) { | ||
if (iacTest instanceof Error) { | ||
return mapIacTestError(iacTest); | ||
} | ||
|
||
if (!iacTest.result) { | ||
// This is an unexpected scenario, we should always have a result object, | ||
// but if we don't, we should handle it gracefully. | ||
debug(`invalid scan result: ${iacTest}`); | ||
const errorMessage = iacTest.path | ||
? `Invalid result for ${iacTest.path}` | ||
: 'Invalid result'; | ||
return mapIacTestError( | ||
new CustomError( | ||
`${errorMessage}. Please run the command again with the \`-d\` flag and contact [email protected] with the contents of the output`, | ||
), | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition, where a result is not set, should never happen anymore if all errors are caught by the fix above. But if it still does, we log a user friendly message instead of letting it crash trying to access props for an inexistent result. |
||
|
||
const infrastructureAsCodeIssues = | ||
iacTest?.result?.cloudConfigResults.map(mapIacIssue) || []; | ||
const { | ||
|
@@ -76,10 +93,10 @@ export function mapIacTestResult( | |
}; | ||
} | ||
|
||
export function mapIacTestError(error: CustomError) { | ||
export function mapIacTestError(error: Error) { | ||
return { | ||
ok: false, | ||
code: error.code, | ||
code: error instanceof CustomError ? error.code : undefined, | ||
error: error.message, | ||
path: (error as any).path, | ||
}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main fix. The customer log attached to the Jira ticket indicates that an
Error
is returned in some cases from the policy engine (it should not) and, because the check was done only forCustomError
, anyError
was allowed to go on the happy path being treated as an actual result and crashing with undefined on prop checking.This is a fix for a regression introduced here: 4d08086#diff-634d0df2f6eab15512d93b5a3ab1ada33e7146aecd9771844e6f4d503ef3f066L200