Skip to content

Commit 484680d

Browse files
W-A-Jamesnbbeeken
authored andcommitted
minimal functional code
1 parent dc2eab7 commit 484680d

File tree

1 file changed

+27
-80
lines changed

1 file changed

+27
-80
lines changed

src/sdam/server.ts

Lines changed: 27 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {
3333
MongoRuntimeError,
3434
MongoServerClosedError,
3535
type MongoServerError,
36-
MongoUnexpectedServerResponseError,
3736
needsRetryableWriteLabel
3837
} from '../error';
3938
import type { ServerApi } from '../mongo_client';
@@ -323,47 +322,17 @@ export class Server extends TypedEventEmitter<ServerEvents> {
323322
return this.command(ns, cmd, finalOptions);
324323
}
325324

326-
this.incrementOperationCount();
327-
328-
/*
329-
const executeCommand: WithConnectionCallBack = (err, conn, cb) => {
330-
if (err || !conn) {
331-
this.decrementOperationCount();
332-
if (!err) {
333-
return cb(new MongoRuntimeError('Failed to create connection without error'));
334-
}
335-
if (!(err instanceof PoolClearedError)) {
336-
this.handleError(err);
337-
}
338-
return cb(err);
339-
}
340-
341-
const handler = makeOperationHandler(this, conn, cmd, finalOptions, (error, response) => {
342-
this.decrementOperationCount();
343-
cb(error, response);
344-
});
345-
conn.command(ns, cmd, finalOptions).then(
346-
r => handler(undefined, r),
347-
e => handler(e)
348-
);
349-
};
350-
*/
351-
352-
const executeCommandAsync = async (err?: AnyError, conn?: Connection) => {
353-
if (err || !conn) {
354-
this.decrementOperationCount();
355-
if (!err) throw new MongoRuntimeError('Failed to create connection without error');
356-
if (!(err instanceof PoolClearedError)) this.handleError(err);
357-
358-
throw err;
359-
}
325+
const runCommand = async (conn: Connection) => {
326+
this.incrementOperationCount();
360327
try {
361-
const result = await conn.command(ns, cmd, finalOptions);
362-
return await this.operationHandler(conn, cmd, finalOptions, undefined, result);
328+
return await conn.command(ns, cmd, finalOptions);
363329
} catch (e) {
364-
return await this.operationHandler(conn, cmd, finalOptions, e, undefined);
330+
throw this.decorateAndHandleError(conn, cmd, finalOptions, e);
331+
} finally {
332+
this.decrementOperationCount();
365333
}
366334
};
335+
367336
const shouldReauth = (e: AnyError): boolean => {
368337
return e instanceof MongoError && e.code === MONGODB_ERROR_CODES.Reauthenticate;
369338
};
@@ -372,47 +341,39 @@ export class Server extends TypedEventEmitter<ServerEvents> {
372341
if (conn) {
373342
// send operation, possibly reauthenticating and return without checking back in
374343
try {
375-
return await executeCommandAsync(undefined, conn);
344+
return await runCommand(conn);
376345
} catch (e) {
377346
if (shouldReauth(e)) {
378347
conn = await this.pool.reauthenticateAsync(conn);
379-
return await executeCommandAsync(undefined, conn);
348+
return await runCommand(conn);
380349
}
381-
382350
throw e;
383351
}
384352
}
385353

386354
// check out connection
387-
conn = await this.pool.checkOut();
355+
try {
356+
conn = await this.pool.checkOut();
357+
} catch (e) {
358+
if (!e) throw new MongoRuntimeError('Failed to create connection without error');
359+
if (!(e instanceof PoolClearedError)) this.handleError(e);
388360

389-
let rv: Document;
361+
throw e;
362+
}
390363
// call executeCommandAsync with that checked out connection
391364
try {
392-
rv = await executeCommandAsync(undefined, conn);
365+
return await runCommand(conn);
393366
} catch (e) {
394367
// if it errors
395-
if (conn) {
396-
// if connection is defined
397-
// reauthenticate
398-
if (shouldReauth(e)) {
399-
conn = await this.pool.reauthenticateAsync(conn);
400-
rv = await executeCommandAsync(undefined, conn);
401-
} else {
402-
throw e;
403-
}
404-
} else {
405-
// throw the error
406-
throw e;
368+
// check if we should reauthenticate
369+
if (shouldReauth(e)) {
370+
conn = await this.pool.reauthenticateAsync(conn);
371+
return await runCommand(conn);
407372
}
408-
}
409-
410-
// if connection exists
411-
// check connection back into pool
412-
if (conn) {
373+
throw e;
374+
} finally {
413375
this.pool.checkIn(conn);
414376
}
415-
return rv;
416377
}
417378

418379
/**
@@ -463,27 +424,13 @@ export class Server extends TypedEventEmitter<ServerEvents> {
463424
}
464425
}
465426

466-
private async operationHandler<T = Document>(
427+
private decorateAndHandleError(
467428
connection: Connection,
468429
cmd: Document,
469430
options: CommandOptions | GetMoreOptions | undefined,
470-
error?: AnyError,
471-
result?: any
472-
): Promise<T> {
431+
error: AnyError
432+
): AnyError {
473433
const session = options?.session;
474-
// We should not swallow an error if it is present.
475-
if (error == null && result != null) {
476-
return result;
477-
}
478-
479-
if (options != null && 'noResponse' in options && options.noResponse === true) {
480-
return null as T;
481-
}
482-
483-
if (!error) {
484-
throw new MongoUnexpectedServerResponseError('Empty response with no error');
485-
}
486-
487434
if (error.name === 'AbortError' && error.cause instanceof MongoError) {
488435
error = error.cause;
489436
}
@@ -537,7 +484,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
537484

538485
this.handleError(error, connection);
539486

540-
throw error;
487+
return error;
541488
}
542489

543490
/**

0 commit comments

Comments
 (0)