ToolJet/ToolJet · error · QueryError

Connection could not be established

Error message

Connection could not be established

What it means

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.

Source

Thrown at marketplace/plugins/portkey/lib/index.ts:54

      throw new QueryError('Query could not be completed', error?.message, {});
    }
    return {
      status: 'ok',
      data: result,
    };
  }

  async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
    const portkey: PortKeyAi.Portkey = await this.getConnection(sourceOptions);
    try {
      const response = await portkey.models.list();
      if (response.data !== undefined) {
        return {
          status: 'ok',
        };
      }
    } catch (error) {
      throw new QueryError('Connection could not be established', error?.message, {});
    }
  }

  async getConnection(sourceOptions: SourceOptions): Promise<PortKeyAi.Portkey> {
    const { apiKey, virtualKey, config } = sourceOptions;
    const creds = { apiKey, virtualKey };
    if (config) {
      creds['config'] = typeof config === 'string' ? JSON.parse(config) : null;
    }
    return new PortKeyAi.Portkey(creds);
  }
}

View on GitHub (pinned to 20602a8e10)

Solutions

  1. Verify apiKey/virtualKey in sourceOptions are valid and active in the Portkey dashboard.
  2. Handle the implicit-undefined case: add an explicit `return { status: 'failed' }` when response.data is undefined.
  3. Log error?.message and error?.status to distinguish auth (401) from network/rate-limit (429/5xx).

Example fix

// before
    try {
      const response = await portkey.models.list();
      if (response.data !== undefined) {
        return { status: 'ok' };
      }
    } catch (error) {
      throw new QueryError('Connection could not be established', error?.message, {});
    }
  }
// after — explicit failure on undefined data and richer error
    try {
      const response = await portkey.models.list();
      if (response.data !== undefined) {
        return { status: 'ok' };
      }
      return { status: 'failed', message: 'Portkey returned no model data' };
    } catch (error) {
      throw new QueryError(
        'Connection could not be established',
        error?.message ?? 'Unknown Portkey error',
        { status: error?.status }
      );
    }
  }
Defensive patterns

Strategy: try-catch

Validate before calling

function validatePortkeyConnection(sourceOptions) {
  if (!sourceOptions?.apiKey && !sourceOptions?.virtualKey) {
    throw new Error('apiKey or virtualKey is required');
  }
}

Type guard

function isPortkeyModelsResponse(value: unknown): value is { data: unknown[] } {
  return typeof value === 'object' && value !== null && Array.isArray((value as any).data);
}

Try / catch

try {
  const response = await portkey.models.list();
  if (!isPortkeyModelsResponse(response)) {
    return { status: 'failed', message: 'Portkey returned no model data' };
  }
  return { status: 'ok' };
} catch (error) {
  throw new QueryError('Connection could not be established', error?.message ?? String(error), { status: error?.status });
}

Prevention

When it happens

Trigger: Invalid apiKey or virtualKey causing portkey.models.list() to reject; network failure reaching Portkey; Portkey API changing its models endpoint; rate limited.

Common situations: Wrong/rotated apiKey in datasource config, virtualKey revoked, Portkey service outage, SDK version mismatch where models.list() throws differently.

Related errors


AI-assisted analysis of ToolJet/ToolJet@20602a8e10 (2026-08-13). Data as JSON: /api/errors/382f5e9d43c119bb. Report an issue: GitHub.