Skip to content

Commit

Permalink
feat(NODE-4847): Add config error handling to logging (#3970)
Browse files Browse the repository at this point in the history
Co-authored-by: Alena Khineika <alena.khineika@gmail.com>
  • Loading branch information
aditi-khare-mongoDB and alenakhineika authored Jan 25, 2024
1 parent 38fb2e4 commit 8f7bb59
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 86 deletions.
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)) ||
(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;
}
},
/**
* @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);
});
});
});
Expand Down
Loading

0 comments on commit 8f7bb59

Please sign in to comment.