Skip to content

Commit 5d5ce00

Browse files
authored
Better malformed launchsettings error (#5145)
* Improve error for malformed launch settings. * Improve exception detail.
1 parent 8cda5c9 commit 5d5ce00

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/Aspire.Hosting/LaunchProfileExtensions.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal static class LaunchProfileExtensions
2525
return null;
2626
}
2727

28-
return projectMetadata.GetLaunchSettings();
28+
return projectMetadata.GetLaunchSettings(projectResource.Name);
2929
}
3030

3131
internal static NamedLaunchProfile? GetEffectiveLaunchProfile(this ProjectResource projectResource, bool throwIfNotFound = false)
@@ -52,7 +52,7 @@ internal static class LaunchProfileExtensions
5252
return launchProfile is not null ? new (launchProfileName, launchProfile) : default;
5353
}
5454

55-
private static LaunchSettings? GetLaunchSettings(this IProjectMetadata projectMetadata)
55+
private static LaunchSettings? GetLaunchSettings(this IProjectMetadata projectMetadata, string resourceName)
5656
{
5757
// For testing
5858
if (projectMetadata.LaunchSettings is { } launchSettings)
@@ -80,8 +80,18 @@ internal static class LaunchProfileExtensions
8080
}
8181

8282
using var stream = File.OpenRead(launchSettingsFilePath);
83-
var settings = JsonSerializer.Deserialize(stream, LaunchSettingsSerializerContext.Default.LaunchSettings);
84-
return settings;
83+
84+
try
85+
{
86+
var settings = JsonSerializer.Deserialize(stream, LaunchSettingsSerializerContext.Default.LaunchSettings);
87+
return settings;
88+
}
89+
catch (JsonException ex)
90+
{
91+
var message = $"Failed to get effective launch profile for project resource '{resourceName}'. There is malformed JSON in the project's launch settings file at '{launchSettingsFilePath}'.";
92+
throw new DistributedApplicationException(message, ex);
93+
}
94+
8595
}
8696

8797
private static readonly LaunchProfileSelector[] s_launchProfileSelectors =

tests/Aspire.Hosting.Tests/ProjectResourceTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,47 @@ namespace Aspire.Hosting.Tests;
1414

1515
public class ProjectResourceTests
1616
{
17+
[Fact]
18+
public async Task AddProjectWithInvalidLaunchSettingsShouldThrowSpecificError()
19+
{
20+
var projectDetails = await PrepareProjectWithMalformedLaunchSettingsAsync();
21+
22+
var ex = Assert.Throws<DistributedApplicationException>(() =>
23+
{
24+
var appBuilder = CreateBuilder();
25+
appBuilder.AddProject("project", projectDetails.ProjectFilePath);
26+
});
27+
28+
var expectedMessage = $"Failed to get effective launch profile for project resource 'project'. There is malformed JSON in the project's launch settings file at '{projectDetails.LaunchSettingsFilePath}'.";
29+
Assert.Equal(expectedMessage, ex.Message);
30+
31+
async static Task<(string ProjectFilePath, string LaunchSettingsFilePath)> PrepareProjectWithMalformedLaunchSettingsAsync()
32+
{
33+
var csProjContent = """
34+
<Project Sdk="Microsoft.NET.Sdk.Web">
35+
<!-- Not a real project, just a stub for testing -->
36+
</Project>
37+
""";
38+
39+
var launchSettingsContent = """
40+
this { is } { mal formed! >
41+
""";
42+
43+
var projectDirectoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
44+
var projectFilePath = Path.Combine(projectDirectoryPath, "Project.csproj");
45+
var propertiesDirectoryPath = Path.Combine(projectDirectoryPath, "Properties");
46+
var launchSettingsFilePath = Path.Combine(propertiesDirectoryPath, "launchSettings.json");
47+
48+
Directory.CreateDirectory(projectDirectoryPath);
49+
await File.WriteAllTextAsync(projectFilePath, csProjContent);
50+
51+
Directory.CreateDirectory(propertiesDirectoryPath);
52+
await File.WriteAllTextAsync(launchSettingsFilePath, launchSettingsContent);
53+
54+
return (projectFilePath, launchSettingsFilePath);
55+
}
56+
}
57+
1758
[Fact]
1859
public async Task AddProjectAddsEnvironmentVariablesAndServiceMetadata()
1960
{

0 commit comments

Comments
 (0)