Skip to content

Commit 3f4bc16

Browse files
authored
fix: enhance error handling and logging in BuildResolutionState method (#39)
1 parent 1090c43 commit 3f4bc16

1 file changed

Lines changed: 135 additions & 50 deletions

File tree

src/JD.Efcpt.Build.Tasks/ResolveSqlProjAndInputs.cs

Lines changed: 135 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -408,49 +408,110 @@ private record TargetContext(bool UseConnectionStringMode, string ConnectionStri
408408

409409
private ResolutionState BuildResolutionState(BuildLog log)
410410
{
411-
// Determine mode using priority-based resolution
412-
var (useConnectionStringMode, connectionString, sqlProjPath) = DetermineMode(log);
411+
// Step 1: Determine mode using priority-based resolution
412+
log.Detail("BuildResolutionState: Step 1 - DetermineMode starting");
413+
TargetContext? targetContext = null;
414+
try
415+
{
416+
targetContext = DetermineMode(log);
417+
}
418+
catch (Exception ex)
419+
{
420+
log.Warn($"BuildResolutionState: DetermineMode threw: {ex.GetType().Name}: {ex.Message}");
421+
throw;
422+
}
413423

414-
return Composer<ResolutionState, ResolutionState>
415-
.New(() => default)
416-
.With(state => state with
417-
{
418-
ConnectionString = connectionString,
419-
UseConnectionStringMode = useConnectionStringMode,
420-
SqlProjPath = sqlProjPath
421-
})
422-
.With(state => state with
423-
{
424-
ConfigPath = ResolveFile(ConfigOverride, "efcpt-config.json")
425-
})
426-
.With(state => state with
427-
{
428-
RenamingPath = ResolveFile(
429-
RenamingOverride,
430-
"efcpt.renaming.json",
431-
"efcpt-renaming.json",
432-
"efpt.renaming.json")
433-
})
434-
.With(state => state with
435-
{
436-
TemplateDir = ResolveDir(
437-
TemplateDirOverride,
438-
"Template",
439-
"CodeTemplates",
440-
"Templates")
441-
})
442-
// Either connection string or SQL project must be resolved
443-
.Require(state
444-
=> state.UseConnectionStringMode
445-
? string.IsNullOrWhiteSpace(state.ConnectionString)
446-
? "Connection string resolution failed. No connection string could be resolved from configuration."
447-
: null
448-
: string.IsNullOrWhiteSpace(state.SqlProjPath)
449-
? "SqlProj resolution failed. No SQL project reference found. " +
450-
"Add a .sqlproj ProjectReference, set EfcptSqlProj property, or provide a connection string via " +
451-
"EfcptConnectionString/appsettings.json/app.config. Check build output for detailed error messages."
452-
: null)
453-
.Build(state => state);
424+
var useConnectionStringMode = targetContext?.UseConnectionStringMode ?? false;
425+
var connectionString = targetContext?.ConnectionString ?? "";
426+
var sqlProjPath = targetContext?.SqlProjPath ?? "";
427+
428+
log.Detail($"BuildResolutionState: Step 1 complete - UseConnectionStringMode={useConnectionStringMode}, " +
429+
$"ConnectionString={(string.IsNullOrEmpty(connectionString) ? "(empty)" : "(set)")}, " +
430+
$"SqlProjPath={(string.IsNullOrEmpty(sqlProjPath) ? "(empty)" : sqlProjPath)}");
431+
432+
// Step 2: Resolve config file
433+
log.Detail("BuildResolutionState: Step 2 - ResolveFile for config starting");
434+
log.Detail($" ConfigOverride={(ConfigOverride ?? "(null)")}");
435+
log.Detail($" ProjectDirectory={(ProjectDirectory ?? "(null)")}");
436+
log.Detail($" DefaultsRoot={(DefaultsRoot ?? "(null)")}");
437+
string configPath;
438+
try
439+
{
440+
configPath = ResolveFile(ConfigOverride ?? "", "efcpt-config.json");
441+
}
442+
catch (Exception ex)
443+
{
444+
log.Warn($"BuildResolutionState: ResolveFile(config) threw: {ex.GetType().Name}: {ex.Message}");
445+
throw;
446+
}
447+
log.Detail($"BuildResolutionState: Step 2 complete - ConfigPath={configPath}");
448+
449+
// Step 3: Resolve renaming file
450+
log.Detail("BuildResolutionState: Step 3 - ResolveFile for renaming starting");
451+
log.Detail($" RenamingOverride={(RenamingOverride ?? "(null)")}");
452+
string renamingPath;
453+
try
454+
{
455+
renamingPath = ResolveFile(
456+
RenamingOverride ?? "",
457+
"efcpt.renaming.json",
458+
"efcpt-renaming.json",
459+
"efpt.renaming.json");
460+
}
461+
catch (Exception ex)
462+
{
463+
log.Warn($"BuildResolutionState: ResolveFile(renaming) threw: {ex.GetType().Name}: {ex.Message}");
464+
throw;
465+
}
466+
log.Detail($"BuildResolutionState: Step 3 complete - RenamingPath={renamingPath}");
467+
468+
// Step 4: Resolve template directory
469+
log.Detail("BuildResolutionState: Step 4 - ResolveDir for templates starting");
470+
log.Detail($" TemplateDirOverride={(TemplateDirOverride ?? "(null)")}");
471+
string templateDir;
472+
try
473+
{
474+
templateDir = ResolveDir(
475+
TemplateDirOverride ?? "",
476+
"Template",
477+
"CodeTemplates",
478+
"Templates");
479+
}
480+
catch (Exception ex)
481+
{
482+
log.Warn($"BuildResolutionState: ResolveDir(templates) threw: {ex.GetType().Name}: {ex.Message}");
483+
throw;
484+
}
485+
log.Detail($"BuildResolutionState: Step 4 complete - TemplateDir={templateDir}");
486+
487+
// Step 5: Validate that either connection string or SQL project was resolved
488+
log.Detail("BuildResolutionState: Step 5 - Validation");
489+
if (useConnectionStringMode)
490+
{
491+
if (string.IsNullOrWhiteSpace(connectionString))
492+
throw new InvalidOperationException(
493+
"Connection string resolution failed. No connection string could be resolved from configuration.");
494+
}
495+
else
496+
{
497+
if (string.IsNullOrWhiteSpace(sqlProjPath))
498+
throw new InvalidOperationException(
499+
"SqlProj resolution failed. No SQL project reference found. " +
500+
"Add a .sqlproj ProjectReference, set EfcptSqlProj property, or provide a connection string via " +
501+
"EfcptConnectionString/appsettings.json/app.config. Check build output for detailed error messages.");
502+
}
503+
504+
log.Detail("BuildResolutionState: All steps complete, building ResolutionState");
505+
506+
// Build the final state
507+
return new ResolutionState(
508+
SqlProjPath: sqlProjPath,
509+
ConfigPath: configPath,
510+
RenamingPath: renamingPath,
511+
TemplateDir: templateDir,
512+
ConnectionString: connectionString,
513+
UseConnectionStringMode: useConnectionStringMode
514+
);
454515
}
455516

456517
private string ResolveSqlProjWithValidation(BuildLog log)
@@ -614,15 +675,27 @@ private static bool IsProjectFile(string? extension)
614675

615676
private string ResolveFile(string overridePath, params string[] fileNames)
616677
{
678+
// Ensure all inputs are non-null
679+
overridePath ??= "";
680+
var projectDir = ProjectDirectory ?? "";
681+
var solutionDir = SolutionDir ?? "";
682+
var defaultsRoot = DefaultsRoot ?? "";
683+
var probeSolutionDir = (ProbeSolutionDir ?? "true").IsTrue();
684+
617685
var chain = FileResolutionChain.Build();
686+
if (chain == null)
687+
throw new InvalidOperationException("FileResolutionChain.Build() returned null");
688+
618689
var candidates = EnumerableExtensions.BuildCandidateNames(overridePath, fileNames);
690+
if (candidates == null)
691+
throw new InvalidOperationException("BuildCandidateNames returned null");
619692

620693
var context = new FileResolutionContext(
621694
OverridePath: overridePath,
622-
ProjectDirectory: ProjectDirectory,
623-
SolutionDir: SolutionDir,
624-
ProbeSolutionDir: ProbeSolutionDir.IsTrue(),
625-
DefaultsRoot: DefaultsRoot,
695+
ProjectDirectory: projectDir,
696+
SolutionDir: solutionDir,
697+
ProbeSolutionDir: probeSolutionDir,
698+
DefaultsRoot: defaultsRoot,
626699
FileNames: candidates);
627700

628701
return chain.Execute(in context, out var result)
@@ -632,15 +705,27 @@ private string ResolveFile(string overridePath, params string[] fileNames)
632705

633706
private string ResolveDir(string overridePath, params string[] dirNames)
634707
{
708+
// Ensure all inputs are non-null
709+
overridePath ??= "";
710+
var projectDir = ProjectDirectory ?? "";
711+
var solutionDir = SolutionDir ?? "";
712+
var defaultsRoot = DefaultsRoot ?? "";
713+
var probeSolutionDir = (ProbeSolutionDir ?? "true").IsTrue();
714+
635715
var chain = DirectoryResolutionChain.Build();
716+
if (chain == null)
717+
throw new InvalidOperationException("DirectoryResolutionChain.Build() returned null");
718+
636719
var candidates = EnumerableExtensions.BuildCandidateNames(overridePath, dirNames);
720+
if (candidates == null)
721+
throw new InvalidOperationException("BuildCandidateNames returned null");
637722

638723
var context = new DirectoryResolutionContext(
639724
OverridePath: overridePath,
640-
ProjectDirectory: ProjectDirectory,
641-
SolutionDir: SolutionDir,
642-
ProbeSolutionDir: ProbeSolutionDir.IsTrue(),
643-
DefaultsRoot: DefaultsRoot,
725+
ProjectDirectory: projectDir,
726+
SolutionDir: solutionDir,
727+
ProbeSolutionDir: probeSolutionDir,
728+
DefaultsRoot: defaultsRoot,
644729
DirNames: candidates);
645730

646731
return chain.Execute(in context, out var result)

0 commit comments

Comments
 (0)