Skip to content

Commit c66e710

Browse files
author
Pelle Wessman
authored
Use @socketsecurity/config + improve errors (#13)
1 parent e26c54f commit c66e710

File tree

6 files changed

+41
-72
lines changed

6 files changed

+41
-72
lines changed

cli.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ try {
3737
} else if (err instanceof InputError) {
3838
errorTitle = 'Invalid input'
3939
errorMessage = err.message
40+
errorBody = err.body
4041
} else if (err instanceof Error) {
4142
errorTitle = 'Unexpected error'
4243
errorMessage = messageWithCauses(err)

lib/commands/report/create.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
import path from 'node:path'
44

5+
import { betterAjvErrors } from '@apideck/better-ajv-errors'
6+
import { readSocketConfig, SocketValidationError } from '@socketsecurity/config'
57
import meow from 'meow'
68
import ora from 'ora'
9+
import { ErrorWithCause } from 'pony-cause'
710

811
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
912
import { ChalkOrMarkdown, logSymbols } from '../../utils/chalk-markdown.js'
13+
import { InputError } from '../../utils/errors.js'
1014
import { printFlagList } from '../../utils/formatting.js'
1115
import { createDebugLogger } from '../../utils/misc.js'
1216
import { getPackageFiles } from '../../utils/path-resolve.js'
1317
import { setupSdk } from '../../utils/sdk.js'
14-
import { readSocketConfig } from '../../utils/socket-config.js'
1518
import { fetchReportData, formatReportDataOutput } from './view.js'
1619

1720
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
@@ -166,6 +169,25 @@ async function setupCommand (name, description, argv, importMeta) {
166169
const absoluteConfigPath = path.join(cwd, 'socket.yml')
167170

168171
const config = await readSocketConfig(absoluteConfigPath)
172+
.catch(/** @param {unknown} cause */ cause => {
173+
if (cause && typeof cause === 'object' && cause instanceof SocketValidationError) {
174+
// Inspired by workbox-build: https://github.com/GoogleChrome/workbox/blob/95f97a207fd51efb3f8a653f6e3e58224183a778/packages/workbox-build/src/lib/validate-options.ts#L68-L71
175+
const betterErrors = betterAjvErrors({
176+
basePath: 'config',
177+
data: cause.data,
178+
errors: cause.validationErrors,
179+
// @ts-ignore
180+
schema: cause.schema,
181+
})
182+
throw new InputError(
183+
'The socket.yml config is not valid',
184+
betterErrors.map((err) => `[${err.path}] ${err.message}.${err.suggestion ? err.suggestion : ''}`).join('\n')
185+
)
186+
} else {
187+
throw new ErrorWithCause('Failed to read socket.yml config', { cause })
188+
}
189+
})
190+
169191
const packagePaths = await getPackageFiles(cwd, cli.input, config, debugLog)
170192

171193
return {

lib/utils/errors.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
export class AuthError extends Error {}
2-
export class InputError extends Error {}
2+
3+
export class InputError extends Error {
4+
/**
5+
* @param {string} message
6+
* @param {string} [body]
7+
*/
8+
constructor (message, body) {
9+
super(message)
10+
11+
/** @type {string|undefined} */
12+
this.body = body
13+
}
14+
}

lib/utils/path-resolve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const GLOB_IGNORE = [
3333
*
3434
* @param {string} cwd The working directory to use when resolving paths
3535
* @param {string[]} inputPaths A list of paths to folders, package.json files and/or recognized lockfiles. Supports globs.
36-
* @param {import('./socket-config.js').SocketYml|undefined} config
36+
* @param {import('@socketsecurity/config').SocketYml|undefined} config
3737
* @param {typeof console.error} debugLog
3838
* @returns {Promise<string[]>}
3939
* @throws {InputError}

lib/utils/socket-config.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@
7474
"typescript": "~4.9.3"
7575
},
7676
"dependencies": {
77+
"@apideck/better-ajv-errors": "^0.3.6",
78+
"@socketsecurity/config": "^1.1.0",
7779
"@socketsecurity/sdk": "^0.4.0",
78-
"ajv": "^8.11.2",
7980
"chalk": "^5.1.2",
8081
"globby": "^13.1.3",
8182
"hpagent": "^1.2.0",
@@ -88,7 +89,6 @@
8889
"pony-cause": "^2.1.8",
8990
"prompts": "^2.4.2",
9091
"terminal-link": "^3.0.0",
91-
"update-notifier": "^6.0.2",
92-
"yaml": "^2.1.3"
92+
"update-notifier": "^6.0.2"
9393
}
9494
}

0 commit comments

Comments
 (0)