Skip to content

Commit c82a4e7

Browse files
committed
fix: address PR review feedback from Copilot and CodeQL
Replace generic catch clauses with catch (Exception) in AppDiagnostics and NetworkRequestManager. Remove redundant null check on successCallback in NetworkRequestManager catch block. Guard nullable Data property in Post/Put/Patch requests with fallback to empty string. Add null checks after JsonSerializer.Deserialize in all JSON request types. Use Assume.That for non-deterministic GC tests in WeakReferenceCallbackTests. Remove unused detached variable and add explicit assertion in WeakReferenceEventListenerWithEventArgsTests. Move helper types into PlatformApiHelperTests as private nested classes. Restore solution file BOM and remove leading blank line. Update misleading catch comment in timer callback.
1 parent 3ded370 commit c82a4e7

11 files changed

Lines changed: 64 additions & 63 deletions

File tree

‎MADE.NET.sln‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
Microsoft Visual Studio Solution File, Format Version 12.00
32
# Visual Studio Version 17
43
VisualStudioVersion = 17.1.32228.430
54
MinimumVisualStudioVersion = 10.0.40219.1

‎src/MADE.Diagnostics/AppDiagnostics.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ await this.EventLogger.WriteCritical(
9494
this.ExceptionObserved?.Invoke(this, new ExceptionObservedEventArgs(correlationId, args.Exception));
9595
}
9696
}
97-
catch
97+
catch (Exception)
9898
{
9999
// Swallow exceptions in last-resort exception handlers to prevent crashing the process.
100100
}
@@ -121,7 +121,7 @@ await this.EventLogger.WriteCritical(
121121

122122
this.ExceptionObserved?.Invoke(this, new ExceptionObservedEventArgs(correlationId, ex));
123123
}
124-
catch
124+
catch (Exception)
125125
{
126126
// Swallow exceptions in last-resort exception handlers to prevent crashing the process.
127127
}

‎src/MADE.Networking/Http/NetworkRequestManager.cs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,7 @@ private static async Task ExecuteRequestsAsync(
236236
}
237237
catch (Exception ex)
238238
{
239-
if (successCallback is not null)
240-
{
241-
successCallback.Invoke(Activator.CreateInstance(successCallback.Type)!);
242-
}
239+
successCallback.Invoke(Activator.CreateInstance(successCallback.Type)!);
243240

244241
errorCallback?.Invoke(ex);
245242
}
@@ -251,9 +248,10 @@ private async void OnProcessTimerTick(object sender, object e)
251248
{
252249
await this.ProcessCurrentQueueAsync().ConfigureAwait(false);
253250
}
254-
catch
251+
catch (Exception)
255252
{
256-
// Swallow exceptions in timer callback to prevent unobserved task exceptions.
253+
// Swallow exceptions in the async void timer callback to prevent them from
254+
// escaping as unhandled exceptions and crashing the process.
257255
}
258256
}
259257
}

