Skip to content

Commit e0c2250

Browse files
committed
wip
1 parent 0b825f1 commit e0c2250

File tree

7 files changed

+51
-33
lines changed

7 files changed

+51
-33
lines changed

src/error.ts

+16-26
Original file line numberDiff line numberDiff line change
@@ -1157,27 +1157,14 @@ export class MongoServerSelectionError extends MongoSystemError {
11571157
}
11581158
}
11591159

1160-
function makeWriteConcernResultObject(input: any) {
1161-
const output = Object.assign({}, input);
1162-
1163-
if (output.ok === 0) {
1164-
output.ok = 1;
1165-
delete output.errmsg;
1166-
delete output.code;
1167-
delete output.codeName;
1168-
}
1169-
1170-
return output;
1171-
}
1172-
11731160
/**
11741161
* An error thrown when the server reports a writeConcernError
11751162
* @public
11761163
* @category Error
11771164
*/
11781165
export class MongoWriteConcernError extends MongoServerError {
1179-
/** The result document (provided if ok: 1) */
1180-
result?: Document;
1166+
/** The result document */
1167+
result: Document;
11811168

11821169
/**
11831170
* **Do not use this constructor!**
@@ -1190,17 +1177,20 @@ export class MongoWriteConcernError extends MongoServerError {
11901177
*
11911178
* @public
11921179
**/
1193-
constructor(message: ErrorDescription, result?: Document) {
1194-
if (result && Array.isArray(result.errorLabels)) {
1195-
message.errorLabels = result.errorLabels;
1196-
}
1197-
1198-
super(message);
1199-
this.errInfo = message.errInfo;
1200-
1201-
if (result != null) {
1202-
this.result = makeWriteConcernResultObject(result);
1203-
}
1180+
constructor({
1181+
writeConcernError,
1182+
...result
1183+
}: {
1184+
writeConcernError: {
1185+
code: number;
1186+
errmsg: string;
1187+
codeName?: string;
1188+
errInfo?: Document;
1189+
};
1190+
} & Document) {
1191+
super(writeConcernError);
1192+
this.errInfo = writeConcernError.errInfo;
1193+
this.result = result;
12041194
}
12051195

12061196
override get name(): string {

src/operations/bulk_write.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
import type { Collection } from '../collection';
88
import type { Server } from '../sdam/server';
99
import type { ClientSession } from '../sessions';
10+
import { throwIfWriteConcernError } from '../utils';
1011
import { AbstractOperation, Aspect, defineAspects } from './operation';
1112

1213
/** @internal */
@@ -51,6 +52,7 @@ export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
5152

5253
// Execute the bulk
5354
const result = await bulk.execute({ ...options, session });
55+
throwIfWriteConcernError(result);
5456
return result;
5557
}
5658
}

src/operations/delete.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MongoCompatibilityError, MongoServerError } from '../error';
44
import { type TODO_NODE_3286 } from '../mongo_types';
55
import type { Server } from '../sdam/server';
66
import type { ClientSession } from '../sessions';
7-
import type { MongoDBNamespace } from '../utils';
7+
import { type MongoDBNamespace, throwIfWriteConcernError } from '../utils';
88
import type { WriteConcernOptions } from '../write_concern';
99
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
1010
import { Aspect, defineAspects, type Hint } from './operation';
@@ -96,6 +96,7 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
9696
}
9797

9898
const res: TODO_NODE_3286 = await super.executeCommand(server, session, command);
99+
throwIfWriteConcernError(res);
99100
return res;
100101
}
101102
}

