|
1 | 1 | import { Command } from 'commander' |
2 | 2 | import chalk from 'chalk' |
3 | 3 | import { Session } from '@0xsequence/auth' |
4 | | -import { listProjects, createProject, getProject, getDefaultAccessKey } from '../lib/api.js' |
| 4 | +import { |
| 5 | + listProjects, |
| 6 | + createProject, |
| 7 | + getProject, |
| 8 | + getDefaultAccessKey, |
| 9 | + isApiError, |
| 10 | +} from '../lib/api.js' |
| 11 | +import { extractErrorMessage } from '../lib/errors.js' |
5 | 12 | import { isLoggedIn, EXIT_CODES, loadConfig } from '../lib/config.js' |
6 | 13 | import { isValidPrivateKey } from '../lib/wallet.js' |
7 | 14 |
|
| 15 | +/** |
| 16 | + * Handle API errors with rate-limit / permission awareness. |
| 17 | + * Returns true if the error was handled; false otherwise. |
| 18 | + */ |
| 19 | +function handleApiErrorOutput(error: unknown, json: boolean): boolean { |
| 20 | + if ( |
| 21 | + isApiError(error) && |
| 22 | + (error.isRateLimited || error.isPermissionDenied || error.isUnauthorized) |
| 23 | + ) { |
| 24 | + if (json) { |
| 25 | + console.log( |
| 26 | + JSON.stringify({ |
| 27 | + error: error.isRateLimited |
| 28 | + ? 'Rate limited' |
| 29 | + : error.isPermissionDenied |
| 30 | + ? 'Permission denied' |
| 31 | + : 'Unauthorized', |
| 32 | + statusCode: error.statusCode, |
| 33 | + retryAfterSeconds: error.retryAfterSeconds, |
| 34 | + detail: error.errorBody, |
| 35 | + code: EXIT_CODES.API_ERROR, |
| 36 | + }) |
| 37 | + ) |
| 38 | + } else { |
| 39 | + if (error.isRateLimited) { |
| 40 | + console.error(chalk.red('✖ Rate limited by the API')) |
| 41 | + if (error.retryAfterSeconds !== null) { |
| 42 | + console.error(chalk.yellow(` Retry after: ${error.retryAfterSeconds}s`)) |
| 43 | + } |
| 44 | + console.error( |
| 45 | + chalk.gray(' You have made too many requests. Please wait before trying again.') |
| 46 | + ) |
| 47 | + } else if (error.isPermissionDenied) { |
| 48 | + console.error(chalk.red('✖ Permission denied (403)')) |
| 49 | + console.error(chalk.gray(' This can happen when:')) |
| 50 | + console.error(chalk.gray(' - Your session token has expired (re-run login)')) |
| 51 | + console.error(chalk.gray(' - Too many requests in a short period')) |
| 52 | + console.error(chalk.gray(' - The access key is invalid or revoked')) |
| 53 | + if (error.retryAfterSeconds !== null) { |
| 54 | + console.error(chalk.yellow(` Retry after: ${error.retryAfterSeconds}s`)) |
| 55 | + } |
| 56 | + } else { |
| 57 | + console.error(chalk.red('✖ Unauthorized (401)')) |
| 58 | + console.error( |
| 59 | + chalk.gray(' Your JWT token may have expired. Re-run: sequence-builder login') |
| 60 | + ) |
| 61 | + } |
| 62 | + if (error.errorBody) { |
| 63 | + console.error(chalk.gray(` Server response: ${error.errorBody}`)) |
| 64 | + } |
| 65 | + } |
| 66 | + return true |
| 67 | + } |
| 68 | + return false |
| 69 | +} |
| 70 | + |
8 | 71 | export const projectsCommand = new Command('projects') |
9 | 72 | .description('Manage Sequence Builder projects') |
10 | 73 | .option('--json', 'Output in JSON format') |
@@ -103,7 +166,10 @@ async function listProjectsAction(options: { json?: boolean; env?: string; apiUr |
103 | 166 | console.log(chalk.gray('Run `sequence-builder apikeys <project-id>` to view API keys')) |
104 | 167 | console.log('') |
105 | 168 | } catch (error) { |
106 | | - const errorMessage = error instanceof Error ? error.message : String(error) |
| 169 | + if (handleApiErrorOutput(error, !!json)) { |
| 170 | + process.exit(EXIT_CODES.API_ERROR) |
| 171 | + } |
| 172 | + const errorMessage = extractErrorMessage(error) |
107 | 173 | if (json) { |
108 | 174 | console.log(JSON.stringify({ error: errorMessage, code: EXIT_CODES.API_ERROR })) |
109 | 175 | } else { |
@@ -233,7 +299,10 @@ async function createProjectAction( |
233 | 299 | } |
234 | 300 | console.log('') |
235 | 301 | } catch (error) { |
236 | | - const errorMessage = error instanceof Error ? error.message : String(error) |
| 302 | + if (handleApiErrorOutput(error, !!json)) { |
| 303 | + process.exit(EXIT_CODES.API_ERROR) |
| 304 | + } |
| 305 | + const errorMessage = extractErrorMessage(error) |
237 | 306 | if (json) { |
238 | 307 | console.log(JSON.stringify({ error: errorMessage, code: EXIT_CODES.API_ERROR })) |
239 | 308 | } else { |
@@ -296,7 +365,10 @@ async function getProjectAction( |
296 | 365 | } |
297 | 366 | console.log('') |
298 | 367 | } catch (error) { |
299 | | - const errorMessage = error instanceof Error ? error.message : String(error) |
| 368 | + if (handleApiErrorOutput(error, !!json)) { |
| 369 | + process.exit(EXIT_CODES.API_ERROR) |
| 370 | + } |
| 371 | + const errorMessage = extractErrorMessage(error) |
300 | 372 | if (json) { |
301 | 373 | console.log(JSON.stringify({ error: errorMessage, code: EXIT_CODES.API_ERROR })) |
302 | 374 | } else { |
|
0 commit comments