Skip to content

Commit 4f84ff4

Browse files
committed
Add colorization to error messages and permission helper
- Import yoctocolors-cjs for terminal colors - Color error types in red (Invalid request, Access denied, etc.) - Color 'Try' labels in cyan for better visibility - Highlight commands in bold (socket whoami, --org flag) - Distinguish Free/Paid plans in yellow for rate limit errors - Color permission names in cyan in 403 helper - Improves visual hierarchy and readability of error output
1 parent bab7054 commit 4f84ff4

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/utils/api.mts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
import { messageWithCauses } from 'pony-cause'
23+
import colors from 'yoctocolors-cjs'
2324

2425
import { logger } from '@socketsecurity/registry/lib/logger'
2526
import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
@@ -87,12 +88,12 @@ function logPermissionsFor403(cmdPath?: string | undefined): void {
8788
}
8889

8990
logger.error('')
90-
logger.error('🔐 Required API Permissions:')
91+
logger.error(`🔐 ${colors.yellow('Required API Permissions')}:`)
9192
for (const permission of requirements.permissions) {
92-
logger.error(` • ${permission}`)
93+
logger.error(` • ${colors.cyan(permission)}`)
9394
}
9495
logger.error('')
95-
logger.error('💡 To fix this:')
96+
logger.error(`💡 ${colors.cyan('To fix this')}:`)
9697
logger.error(
9798
` 1. Visit ${webLink('https://socket.dev/settings/api-tokens')}`,
9899
)
@@ -121,52 +122,52 @@ export function getDefaultApiBaseUrl(): string | undefined {
121122
export async function getErrorMessageForHttpStatusCode(code: number) {
122123
if (code === HTTP_STATUS_BAD_REQUEST) {
123124
return (
124-
'❌ Invalid request: One of the options or parameters may be incorrect.\n' +
125-
'💡 Try: Check your command syntax and parameter values.'
125+
`❌ ${colors.red('Invalid request')}: One of the options or parameters may be incorrect.\n` +
126+
`💡 ${colors.cyan('Try')}: Check your command syntax and parameter values.`
126127
)
127128
}
128129
if (code === HTTP_STATUS_FORBIDDEN || code === HTTP_STATUS_UNAUTHORIZED) {
129130
return (
130-
'❌ Access denied: Your API token lacks required permissions or organization access.\n' +
131-
'💡 Try:\n' +
132-
' • Run `socket whoami` to verify your account and organization\n' +
131+
`❌ ${colors.red('Access denied')}: Your API token lacks required permissions or organization access.\n` +
132+
`💡 ${colors.cyan('Try')}:\n` +
133+
` • Run ${colors.bold('socket whoami')} to verify your account and organization\n` +
133134
` • Check your API token permissions at ${webLink('https://socket.dev/settings/api-tokens')}\n` +
134-
" • Ensure you're accessing the correct organization with `--org` flag\n" +
135+
` • Ensure you're accessing the correct organization with ${colors.bold('--org')} flag\n` +
135136
` • Verify your plan includes this feature at ${webLink('https://socket.dev/pricing')}`
136137
)
137138
}
138139
if (code === HTTP_STATUS_NOT_FOUND) {
139140
return (
140-
"❌ Not found: The requested endpoint or resource doesn't exist.\n" +
141-
'💡 Try:\n' +
141+
`❌ ${colors.red('Not found')}: The requested endpoint or resource doesn't exist.\n` +
142+
`💡 ${colors.cyan('Try')}:\n` +
142143
' • Verify resource names (package, repository, organization)\n' +
143144
' • Check if the resource was deleted or moved\n' +
144-
' • Update to the latest CLI version: `socket self-update` (SEA) or `npm update -g socket`\n' +
145+
` • Update to the latest CLI version: ${colors.bold('socket self-update')} (SEA) or ${colors.bold('npm update -g socket')}\n` +
145146
` • Report persistent issues at ${githubRepoLink('SocketDev', 'socket-cli', 'issues')}`
146147
)
147148
}
148149
if (code === HTTP_STATUS_TOO_MANY_REQUESTS) {
149150
return (
150-
'❌ Rate limit exceeded: Too many API requests.\n' +
151-
'💡 Try:\n' +
152-
` • Free plan: Wait a few minutes for quota reset or upgrade at ${webLink('https://socket.dev/pricing')}\n` +
153-
' • Paid plan: Contact support if rate limits seem incorrect\n' +
154-
' • Check current quota: `socket organization quota`\n' +
151+
`❌ ${colors.red('Rate limit exceeded')}: Too many API requests.\n` +
152+
`💡 ${colors.cyan('Try')}:\n` +
153+
` • ${colors.yellow('Free plan')}: Wait a few minutes for quota reset or upgrade at ${webLink('https://socket.dev/pricing')}\n` +
154+
`${colors.yellow('Paid plan')}: Contact support if rate limits seem incorrect\n` +
155+
` • Check current quota: ${colors.bold('socket organization quota')}\n` +
155156
' • Reduce request frequency or batch operations'
156157
)
157158
}
158159
if (code === HTTP_STATUS_INTERNAL_SERVER_ERROR) {
159160
return (
160-
'❌ Server error: Socket API encountered an internal problem (HTTP 500).\n' +
161-
'💡 Try:\n' +
161+
`❌ ${colors.red('Server error')}: Socket API encountered an internal problem (HTTP 500).\n` +
162+
`💡 ${colors.cyan('Try')}:\n` +
162163
' • Wait a few minutes and retry your command\n' +
163164
` • Check Socket status: ${webLink('https://status.socket.dev')}\n` +
164165
` • Report persistent issues: ${githubRepoLink('SocketDev', 'socket-cli', 'issues')}`
165166
)
166167
}
167168
return (
168-
`❌ HTTP ${code}: Server responded with unexpected status code.\n` +
169-
`💡 Try: Check Socket status at ${webLink('https://status.socket.dev')} or report the issue.`
169+
`❌ ${colors.red(`HTTP ${code}`)}: Server responded with unexpected status code.\n` +
170+
`💡 ${colors.cyan('Try')}: Check Socket status at ${webLink('https://status.socket.dev')} or report the issue.`
170171
)
171172
}
172173

0 commit comments

Comments
 (0)