Skip to content

Commit 5504fdf

Browse files
authored
Throw readable error if passing endpoint and keyspace as a string to db() (#55)
"Unexpected db() argument: database id can't start with "https://". Did you mean to call .db(endpoint, { namespace })?"
1 parent cb66f2c commit 5504fdf

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/data-api/db.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ export function mkDb(rootOpts: InternalRootClientOpts, endpointOrId: string, reg
489489

490490
validateDbOpts(options);
491491

492+
if (typeof regionOrOptions === 'string' && endpointOrId.startsWith('https://')) {
493+
throw new Error('Unexpected db() argument: database id can\'t start with "https://". Did you mean to call `.db(endpoint, { namespace })`?');
494+
}
495+
492496
const endpoint = (typeof regionOrOptions === 'string')
493497
? 'https://' + endpointOrId + '-' + regionOrOptions + '.apps.astra.datastax.com'
494498
: endpointOrId;

tests/integration/client/data-api-client.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,13 @@ describe('integration.client.data-api-client', () => {
286286
assert.ok(failedEvent.error instanceof DataAPITimeoutError);
287287
assert.strictEqual(failedEvent.error.timeout, 1);
288288
});
289+
290+
it('throws an error if passing in endpoint and keyspace name as a string', () => {
291+
const client = new DataAPIClient(TEST_APPLICATION_TOKEN);
292+
assert.throws(
293+
() => client.db(TEST_ASTRA_URI, OTHER_NAMESPACE),
294+
{ message: 'Unexpected db() argument: database id can\'t start with "https://". Did you mean to call `.db(endpoint, { namespace })`?' }
295+
);
296+
});
289297
});
290298
});

0 commit comments

Comments
 (0)