-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: extra error handling and debugging [IAC-3138]
- Loading branch information
1 parent
8956366
commit 7fbae0f
Showing
2 changed files
with
40 additions
and
3 deletions.
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`, | ||
), | ||
); | ||
} | ||
|
||
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, | ||
}; | ||
|