Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-5999): Change TopologyDescription.error type to MongoError #4028

Merged
merged 8 commits into from
Mar 14, 2024
6 changes: 3 additions & 3 deletions src/sdam/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
// clear for the specific service id.
if (!this.loadBalanced) {
error.addErrorLabel(MongoErrorLabel.ResetPool);
markServerUnknown(this, error as MongoServerError);
markServerUnknown(this, error);
} else if (connection) {
this.pool.clear({ serviceId: connection.serviceId });
}
Expand All @@ -373,7 +373,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
if (shouldClearPool) {
error.addErrorLabel(MongoErrorLabel.ResetPool);
}
markServerUnknown(this, error as MongoServerError);
markServerUnknown(this, error);
process.nextTick(() => this.requestCheck());
}
}
Expand Down Expand Up @@ -476,7 +476,7 @@ function calculateRoundTripTime(oldRtt: number, duration: number): number {
return alpha * duration + (1 - alpha) * oldRtt;
}

function markServerUnknown(server: Server, error?: MongoServerError) {
function markServerUnknown(server: Server, error?: MongoError) {
// Load balancer servers can never be marked unknown.
if (server.loadBalanced) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/sdam/server_description.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Document, Long, type ObjectId } from '../bson';
import { type MongoError, MongoRuntimeError, type MongoServerError } from '../error';
import { type MongoError, MongoRuntimeError } from '../error';
import { arrayStrictEqual, compareObjectId, errorStrictEqual, HostAddress, now } from '../utils';
import type { ClusterTime } from './common';
import { ServerType } from './common';
Expand Down Expand Up @@ -31,7 +31,7 @@ export type TagSet = { [key: string]: string };
/** @internal */
export interface ServerDescriptionOptions {
/** An Error used for better reporting debugging */
error?: MongoServerError;
error?: MongoError;

/** The round trip time to ping this server (in ms) */
roundTripTime?: number;
Expand Down
6 changes: 3 additions & 3 deletions src/sdam/topology_description.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ObjectId } from '../bson';
import * as WIRE_CONSTANTS from '../cmap/wire_protocol/constants';
import { MongoRuntimeError, type MongoServerError } from '../error';
import { type MongoError, MongoRuntimeError } from '../error';
import { compareObjectId, shuffle } from '../utils';
import { ServerType, TopologyType } from './common';
import { ServerDescription } from './server_description';
Expand Down Expand Up @@ -307,13 +307,13 @@ export class TopologyDescription {
);
}

get error(): MongoServerError | null {
get error(): MongoError | null {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically public. I think this change is okay to make, because we usually permit breaking TS fixes in non-major releases. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is an expanded type considered a breaking change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm on second thought - if we had made this change before c023242, I don't think this would have been breaking. but since MongoServerError is no longer assignable to MongoError, it's now breaking to make this change? But I still think that's okay. This is a correctness fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MongoServerError inherits from MongoError, so MongoServerError can be assigned to MongoError, but not the other way around. This is why we had to make this change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interface Foo {
  name: string;
}

interface Bar extends Foo {
  age: number;
}

const document: Bar = { name: 'bumpy', age: 7 };

If Bar is changed to Foo, this code fails to compile. That's why this change is potentially problematic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm I see. Should we wait until the next major version to make this change?

const descriptionsWithError = Array.from(this.servers.values()).filter(
(sd: ServerDescription) => sd.error
);

if (descriptionsWithError.length > 0) {
return descriptionsWithError[0].error as MongoServerError;
return descriptionsWithError[0].error;
}

return null;
Expand Down
Loading