src/operations/find_and_modify.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { ReadPreference } from '../read_preference';
55
import type { Server } from '../sdam/server';
66
import type { ClientSession } from '../sessions';
77
import { formatSort, type Sort, type SortForCmd } from '../sort';
8-
import { decorateWithCollation, hasAtomicOperators, maxWireVersion } from '../utils';
8+
import {
9+
decorateWithCollation,
10+
hasAtomicOperators,
11+
maxWireVersion,
12+
throwIfWriteConcernError
13+
} from '../utils';
914
import type { WriteConcern, WriteConcernSettings } from '../write_concern';
1015
import { CommandOperation, type CommandOperationOptions } from './command';
1116
import { Aspect, defineAspects } from './operation';
@@ -214,6 +219,7 @@ export class FindAndModifyOperation extends CommandOperation<Document> {
214219

215220
// Execute the command
216221
const result = await super.executeCommand(server, session, cmd);
222+
throwIfWriteConcernError(result);
217223
return options.includeResultMetadata ? result : result.value ?? null;
218224
}
219225
}

src/operations/update.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MongoCompatibilityError, MongoInvalidArgumentError, MongoServerError }
44
import type { InferIdType, TODO_NODE_3286 } from '../mongo_types';
55
import type { Server } from '../sdam/server';
66
import type { ClientSession } from '../sessions';
7-
import { hasAtomicOperators, type MongoDBNamespace } from '../utils';
7+
import { hasAtomicOperators, type MongoDBNamespace, throwIfWriteConcernError } from '../utils';
88
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
99
import { Aspect, defineAspects, type Hint } from './operation';
1010

@@ -122,7 +122,9 @@ export class UpdateOperation extends CommandOperation<Document> {
122122
}
123123
}
124124

125-
return await super.executeCommand(server, session, command);
125+
const res = await super.executeCommand(server, session, command);
126+
throwIfWriteConcernError(res);
127+
return res;
126128
}
127129
}
128130

src/sdam/server.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ import {
4646
makeStateMachine,
4747
maxWireVersion,
4848
type MongoDBNamespace,
49-
supportsRetryableWrites
49+
supportsRetryableWrites,
50+
throwIfWriteConcernError
5051
} from '../utils';
5152
import {
5253
type ClusterTime,
@@ -323,7 +324,9 @@ export class Server extends TypedEventEmitter<ServerEvents> {
323324

324325
try {
325326
try {
326-
return await conn.command(ns, cmd, finalOptions, responseType);
327+
const res = await conn.command(ns, cmd, finalOptions, responseType);
328+
throwIfWriteConcernError(res);
329+
return res;
327330
} catch (commandError) {
328331
throw this.decorateCommandError(conn, cmd, finalOptions, commandError);
329332
}

src/utils.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { promisify } from 'util';
1111
import { deserialize, type Document, ObjectId, resolveBSONOptions } from './bson';
1212
import type { Connection } from './cmap/connection';
1313
import { MAX_SUPPORTED_WIRE_VERSION } from './cmap/wire_protocol/constants';
14+
import { MongoDBResponse } from './cmap/wire_protocol/responses';
1415
import type { Collection } from './collection';
1516
import { kDecoratedKeys, LEGACY_HELLO_COMMAND } from './constants';
1617
import type { AbstractCursor } from './cursor/abstract_cursor';
@@ -23,7 +24,8 @@ import {
2324
MongoNetworkTimeoutError,
2425
MongoNotConnectedError,
2526
MongoParseError,
26-
MongoRuntimeError
27+
MongoRuntimeError,
28+
MongoWriteConcernError
2729
} from './error';
2830
import type { Explain } from './explain';
2931
import type { MongoClient } from './mongo_client';
@@ -1416,3 +1418,15 @@ export function decorateDecryptionResult(
14161418
decorateDecryptionResult(decrypted[k], originalValue, false);
14171419
}
14181420
}
1421+
1422+
/** Called with either a plain object or MongoDBResponse */
1423+
export function throwIfWriteConcernError(response: unknown): void {
1424+
if (typeof response === 'object' && response != null) {
1425+
if (MongoDBResponse.is(response) && response.has('writeConcernError')) {
1426+
const object = response.toObject();
1427+
throw new MongoWriteConcernError(object.writeConcernError, object);
1428+
} else if ('writeConcernError' in response) {
1429+
throw new MongoWriteConcernError(response.writeConcernError as any, response);
1430+
}
1431+
}
1432+
}

0 commit comments

Comments
 (0)