Skip to content

Commit 3cf44d6

Browse files
committed
Improve error exit and logging
1 parent 5d33660 commit 3cf44d6

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

.eslintrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
"semi": "off",
4646
"@typescript-eslint/semi": ["error", "never"],
4747
"@typescript-eslint/type-annotation-spacing": "error",
48-
"@typescript-eslint/unbound-method": "error"
48+
"@typescript-eslint/unbound-method": "error",
49+
"no-shadow": "off",
50+
"@typescript-eslint/no-shadow": "warn"
4951
},
5052
"env": {
5153
"node": true,

.github/workflows/test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ jobs:
2727
uses: actions/upload-artifact@v3
2828
with:
2929
name: sarif-output
30-
path: test-data/webgoat.sarif
30+
path: test-data/webgoat.sarif
31+
- uses: ./
32+
continue-on-error: true
33+
with:
34+
sarifFile: test-data/webgoat1.sarif

src/main.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-console */
21
import {resolve, dirname} from 'path'
32
import {env} from 'process'
43
import yargs from 'yargs'
@@ -8,6 +7,7 @@ import * as core from '@actions/core'
87
import {DOMParser} from '@xmldom/xmldom'
98
import * as xpath from 'xpath'
109
import {JSONPath} from 'jsonpath-plus'
10+
import {LogLevel, log} from './utils'
1111

1212
let sarifFilePath: string
1313
let outputFilePath: string
@@ -52,25 +52,26 @@ if (env.GITHUB_ACTIONS === 'true') {
5252
outputFilePath = resolve(argv.outputFile || sarifFilePath)
5353
}
5454

55-
console.log(`Using ${sarifFilePath} for SARIF file`)
56-
console.log(`Using ${cweFilePath} for CWE file`)
57-
console.log(`Using ${outputFilePath} for output file`)
55+
log(`Using ${sarifFilePath} for SARIF file`)
56+
log(`Using ${cweFilePath} for CWE file`)
57+
log(`Using ${outputFilePath} for output file`)
5858

5959
// Load SARIF file
6060
try {
6161
sarifResults = JSON.parse(readFileSync(sarifFilePath, 'utf8'))
6262
} catch (err) {
63-
core.setFailed(`Unable to load SARIF file: ${err}`)
64-
process.exit(1)
63+
log(`Unable to load SARIF file`, LogLevel.Error)
64+
core.setFailed(err as Error)
65+
throw err
6566
}
6667

6768
// Load security standard CWE XML file
6869
try {
6970
cweXml = new DOMParser().parseFromString(readFileSync(cweFilePath, 'utf8'))
7071
} catch (err) {
71-
console.log(`Unable to load CWE file: ${err}`)
72-
core.setFailed(`Unable to load CWE file: ${err}`)
73-
process.exit(1)
72+
log(`Unable to load CWE file`, LogLevel.Error)
73+
core.setFailed(err as Error)
74+
throw err
7475
}
7576
const select = xpath.useNamespaces(cweFileXmlNs)
7677
const cweIds = (select(cweIdXpath, cweXml) as Attr[]).map(attribute => attribute.value)
@@ -106,6 +107,7 @@ JSONPath({
106107
try {
107108
writeFileSync(outputFilePath, JSON.stringify(sarifResults))
108109
} catch (err) {
109-
core.setFailed(`Unable to write SARIF file: ${err}`)
110-
process.exit(1)
110+
log(`Unable to write SARIF file`, LogLevel.Error)
111+
core.setFailed(err as Error)
112+
throw err
111113
}

src/utils.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-disable no-console */
2+
import {env} from 'process'
3+
import * as core from '@actions/core'
4+
5+
export enum LogLevel {
6+
Info = 'Info',
7+
Warn = 'Warn',
8+
Error = 'Error'
9+
}
10+
11+
export function log(message: string, level = LogLevel.Info): void {
12+
if (env.GITHUB_ACTIONS === 'true') {
13+
switch (level) {
14+
case LogLevel.Info: {
15+
core.info(message)
16+
break
17+
}
18+
case LogLevel.Warn: {
19+
core.warning(message)
20+
break
21+
}
22+
case LogLevel.Error: {
23+
core.error(message)
24+
break
25+
}
26+
}
27+
} else {
28+
switch (level) {
29+
case LogLevel.Info: {
30+
console.info(message)
31+
break
32+
}
33+
case LogLevel.Warn: {
34+
console.warn(message)
35+
break
36+
}
37+
case LogLevel.Error: {
38+
console.error(message)
39+
break
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)