‎src/MADE.Networking/Http/Requests/Json/JsonDeleteNetworkRequest.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public JsonDeleteNetworkRequest(HttpClient client, string url, Dictionary<string
6666
public override async Task<TResponse> ExecuteAsync<TResponse>(CancellationToken cancellationToken = default)
6767
{
6868
string json = await this.GetJsonResponseAsync(cancellationToken).ConfigureAwait(false);
69-
return JsonSerializer.Deserialize<TResponse>(json, DefaultJsonOptions);
69+
return JsonSerializer.Deserialize<TResponse>(json, DefaultJsonOptions)
70+
?? throw new InvalidOperationException($"Failed to deserialize response to {typeof(TResponse).Name}.");
7071
}
7172

7273
/// <summary>
@@ -86,7 +87,8 @@ public override async Task<object> ExecuteAsync(
8687
CancellationToken cancellationToken = default)
8788
{
8889
string json = await this.GetJsonResponseAsync(cancellationToken).ConfigureAwait(false);
89-
return JsonSerializer.Deserialize(json, expectedResponse, DefaultJsonOptions);
90+
return JsonSerializer.Deserialize(json, expectedResponse, DefaultJsonOptions)
91+
?? throw new InvalidOperationException($"Failed to deserialize response to {expectedResponse.Name}.");
9092
}
9193

9294
private async Task<string> GetJsonResponseAsync(CancellationToken cancellationToken = default)

‎src/MADE.Networking/Http/Requests/Json/JsonGetNetworkRequest.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public JsonGetNetworkRequest(HttpClient client, string url, Dictionary<string, s
6666
public override async Task<TResponse> ExecuteAsync<TResponse>(CancellationToken cancellationToken = default)
6767
{
6868
string json = await this.GetJsonResponseAsync(cancellationToken).ConfigureAwait(false);
69-
return JsonSerializer.Deserialize<TResponse>(json, DefaultJsonOptions);
69+
return JsonSerializer.Deserialize<TResponse>(json, DefaultJsonOptions)
70+
?? throw new InvalidOperationException($"Failed to deserialize response to {typeof(TResponse).Name}.");
7071
}
7172

7273
/// <summary>
@@ -86,7 +87,8 @@ public override async Task<object> ExecuteAsync(
8687
CancellationToken cancellationToken = default)
8788
{
8889
string json = await this.GetJsonResponseAsync(cancellationToken).ConfigureAwait(false);
89-
return JsonSerializer.Deserialize(json, expectedResponse, DefaultJsonOptions);
90+
return JsonSerializer.Deserialize(json, expectedResponse, DefaultJsonOptions)
91+
?? throw new InvalidOperationException($"Failed to deserialize response to {expectedResponse.Name}.");
9092
}
9193

9294
private async Task<string> GetJsonResponseAsync(CancellationToken cancellationToken = default)

‎src/MADE.Networking/Http/Requests/Json/JsonPatchNetworkRequest.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public JsonPatchNetworkRequest(
9797
public override async Task<TResponse> ExecuteAsync<TResponse>(CancellationToken cancellationToken = default)
9898
{
9999
string json = await this.GetJsonResponseAsync(cancellationToken).ConfigureAwait(false);
100-
return JsonSerializer.Deserialize<TResponse>(json, DefaultJsonOptions);
100+
return JsonSerializer.Deserialize<TResponse>(json, DefaultJsonOptions)
101+
?? throw new InvalidOperationException($"Failed to deserialize response to {typeof(TResponse).Name}.");
101102
}
102103

103104
/// <summary>
@@ -117,7 +118,8 @@ public override async Task<object> ExecuteAsync(
117118
CancellationToken cancellationToken = default)
118119
{
119120
string json = await this.GetJsonResponseAsync(cancellationToken).ConfigureAwait(false);
120-
return JsonSerializer.Deserialize(json, expectedResponse, DefaultJsonOptions);
121+
return JsonSerializer.Deserialize(json, expectedResponse, DefaultJsonOptions)
122+
?? throw new InvalidOperationException($"Failed to deserialize response to {expectedResponse.Name}.");
121123
}
122124

123125
private async Task<string> GetJsonResponseAsync(CancellationToken cancellationToken = default)
@@ -139,7 +141,7 @@ private async Task<string> GetJsonResponseAsync(CancellationToken cancellationTo
139141
{
140142
Method = new HttpMethod("PATCH"),
141143
RequestUri = uri,
142-
Content = new StringContent(this.Data, Encoding.UTF8, "application/json"),
144+
Content = new StringContent(this.Data ?? string.Empty, Encoding.UTF8, "application/json"),
143145
};
144146

145147
if (this.Headers != null)

‎src/MADE.Networking/Http/Requests/Json/JsonPostNetworkRequest.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public JsonPostNetworkRequest(
9797
public override async Task<TResponse> ExecuteAsync<TResponse>(CancellationToken cancellationToken = default)
9898
{
9999
string json = await this.GetJsonResponseAsync(cancellationToken).ConfigureAwait(false);
100-
return JsonSerializer.Deserialize<TResponse>(json, DefaultJsonOptions);
100+
return JsonSerializer.Deserialize<TResponse>(json, DefaultJsonOptions)
101+
?? throw new InvalidOperationException($"Failed to deserialize response to {typeof(TResponse).Name}.");
101102
}
102103

103104
/// <summary>
@@ -117,7 +118,8 @@ public override async Task<object> ExecuteAsync(
117118
CancellationToken cancellationToken = default)
118119
{
119120
string json = await this.GetJsonResponseAsync(cancellationToken).ConfigureAwait(false);
120-
return JsonSerializer.Deserialize(json, expectedResponse, DefaultJsonOptions);
121+
return JsonSerializer.Deserialize(json, expectedResponse, DefaultJsonOptions)
122+
?? throw new InvalidOperationException($"Failed to deserialize response to {expectedResponse.Name}.");
121123
}
122124

123125
private async Task<string> GetJsonResponseAsync(CancellationToken cancellationToken = default)
@@ -137,7 +139,7 @@ private async Task<string> GetJsonResponseAsync(CancellationToken cancellationTo
137139

138140
using var request = new HttpRequestMessage(HttpMethod.Post, uri)
139141
{
140-
Content = new StringContent(this.Data, Encoding.UTF8, "application/json"),
142+
Content = new StringContent(this.Data ?? string.Empty, Encoding.UTF8, "application/json"),
141143
};
142144

143145
if (this.Headers != null)

‎src/MADE.Networking/Http/Requests/Json/JsonPutNetworkRequest.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public JsonPutNetworkRequest(HttpClient client, string url, string? jsonData, Di
9393
public override async Task<TResponse> ExecuteAsync<TResponse>(CancellationToken cancellationToken = default)
9494
{
9595
string json = await this.GetJsonResponseAsync(cancellationToken).ConfigureAwait(false);
96-
return JsonSerializer.Deserialize<TResponse>(json, DefaultJsonOptions);
96+
return JsonSerializer.Deserialize<TResponse>(json, DefaultJsonOptions)
97+
?? throw new InvalidOperationException($"Failed to deserialize response to {typeof(TResponse).Name}.");
9798
}
9899

99100
/// <summary>
@@ -113,7 +114,8 @@ public override async Task<object> ExecuteAsync(
113114
CancellationToken cancellationToken = default)
114115
{
115116
string json = await this.GetJsonResponseAsync(cancellationToken).ConfigureAwait(false);
116-
return JsonSerializer.Deserialize(json, expectedResponse, DefaultJsonOptions);
117+
return JsonSerializer.Deserialize(json, expectedResponse, DefaultJsonOptions)
118+
?? throw new InvalidOperationException($"Failed to deserialize response to {expectedResponse.Name}.");
117119
}
118120

119121
private async Task<string> GetJsonResponseAsync(CancellationToken cancellationToken = default)
@@ -133,7 +135,7 @@ private async Task<string> GetJsonResponseAsync(CancellationToken cancellationTo
133135

134136
using var request = new HttpRequestMessage(HttpMethod.Put, uri)
135137
{
136-
Content = new StringContent(this.Data, Encoding.UTF8, "application/json"),
138+
Content = new StringContent(this.Data ?? string.Empty, Encoding.UTF8, "application/json"),
137139
};
138140

139141
if (this.Headers != null)

‎tests/MADE.Foundation.Tests/Tests/PlatformApiHelperTests.cs‎

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -119,49 +119,49 @@ public void ShouldReturnFalseForUnsupportedTypeWithUnsupportedProperty()
119119
result.ShouldBeFalse();
120120
}
121121
}
122-
}
123-
124-
public class SupportedType
125-
{
126-
}
127122

128-
[PlatformNotSupported]
129-
public class UnsupportedType
130-
{
131-
public void SomeMethod()
123+
private class SupportedType
132124
{
133125
}
134-
}
135126

136-
[PlatformNotSupported]
137-
public class UnsupportedTypeWithUnsupportedMethod
138-
{
139127
[PlatformNotSupported]
140-
public void AlsoUnsupported()
128+
private class UnsupportedType
141129
{
130+
public void SomeMethod()
131+
{
132+
}
142133
}
143-
}
144134

145-
[PlatformNotSupported]
146-
public class UnsupportedTypeWithUnsupportedProp
147-
{
148135
[PlatformNotSupported]
149-
public string? AlsoUnsupported { get; set; }
150-
}
151-
152-
public class MixedSupportType
153-
{
154-
public string? SupportedProperty { get; set; }
136+
private class UnsupportedTypeWithUnsupportedMethod
137+
{
138+
[PlatformNotSupported]
139+
public void AlsoUnsupported()
140+
{
141+
}
142+
}
155143

156144
[PlatformNotSupported]
157-
public string? UnsupportedProperty { get; set; }
158-
159-
public void SupportedMethod()
145+
private class UnsupportedTypeWithUnsupportedProp
160146
{
147+
[PlatformNotSupported]
148+
public string? AlsoUnsupported { get; set; }
161149
}
162150

163-
[PlatformNotSupported]
164-
public void UnsupportedMethod()
151+
private class MixedSupportType
165152
{
153+
public string? SupportedProperty { get; set; }
154+
155+
[PlatformNotSupported]
156+
public string? UnsupportedProperty { get; set; }
157+
158+
public void SupportedMethod()
159+
{
160+
}
161+
162+
[PlatformNotSupported]
163+
public void UnsupportedMethod()
164+
{
165+
}
166166
}
167167
}

‎tests/MADE.Runtime.Tests/Tests/WeakReferenceCallbackTests.cs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ public void ShouldThrowWhenCallbackIsNoLongerAlive()
5959
GC.Collect();
6060

6161
// Act & Assert
62-
if (!callback.IsAlive)
63-
{
64-
Should.Throw<InvalidOperationException>(() => callback.Invoke("test"));
65-
}
62+
Assume.That(callback.IsAlive, Is.False, "Callback target is still alive; cannot assert dead-target behavior deterministically.");
63+
Should.Throw<InvalidOperationException>(() => callback.Invoke("test"));
6664
}
6765
}
6866

0 commit comments

Comments
 (0)