| | | 1 | | import pc from 'picocolors'; |
| | | 2 | | import { |
| | | 3 | | ExpiredCredentialsError, |
| | | 4 | | SecretsFetchError, |
| | | 5 | | SsoSessionExpiredError, |
| | | 6 | | } from '../../../core/domain/errors/DomainErrors.js'; |
| | | 7 | | import { describeError } from '../../../core/infrastructure/describeError.js'; |
| | | 8 | | |
| | | 9 | | export function presentError(error: unknown): string { |
| | 15 | 10 | | if (error instanceof SsoSessionExpiredError) { |
| | 8 | 11 | | return renderSsoSessionExpired(error); |
| | | 12 | | } |
| | 7 | 13 | | if (error instanceof ExpiredCredentialsError) { |
| | 1 | 14 | | return renderExpiredCredentials(); |
| | | 15 | | } |
| | 6 | 16 | | if (error instanceof SecretsFetchError) { |
| | 4 | 17 | | return renderSecretsFetchError(error); |
| | | 18 | | } |
| | 2 | 19 | | return renderFallback(error); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | function gameOver(title: string): string { |
| | 15 | 23 | | return `\n${pc.bold(pc.red(`\u{1F4A5} GAME OVER \u2014 ${title}`))}`; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | function renderSsoSessionExpired(error: SsoSessionExpiredError): string { |
| | 8 | 27 | | const body = error.profileName |
| | | 28 | | ? ` ${pc.dim('Your AWS SSO session for profile ')}${pc.magenta(`"${error.profileName}"`)}${pc.dim(' ran out of live |
| | | 29 | | : ` ${pc.dim('Your AWS SSO session ran out of lives.')}`; |
| | 8 | 30 | | const command = error.profileName |
| | | 31 | | ? `aws sso login --profile ${error.profileName}` |
| | | 32 | | : 'aws sso login'; |
| | | 33 | | |
| | 8 | 34 | | return [ |
| | | 35 | | gameOver('SSO session expired'), |
| | | 36 | | '', |
| | | 37 | | body, |
| | | 38 | | '', |
| | | 39 | | ` ${pc.bold(pc.yellow('\u2B95 CONTINUE?'))}`, |
| | | 40 | | ` ${pc.dim(' Run: ')}${pc.cyan(command)}`, |
| | | 41 | | ` ${pc.dim(' then re-run your envilder command.')}`, |
| | | 42 | | '', |
| | | 43 | | ].join('\n'); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | function renderExpiredCredentials(): string { |
| | 1 | 47 | | return [ |
| | | 48 | | gameOver('AWS credentials expired'), |
| | | 49 | | '', |
| | | 50 | | ` ${pc.dim('Your security token ran out of time.')}`, |
| | | 51 | | '', |
| | | 52 | | ` ${pc.bold(pc.yellow('\u2B95 CONTINUE?'))}`, |
| | | 53 | | ` ${pc.dim(' Refresh your credentials and retry ')}${pc.dim('(for SSO: ')}${pc.cyan('aws sso login')}${pc.dim('). |
| | | 54 | | '', |
| | | 55 | | ].join('\n'); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | function renderFallback(error: unknown): string { |
| | 2 | 59 | | const message = describeError(error); |
| | 2 | 60 | | return [ |
| | | 61 | | gameOver('you fell down the wrong pipe!'), |
| | | 62 | | '', |
| | | 63 | | ` ${pc.dim(message)}`, |
| | | 64 | | '', |
| | | 65 | | ].join('\n'); |
| | | 66 | | } |
| | | 67 | | |
| | 3 | 68 | | const ACCESS_DENIED_PATTERN = /access.?denied|not authorized|forbidden/i; |
| | | 69 | | |
| | | 70 | | function isAccessDeniedReason(reason: string): boolean { |
| | 6 | 71 | | return ACCESS_DENIED_PATTERN.test(reason); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | function renderSecretsFetchError(error: SecretsFetchError): string { |
| | 4 | 75 | | const rule = pc.red('\u2501'.repeat(60)); |
| | 4 | 76 | | const failureLines = error.failures.map( |
| | | 77 | | (failure) => |
| | 7 | 78 | | ` ${pc.red('\u2717 ')}${pc.bold(failure.envVar.padEnd(20))}${pc.dim('\u2192 ')}${pc.dim(failure.path)}`, |
| | | 79 | | ); |
| | 7 | 80 | | const reasons = [...new Set(error.failures.map((failure) => failure.reason))]; |
| | 4 | 81 | | const reasonLines = reasons.map((reason) => |
| | 6 | 82 | | isAccessDeniedReason(reason) |
| | | 83 | | ? ` ${pc.bold(pc.red(reason))}` |
| | | 84 | | : ` ${pc.dim(reason)}`, |
| | | 85 | | ); |
| | 4 | 86 | | return [ |
| | | 87 | | gameOver('some secrets could not be fetched'), |
| | | 88 | | rule, |
| | | 89 | | ...failureLines, |
| | | 90 | | '', |
| | | 91 | | ` ${pc.bold(pc.yellow('\u2B95 WHY?'))}`, |
| | | 92 | | ...reasonLines, |
| | | 93 | | '', |
| | | 94 | | ].join('\n'); |
| | | 95 | | } |