You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: add error handling and log warning for ormconfig loading failures - #11871
This PR adds warning messages when ormconfig configuration files fail to load, addressing issue #6960.
Problem:
Previously, when TypeORM's ConnectionOptionsReader encountered errors loading ormconfig files (e.g., syntax errors in ormconfig.js, invalid JSON in ormconfig.json, or module resolution failures), these errors were silently swallowed. This led to confusing debugging sessions where developers spent hours trying to figure out why their configuration wasn't working, with no indication that the file loading had failed.
Solution:
Added try-catch blocks around file loading operations in ConnectionOptionsReader.load() that:
Catch errors when loading .env, .js, .mjs, .cjs, .ts, .mts, .cts, and .json configuration files
Log clear warning messages using PlatformTools.logWarn() with:
The specific file path that failed to load
The actual error message from the failure
Allow execution to continue gracefully (existing behavior preserved)
Example output:
Warning: Could not load ormconfig file at /path/to/ormconfig.js
Cannot find module 'some-missing-dependency'
Verification:
Added comprehensive unit tests in test/unit/connection-options-reader/connection-options-reader.test.ts
Tests verify that warnings are logged when configuration files fail to load
All existing tests continue to pass
Manually tested with malformed configuration files to confirm warning messages appear
Benefits:
Developers immediately see when configuration loading fails
Error messages include the specific file path and error details
Significantly reduces debugging time for configuration issues
No breaking changes - existing behavior preserved
Pull-Request Checklist
Code is up-to-date with the master branch
This pull request links relevant issues as Fixes #6960
There are new or updated tests validating the change (tests/**.test.ts)
Documentation has been updated to reflect this change (docs/docs/**.md)
The test only covers malformed JS files. Consider adding tests for other file formats mentioned in the implementation (.json, .env, .mjs, .cjs, .ts, .mts, .cts) to ensure warnings are logged correctly for all supported configuration file types.
it("should log warning when ormconfig file fails to load",async()=>{// Create a malformed JS config fileawaitfs.writeFile("./temp/configs/malformed-config.js","module.exports = { invalid syntax here",)// Spy on logWarnconstoriginalLogWarn=PlatformTools.logWarnletwarnCalled=falseletwarnMessage=""PlatformTools.logWarn=(...args: any[])=>{warnCalled=truewarnMessage=args.join(" ")}try{constreader=newConnectionOptionsReader({root: "./temp",configName: "configs/malformed-config",})awaitreader.all()}catch(err: any){expect(err.message).to.include("No connection options were found")}finally{PlatformTools.logWarn=originalLogWarnawaitfs.unlink("./temp/configs/malformed-config.js")}expect(warnCalled).to.be.trueexpect(warnMessage).to.include("Could not load ormconfig file")})
When configuration file loading fails, the code logs a warning but continues execution. If connectionOptions remains undefined, the method returns an empty array. This might lead to confusing behavior where users see a warning but the application continues without proper configuration, potentially causing issues downstream.
try{const[importOrRequireResult,moduleSystem]=awaitimportOrRequireFile(configFile)constconfigModule=awaitimportOrRequireResultif(moduleSystem==="esm"||(configModule&&"__esModule"inconfigModule&&"default"inconfigModule)){connectionOptions=configModule.default}else{connectionOptions=configModule}}catch(err){PlatformTools.logWarn(`Warning: Could not load ormconfig file at ${configFile}`,errinstanceofError ? err.message : String(err),)}
To prevent resource leaks, refactor the test by wrapping the setup and execution logic within a try...finally block to ensure cleanup code is always executed.
Why: The suggestion correctly identifies a potential resource leak in the test case where cleanup might not run if setup fails, and proposes a valid structural improvement to make the test more robust.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of change
This PR adds warning messages when ormconfig configuration files fail to load, addressing issue #6960.
Problem:
Previously, when TypeORM's
ConnectionOptionsReaderencountered errors loading ormconfig files (e.g., syntax errors inormconfig.js, invalid JSON inormconfig.json, or module resolution failures), these errors were silently swallowed. This led to confusing debugging sessions where developers spent hours trying to figure out why their configuration wasn't working, with no indication that the file loading had failed.Solution:
Added try-catch blocks around file loading operations in
ConnectionOptionsReader.load()that:.env,.js,.mjs,.cjs,.ts,.mts,.cts, and.jsonconfiguration filesPlatformTools.logWarn()with:Example output:
Verification:
test/unit/connection-options-reader/connection-options-reader.test.tsBenefits:
Pull-Request Checklist
masterbranchFixes #6960tests/**.test.ts)docs/docs/**.md)Fixes #6960