Skip to content

Commit e74bc72

Browse files
Copilotdiberry
andcommitted
Improve error handling with optional chaining and status code checks
Co-authored-by: diberry <41597107+diberry@users.noreply.github.com>
1 parent f4398d4 commit e74bc72

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

nosql-vector-search-typescript/src/index-and-query.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ async function main() {
6868
console.error('App failed:', error);
6969

7070
// Provide helpful error message if resources don't exist
71-
if (error instanceof Error) {
72-
if (error.message.includes('NotFound') || error.message.includes('does not exist') ||
73-
error.message.includes('ResourceNotFound') || error.message.includes('404')) {
71+
if (error instanceof Error || (typeof error === 'object' && error !== null)) {
72+
const err = error as any;
73+
if (err?.message?.includes('NotFound') || err?.message?.includes('does not exist') ||
74+
err?.message?.includes('ResourceNotFound') || err?.code === 404 || err?.statusCode === 404) {
7475
console.error('\n=== RESOURCE NOT FOUND ===');
7576
console.error(`The database '${config.dbName}' or container '${config.collectionName}' does not exist.`);
7677
console.error('\nPlease create these resources before running this sample:');
@@ -84,7 +85,7 @@ async function main() {
8485
}
8586

8687
process.exitCode = 1;
87-
}
88+
}
8889
}
8990

9091
// Execute the main function

nosql-vector-search-typescript/src/insert-at-scale.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,8 @@ async function main() {
831831
console.log(`The calculation extrapolates your current usage pattern over a 30-day period.`);
832832
} catch (err: any) {
833833
// Provide helpful error message if resources don't exist
834-
if (err.message && (err.message.includes('NotFound') || err.message.includes('does not exist') ||
835-
err.message.includes('ResourceNotFound') || err.code === 404 || err.statusCode === 404)) {
834+
if (err && (err?.message?.includes('NotFound') || err?.message?.includes('does not exist') ||
835+
err?.message?.includes('ResourceNotFound') || err?.code === 404 || err?.statusCode === 404)) {
836836
console.error('\n=== RESOURCE NOT FOUND ===');
837837
console.error(`The database '${databaseName}' or container '${containerName}' does not exist.`);
838838
console.error('\nPlease create these resources before running this sample:');

0 commit comments

Comments
 (0)