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

feat(NODE-4847): Add config error handling to logging #3970

Merged
merged 7 commits into from
Jan 25, 2024
Merged
49 changes: 46 additions & 3 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import {
ServerApiVersion
} from './mongo_client';
import {
MongoLoggableComponent,
MongoLogger,
type MongoLoggerEnvOptions,
type MongoLoggerMongoClientOptions
type MongoLoggerMongoClientOptions,
SeverityLevel
} from './mongo_logger';
import { ReadConcern, type ReadConcernLevel } from './read_concern';
import { ReadPreference, type ReadPreferenceMode } from './read_preference';
Expand Down Expand Up @@ -1246,12 +1248,53 @@ export const OPTIONS = {
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogPath: { type: 'any' },
mongodbLogPath: {
transform({ values: [value] }) {
if (
!(
(typeof value === 'string' && ['stderr', 'stdout'].includes(value)) ||
alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
(value &&
typeof value === 'object' &&
'write' in value &&
typeof value.write === 'function')
)
) {
throw new MongoAPIError(
`Option 'mongodbLogPath' must be of type 'stderr' | 'stdout' | MongoDBLogWritable`
);
}
return value;
}
},
/**
* @internal
* TODO: NODE-5671 - remove internal flag
*/
mongodbLogComponentSeverities: { type: 'any' },
mongodbLogComponentSeverities: {
transform({ values: [value] }) {
if (typeof value !== 'object' || !value) {
throw new MongoAPIError(`Option 'mongodbLogComponentSeverities' must be a non-null object`);
}
for (const [k, v] of Object.entries(value)) {
if (typeof v !== 'string' || typeof k !== 'string') {
throw new MongoAPIError(
`User input for option 'mongodbLogComponentSeverities' object cannot include a non-string key or value`
);
}
if (!Object.values(MongoLoggableComponent).some(val => val === k) && k !== 'default') {
throw new MongoAPIError(
`User input for option 'mongodbLogComponentSeverities' contains invalid key: ${k}`
);
}
if (!Object.values(SeverityLevel).some(val => val === v)) {
throw new MongoAPIError(
`Option 'mongodbLogComponentSeverities' does not support ${v} as a value for ${k}`
);
}
}
return value;
}
},
alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
/**
* @internal
* TODO: NODE-5671 - remove internal flag
Expand Down
5 changes: 4 additions & 1 deletion src/mongo_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ export interface Log extends Record<string, any> {
message?: string;
}

/** @internal */
/**
* @internal
* TODO: NODE-5671 - remove internal flag and add API comments
*/
export interface MongoDBLogWritable {
write(log: Log): PromiseLike<unknown> | unknown;
}
Expand Down
12 changes: 5 additions & 7 deletions test/unit/connection_string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,13 +912,11 @@ describe('Connection String', function () {
});

context('when option is invalid', function () {
it('it defaults to stderr', function () {
const client = new MongoClient('mongodb://a/?mongodbLogPath=stdnothing', {
[loggerFeatureFlag]: true
});
const log: Log = { t: new Date(), c: 'ConnectionStringInvalidOption', s: 'error' };
client.options.mongoLoggerOptions.logDestination.write(log);
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
it('should throw error at construction', function () {
expect(
() =>
new MongoClient('mongodb://a/?mongodbLogPath=stdnothing', { [loggerFeatureFlag]: true })
).to.throw(MongoAPIError);
alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
Expand Down
Loading
Loading