Skip to content

Commit 5abd9ac

Browse files
committed
refactor: try catch for the whole function
1 parent 73d67e5 commit 5abd9ac

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/domain/getLabelConfigs.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@ import JSON5 from 'json5'
55
const jsonTypes = ['json', 'jsonc', 'json5']
66

77
const getFilePath = (configurationPath: string): string | undefined => {
8-
const repoPath = `./${configurationPath}`
9-
.replace('//', '/')
10-
.replace('././', './')
11-
if (configurationPath.includes('.json') && fs.existsSync(repoPath))
12-
return repoPath
13-
if (!configurationPath.includes('.json')) {
14-
let allFiles
15-
try {
16-
allFiles = fs.readdirSync(repoPath)
17-
} catch (error: any) {
18-
core.warning(
19-
`Could not read configuration file at ${repoPath}: ${error.message}. Skipping.`
8+
try {
9+
const repoPath = `./${configurationPath}`
10+
.replace('//', '/')
11+
.replace('././', './')
12+
if (configurationPath.includes('.json') && fs.existsSync(repoPath))
13+
return repoPath
14+
if (!configurationPath.includes('.json')) {
15+
const allFiles = fs.readdirSync(repoPath)
16+
17+
const expectedFilenames = jsonTypes.map((type) => `auto-label.${type}`)
18+
const files = allFiles.filter((filename) =>
19+
expectedFilenames.includes(filename)
2020
)
21-
return
21+
if (!files.length) {
22+
throw new Error('No default files located.')
23+
}
24+
return `${repoPath}/${files[0]}`.replace('//', '/')
2225
}
23-
const expectedFilenames = jsonTypes.map((type) => `auto-label.${type}`)
24-
const files = allFiles.filter((filename) =>
25-
expectedFilenames.includes(filename)
26+
} catch (error: any) {
27+
core.warning(
28+
`Could not read configuration file, configurationPath: "${configurationPath}", error: "${error.message}". Skipping.`
2629
)
27-
if (!files.length) return
28-
return `${repoPath}/${files[0]}`.replace('//', '/')
30+
return
2931
}
3032
}
3133

0 commit comments

Comments
 (0)