{"record":{"id":"382f5e9d43c119bb","repo":"ToolJet/ToolJet","slug":"connection-could-not-be-established-382f5e","errorCode":null,"errorMessage":"Connection could not be established","messagePattern":"Connection could not be established","errorType":"exception","errorClass":"QueryError","httpStatus":null,"severity":"error","filePath":"marketplace/plugins/portkey/lib/index.ts","lineNumber":54,"sourceCode":"      throw new QueryError('Query could not be completed', error?.message, {});\n    }\n    return {\n      status: 'ok',\n      data: result,\n    };\n  }\n\n  async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {\n    const portkey: PortKeyAi.Portkey = await this.getConnection(sourceOptions);\n    try {\n      const response = await portkey.models.list();\n      if (response.data !== undefined) {\n        return {\n          status: 'ok',\n        };\n      }\n    } catch (error) {\n      throw new QueryError('Connection could not be established', error?.message, {});\n    }\n  }\n\n  async getConnection(sourceOptions: SourceOptions): Promise<PortKeyAi.Portkey> {\n    const { apiKey, virtualKey, config } = sourceOptions;\n    const creds = { apiKey, virtualKey };\n    if (config) {\n      creds['config'] = typeof config === 'string' ? JSON.parse(config) : null;\n    }\n    return new PortKeyAi.Portkey(creds);\n  }\n}\n","sourceCodeStart":36,"sourceCodeEnd":67,"githubUrl":"https://github.com/ToolJet/ToolJet/blob/20602a8e101f2e59686c9afde0d1402aac2c8871/marketplace/plugins/portkey/lib/index.ts#L36-L67","documentation":"testConnection() catch at portkey/lib/index.ts:54. It calls portkey.models.list() and, on throw, surfaces QueryError('Connection could not be established', error?.message, {}). Note a subtle bug: if models.list() resolves but response.data is undefined (no error thrown), the function falls through without returning, yielding undefined instead of a ConnectionTestResult — the caller may then misinterpret the result. Genuine failures (bad apiKey, virtualKey, network) hit this catch.","triggerScenarios":"Invalid apiKey or virtualKey causing portkey.models.list() to reject; network failure reaching Portkey; Portkey API changing its models endpoint; rate limited.","commonSituations":"Wrong/rotated apiKey in datasource config, virtualKey revoked, Portkey service outage, SDK version mismatch where models.list() throws differently.","solutions":["Verify apiKey/virtualKey in sourceOptions are valid and active in the Portkey dashboard.","Handle the implicit-undefined case: add an explicit `return { status: 'failed' }` when response.data is undefined.","Log error?.message and error?.status to distinguish auth (401) from network/rate-limit (429/5xx)."],"exampleFix":"// before\n    try {\n      const response = await portkey.models.list();\n      if (response.data !== undefined) {\n        return { status: 'ok' };\n      }\n    } catch (error) {\n      throw new QueryError('Connection could not be established', error?.message, {});\n    }\n  }\n// after — explicit failure on undefined data and richer error\n    try {\n      const response = await portkey.models.list();\n      if (response.data !== undefined) {\n        return { status: 'ok' };\n      }\n      return { status: 'failed', message: 'Portkey returned no model data' };\n    } catch (error) {\n      throw new QueryError(\n        'Connection could not be established',\n        error?.message ?? 'Unknown Portkey error',\n        { status: error?.status }\n      );\n    }\n  }","handlingStrategy":"try-catch","validationCode":"function validatePortkeyConnection(sourceOptions) {\n  if (!sourceOptions?.apiKey && !sourceOptions?.virtualKey) {\n    throw new Error('apiKey or virtualKey is required');\n  }\n}","typeGuard":"function isPortkeyModelsResponse(value: unknown): value is { data: unknown[] } {\n  return typeof value === 'object' && value !== null && Array.isArray((value as any).data);\n}","tryCatchPattern":"try {\n  const response = await portkey.models.list();\n  if (!isPortkeyModelsResponse(response)) {\n    return { status: 'failed', message: 'Portkey returned no model data' };\n  }\n  return { status: 'ok' };\n} catch (error) {\n  throw new QueryError('Connection could not be established', error?.message ?? String(error), { status: error?.status });\n}","preventionTips":["Always return an explicit ConnectionTestResult from every branch (including the data-undefined fallthrough).","Distinguish 401 (auth) from 429/5xx (transient) by inspecting error.status.","Validate apiKey/virtualKey before calling models.list()."],"tags":["portkey","llm","connection","authentication","typescript"],"backgroundTag":null,"analyzedSha":"20602a8e101f2e59686c9afde0d1402aac2c8871","analyzedAt":"2026-08-13T05:58:54.221Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}