-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: start new auth flow on 401 responses #339
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ axiosDebug({ | |
}); | ||
import { Api } from '@neondatabase/api-client'; | ||
|
||
import { ensureAuth } from './commands/auth.js'; | ||
import { ensureAuth, deleteCredentials } from './commands/auth.js'; | ||
import { defaultDir, ensureConfigDir } from './config.js'; | ||
import { log } from './log.js'; | ||
import { defaultClientID } from './auth.js'; | ||
|
@@ -160,45 +160,67 @@ builder = builder | |
'For more information, visit https://neon.tech/docs/reference/neon-cli', | ||
) | ||
.wrap(null) | ||
.fail(async (msg, err) => { | ||
if (process.argv.some((arg) => arg === '--help' || arg === '-h')) { | ||
await showHelp(builder); | ||
process.exit(0); | ||
} | ||
.fail(false); | ||
|
||
async function handleError(msg: string, err: unknown): Promise<boolean> { | ||
if (process.argv.some((arg) => arg === '--help' || arg === '-h')) { | ||
await showHelp(builder); | ||
process.exit(0); | ||
} | ||
|
||
// Log stack trace if available | ||
if (err instanceof Error && err.stack) { | ||
log.debug('Stack: %s', err.stack); | ||
} | ||
|
||
if (isAxiosError(err)) { | ||
if (err.code === 'ECONNABORTED') { | ||
log.error('Request timed out'); | ||
sendError(err, 'REQUEST_TIMEOUT'); | ||
} else if (err.response?.status === 401) { | ||
sendError(err, 'AUTH_FAILED'); | ||
log.error('Authentication failed, please run `neonctl auth`'); | ||
} else { | ||
if (err.response?.data?.message) { | ||
log.error(err.response?.data?.message); | ||
} | ||
if (isAxiosError(err)) { | ||
if (err.code === 'ECONNABORTED') { | ||
log.error('Request timed out'); | ||
sendError(err, 'REQUEST_TIMEOUT'); | ||
return false; | ||
} else if (err.response?.status === 401) { | ||
sendError(err, 'AUTH_FAILED'); | ||
log.info('Authentication failed, deleting credentials...'); | ||
try { | ||
deleteCredentials(defaultDir); | ||
return true; // Allow retry for auth failures | ||
} catch (deleteErr) { | ||
log.debug( | ||
'status: %d %s | path: %s', | ||
err.response?.status, | ||
err.response?.statusText, | ||
err.request?.path, | ||
'Failed to delete credentials: %s', | ||
deleteErr instanceof Error ? deleteErr.message : 'unknown error', | ||
); | ||
sendError(err, 'API_ERROR'); | ||
return false; | ||
} | ||
} else { | ||
sendError(err || new Error(msg), matchErrorCode(msg || err?.message)); | ||
log.error(msg || err?.message); | ||
} | ||
await closeAnalytics(); | ||
if (err?.stack) { | ||
log.debug('Stack: %s', err.stack); | ||
if (err.response?.data?.message) { | ||
log.error(err.response?.data?.message); | ||
} | ||
log.debug( | ||
'status: %d %s | path: %s', | ||
err.response?.status, | ||
err.response?.statusText, | ||
err.request?.path, | ||
); | ||
sendError(err, 'API_ERROR'); | ||
return false; | ||
} | ||
process.exit(1); | ||
}); | ||
} else { | ||
const error = | ||
err instanceof Error ? err : new Error(msg || 'Unknown error'); | ||
sendError(error, matchErrorCode(error.message)); | ||
log.error(error.message); | ||
return false; | ||
} | ||
} | ||
|
||
void (async () => { | ||
// Main loop with max 2 attempts (initial + 1 retry): | ||
let attempts = 0; | ||
const MAX_ATTEMPTS = 2; | ||
|
||
while (attempts < MAX_ATTEMPTS) { | ||
try { | ||
const args = await builder.argv; | ||
// Send analytics for a successful attempt | ||
trackEvent('cli_command_success', { | ||
...getAnalyticsEventProperties(args), | ||
projectId: args.projectId, | ||
|
@@ -208,8 +230,16 @@ void (async () => { | |
await showHelp(builder); | ||
process.exit(0); | ||
} | ||
|
||
await closeAnalytics(); | ||
} catch { | ||
// noop | ||
process.exit(0); | ||
} catch (err) { | ||
attempts++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps we should show number of attempts to the user somewhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's an implementation detail and not for retrying networking requests, that's why I think having a counter is unnecessary at this point. We may handle it differently in the future 🤷 Let me explain why it's there. Maybe it's not very clear from the PR. If a user had invalid credentials:
This design makes it impossible to re-enter the command execution if it fails. First, I removed the error handler from From the doc:
Finally, third, I had to introduce a loop to call Phew, I think I didn't forget anything. |
||
const shouldRetry = await handleError('', err); | ||
if (!shouldRetry || attempts >= MAX_ATTEMPTS) { | ||
await closeAnalytics(); | ||
process.exit(1); | ||
} | ||
// If shouldRetry is true and we haven't hit max attempts, loop continues | ||
} | ||
})(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to use
rmSync
just like everywhere else.