Skip to content

Commit 2b3bc20

Browse files
committed
refactor: Address code review feedback for Scriban HTML formatter
- Fix CustomTemplateLoader to avoid using internal Scriban API - Replace Console.WriteLine with proper exception handling - Remove duplicate PrepareConfigurationData method - Improve error handling in GetCssStyles() and GetJavaScriptCode() - Use Debug.WriteLine for non-critical warnings instead of empty catch blocks These changes improve code robustness, eliminate code duplication, and follow better error handling practices.
1 parent 760d34e commit 2b3bc20

File tree

2 files changed

+11
-65
lines changed

2 files changed

+11
-65
lines changed

src/DotNetApiDiff/Reporting/CustomTemplateLoader.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,25 @@
55
namespace DotNetApiDiff.Reporting;
66

77
/// <summary>
8-
/// Custom template loader for Scriban that loads templates from a dictionary
8+
/// Custom template loader for Scriban that loads templates from embedded resources
99
/// </summary>
1010
public class CustomTemplateLoader : ITemplateLoader
1111
{
12-
private readonly Dictionary<string, Template> _templates;
13-
14-
public CustomTemplateLoader(Dictionary<string, Template> templates)
15-
{
16-
_templates = templates;
17-
}
18-
1912
public string GetPath(TemplateContext context, SourceSpan callerSpan, string templateName)
2013
{
2114
return templateName;
2215
}
2316

2417
public string Load(TemplateContext context, SourceSpan callerSpan, string templatePath)
2518
{
26-
if (_templates.TryGetValue(templatePath, out var template))
27-
{
28-
// Get the source code of the template
29-
return template.Page.Body.ToString();
30-
}
31-
32-
// Try loading from embedded resources directly
3319
try
3420
{
21+
// Load template source directly from embedded resources
3522
return EmbeddedTemplateLoader.LoadTemplate($"{templatePath}.scriban");
3623
}
37-
catch
24+
catch (Exception ex)
3825
{
39-
throw new InvalidOperationException($"Template '{templatePath}' not found");
26+
throw new InvalidOperationException($"Template '{templatePath}' not found: {ex.Message}", ex);
4027
}
4128
}
4229

src/DotNetApiDiff/Reporting/HtmlFormatterScriban.cs

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public HtmlFormatterScriban()
3535
}
3636
catch (Exception ex)
3737
{
38-
// Fallback to basic template if templates can't be loaded
39-
Console.WriteLine($"Warning: Could not load templates, using fallback: {ex.Message}");
40-
_mainTemplate = Template.Parse(GetFallbackTemplate());
41-
_partialTemplates = new Dictionary<string, Template>();
38+
throw new InvalidOperationException("Failed to initialize Scriban HTML formatter templates. Ensure template resources are properly embedded.", ex);
4239
}
4340
}
4441

@@ -72,7 +69,7 @@ public string Format(ComparisonResult result)
7269
context.PushGlobal(scriptObject);
7370

7471
// Set up template loader for includes
75-
context.TemplateLoader = new CustomTemplateLoader(_partialTemplates);
72+
context.TemplateLoader = new CustomTemplateLoader();
7673

7774
return _mainTemplate.Render(context);
7875
}
@@ -137,7 +134,7 @@ private object PrepareResultData(ComparisonResult result)
137134
modified_count = result.Summary.ModifiedCount,
138135
breaking_changes_count = result.Summary.BreakingChangesCount
139136
},
140-
configuration = PrepareConfigurationData(result.Configuration),
137+
configuration = PrepareConfigData(result.Configuration),
141138
breaking_changes = PrepareBreakingChangesData(result.Differences.Where(d => d.IsBreakingChange))
142139
};
143140
}
@@ -251,46 +248,6 @@ private object[] PrepareBreakingChangesData(IEnumerable<ApiDifference> breakingC
251248
}).ToArray();
252249
}
253250

254-
private object PrepareConfigurationData(DotNetApiDiff.Models.Configuration.ComparisonConfiguration config)
255-
{
256-
return new
257-
{
258-
filters = new
259-
{
260-
include_internals = config.Filters.IncludeInternals,
261-
include_compiler_generated = config.Filters.IncludeCompilerGenerated,
262-
include_namespaces = config.Filters.IncludeNamespaces,
263-
exclude_namespaces = config.Filters.ExcludeNamespaces,
264-
include_types = config.Filters.IncludeTypes,
265-
exclude_types = config.Filters.ExcludeTypes
266-
},
267-
mappings = new
268-
{
269-
auto_map_same_name_types = config.Mappings.AutoMapSameNameTypes,
270-
ignore_case = config.Mappings.IgnoreCase,
271-
type_mappings = config.Mappings.TypeMappings.Select(kvp => new { key = kvp.Key, value = kvp.Value }),
272-
namespace_mappings = config.Mappings.NamespaceMappings.Select(kvp => new { key = kvp.Key, value = kvp.Value })
273-
},
274-
exclusions = new
275-
{
276-
excluded_types = config.Exclusions.ExcludedTypes,
277-
excluded_members = config.Exclusions.ExcludedMembers,
278-
excluded_type_patterns = config.Exclusions.ExcludedTypePatterns,
279-
excluded_member_patterns = config.Exclusions.ExcludedMemberPatterns
280-
},
281-
breaking_change_rules = new
282-
{
283-
treat_type_removal_as_breaking = config.BreakingChangeRules.TreatTypeRemovalAsBreaking,
284-
treat_member_removal_as_breaking = config.BreakingChangeRules.TreatMemberRemovalAsBreaking,
285-
treat_signature_change_as_breaking = config.BreakingChangeRules.TreatSignatureChangeAsBreaking,
286-
treat_reduced_accessibility_as_breaking = config.BreakingChangeRules.TreatReducedAccessibilityAsBreaking
287-
},
288-
output_format = config.OutputFormat,
289-
fail_on_breaking_changes = config.FailOnBreakingChanges,
290-
output_path = config.OutputPath
291-
};
292-
}
293-
294251
private string RenderPartial(string templateName, object data)
295252
{
296253
if (_partialTemplates.TryGetValue(templateName, out var template))
@@ -313,8 +270,9 @@ private string GetCssStyles()
313270
{
314271
return EmbeddedTemplateLoader.LoadStyles();
315272
}
316-
catch
273+
catch (Exception ex)
317274
{
275+
System.Diagnostics.Debug.WriteLine($"Warning: Could not load CSS styles, using fallback: {ex.Message}");
318276
return GetFallbackStyles();
319277
}
320278
}
@@ -325,8 +283,9 @@ private string GetJavaScriptCode()
325283
{
326284
return EmbeddedTemplateLoader.LoadScripts();
327285
}
328-
catch
286+
catch (Exception ex)
329287
{
288+
System.Diagnostics.Debug.WriteLine($"Warning: Could not load JavaScript, using fallback: {ex.Message}");
330289
return GetFallbackJavaScript();
331290
}
332291
}

0 commit comments

Comments
 (0)