-
Notifications
You must be signed in to change notification settings - Fork 465
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix(connection): getConnectionCredentials should return credentials even on error #2870
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import { LogActionEnum, LogTypes, proxyService, connectionService, telemetry, ge | |
import * as postConnectionHandlers from './index.js'; | ||
import type { LogContext, LogContextGetter } from '@nangohq/logs'; | ||
import { stringifyError } from '@nangohq/utils'; | ||
import { connectionRefreshFailed as connectionRefreshFailedHook, connectionRefreshSuccess as connectionRefreshSuccessHook } from '../hooks.js'; | ||
import type { InternalProxyConfiguration } from '@nangohq/types'; | ||
|
||
type PostConnectionHandler = (internalNango: InternalNango) => Promise<void>; | ||
|
@@ -23,22 +22,12 @@ async function execute(createdConnection: RecentlyCreatedConnection, providerNam | |
const { connection: upsertedConnection, environment, account } = createdConnection; | ||
let logCtx: LogContext | undefined; | ||
try { | ||
const credentialResponse = await connectionService.getConnectionCredentials({ | ||
account, | ||
environment, | ||
connectionId: upsertedConnection.connection_id, | ||
providerConfigKey: upsertedConnection.provider_config_key, | ||
logContextGetter, | ||
instantRefresh: false, | ||
onRefreshSuccess: connectionRefreshSuccessHook, | ||
onRefreshFailed: connectionRefreshFailedHook | ||
}); | ||
|
||
if (credentialResponse.isErr()) { | ||
const connectionRes = await connectionService.getConnection(upsertedConnection.connection_id, upsertedConnection.provider_config_key, environment.id); | ||
if (connectionRes.error || !connectionRes.response) { | ||
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. On post connection I don't think we need to test again the credentials since we just fetched them, but maybe I forgot an edge case? @khaliqgant 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. No, should be fine to just use |
||
return; | ||
} | ||
|
||
const { value: connection } = credentialResponse; | ||
const connection = connectionRes.response; | ||
|
||
const internalConfig: InternalProxyConfiguration = { | ||
connection, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ export function refreshConnectionsCron(): void { | |
} | ||
|
||
export async function exec(): Promise<void> { | ||
return await tracer.trace<Promise<void>>('nango.server.cron.refreshConnections', async (span) => { | ||
await tracer.trace<Promise<void>>('nango.server.cron.refreshConnections', async (span) => { | ||
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. exec is itself awaited above. 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. yes, it's just eslint best practice rules when you return void https://typescript-eslint.io/rules/no-confusing-void-expression/ |
||
let lock: Lock | undefined; | ||
try { | ||
logger.info(`${cronName} starting`); | ||
|
@@ -56,21 +56,22 @@ export async function exec(): Promise<void> { | |
const limit = 1000; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const staleConnections = await connectionService.getStaleConnections({ days: 0, limit, cursor }); | ||
const staleConnections = await connectionService.getStaleConnections({ days: 1, limit, cursor }); | ||
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. I think it was an error, let me know @TBonnin 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. yes, that's a test leftover. Thank you for fixing |
||
logger.info(`${cronName} found ${staleConnections.length} stale connections`); | ||
for (const staleConnection of staleConnections) { | ||
if (Date.now() - startTimestamp > ttlMs) { | ||
logger.info(`${cronName} time limit reached, stopping`); | ||
return; | ||
} | ||
const { connection_id, environment, provider_config_key, account } = staleConnection; | ||
logger.info(`${cronName} refreshing connection '${connection_id}' for accountId '${account.id}'`); | ||
|
||
const { connection, account, environment, integration } = staleConnection; | ||
logger.info(`${cronName} refreshing connection '${connection.connection_id}' for accountId '${account.id}'`); | ||
try { | ||
const credentialResponse = await connectionService.getConnectionCredentials({ | ||
const credentialResponse = await connectionService.refreshOrTestCredentials({ | ||
account, | ||
environment, | ||
connectionId: connection_id, | ||
providerConfigKey: provider_config_key, | ||
integration, | ||
connection, | ||
logContextGetter, | ||
instantRefresh: false, | ||
onRefreshSuccess: connectionRefreshSuccessHook, | ||
|
@@ -83,7 +84,7 @@ export async function exec(): Promise<void> { | |
metrics.increment(metrics.Types.REFRESH_CONNECTIONS_FAILED); | ||
} | ||
} catch (err) { | ||
logger.error(`${cronName} failed to refresh connection '${connection_id}' ${stringifyError(err)}`); | ||
logger.error(`${cronName} failed to refresh connection '${connection.connection_id}' ${stringifyError(err)}`); | ||
metrics.increment(metrics.Types.REFRESH_CONNECTIONS_FAILED); | ||
} | ||
cursor = staleConnection.cursor; | ||
|
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.
should we return something else than 200 in case of error?
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.
The thing is in the UI it's still used to display the page, before that we returned an error by did not respected that and still displayed the page. My take on this: it should be a totally different endpoint.
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.
ok. can you add a comment?