Skip to content

Commit d987f1b

Browse files
committed
Address CodeRabbit nitpick feedback (round 2)
- Make implicit operators on DomainName, GotenbergStatusCode, and CssSelector throw ArgumentNullException instead of silently returning null/0 — consistent with DDD validation philosophy - Add null implicit conversion test for GotenbergStatusCode - Add negative integration test verifying failOnResourceLoadingFailed triggers GotenbergApiException when resources fail to load
1 parent cc045fd commit d987f1b

5 files changed

Lines changed: 35 additions & 3 deletions

File tree

src/Gotenberg.Sharp.Api.Client/Domain/ValueObjects/CssSelector.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public static CssSelector Create(string selector)
4949

5050
public override int GetHashCode() => Value.GetHashCode();
5151

52-
public static implicit operator string(CssSelector selector) => selector?.Value!;
52+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="selector"/> is null.</exception>
53+
public static implicit operator string(CssSelector selector) =>
54+
selector?.Value ?? throw new ArgumentNullException(nameof(selector));
5355

5456
public static bool operator ==(CssSelector? left, CssSelector? right) => Equals(left, right);
5557

src/Gotenberg.Sharp.Api.Client/Domain/ValueObjects/DomainName.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public bool Equals(DomainName? other) => other is not null
5050

5151
public override int GetHashCode() => StringComparer.OrdinalIgnoreCase.GetHashCode(Value);
5252

53-
public static implicit operator string(DomainName domain) => domain?.Value!;
53+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="domain"/> is null.</exception>
54+
public static implicit operator string(DomainName domain) =>
55+
domain?.Value ?? throw new ArgumentNullException(nameof(domain));
5456

5557
public static bool operator ==(DomainName? left, DomainName? right) => Equals(left, right);
5658

src/Gotenberg.Sharp.Api.Client/Domain/ValueObjects/GotenbergStatusCode.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public static GotenbergStatusCode Create(int statusCode)
6363

6464
public int CompareTo(GotenbergStatusCode? other) => other is null ? 1 : Value.CompareTo(other.Value);
6565

66-
public static implicit operator int(GotenbergStatusCode code) => code?.Value ?? 0;
66+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="code"/> is null.</exception>
67+
public static implicit operator int(GotenbergStatusCode code) =>
68+
code?.Value ?? throw new ArgumentNullException(nameof(code));
6769

6870
public static bool operator ==(GotenbergStatusCode? left, GotenbergStatusCode? right) => Equals(left, right);
6971

test/GotenbergSharpClient.Tests/ChromiumMissingFieldsIntegrationTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Gotenberg.Sharp.API.Client.Domain.Builders;
22
using Gotenberg.Sharp.API.Client.Domain.Settings;
33
using Gotenberg.Sharp.API.Client.Extensions;
4+
using Gotenberg.Sharp.API.Client.Infrastructure;
45
using Microsoft.Extensions.DependencyInjection;
56

67
namespace GotenbergSharpClient.Tests;
@@ -81,6 +82,21 @@ public async Task HtmlToPdf_WithFailOnHttpStatusCodes_Succeeds()
8182
result.Length.Should().BeGreaterThan(0);
8283
}
8384

85+
[Category("Integration")]
86+
[Test]
87+
public async Task HtmlToPdf_WithFailOnResourceLoadingFailed_WhenResourceFails_Throws()
88+
{
89+
var builder = new HtmlRequestBuilder()
90+
.AddDocument(doc => doc.SetBody(
91+
"<html><body><img src='http://192.0.2.1/nonexistent.png'/></body></html>"))
92+
.SetConversionBehaviors(b => b
93+
.FailOnResourceLoadingFailed());
94+
95+
var act = async () => await _client.HtmlToPdfAsync(builder);
96+
97+
await act.Should().ThrowAsync<GotenbergApiException>();
98+
}
99+
84100
[Category("Integration")]
85101
[Test]
86102
public async Task HtmlToPdf_WithFailOnResourceLoadingFailed_Succeeds()

test/GotenbergSharpClient.Tests/ValueObjects/GotenbergStatusCodeTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public void ImplicitConversion_ToIntReturnsValue()
3838
result.Should().Be(404);
3939
}
4040

41+
[Test]
42+
public void ImplicitConversion_WithNull_Throws()
43+
{
44+
GotenbergStatusCode? nullCode = null;
45+
46+
var act = () => { int _ = nullCode!; };
47+
48+
act.Should().ThrowExactly<ArgumentNullException>();
49+
}
50+
4151
[Test]
4252
public void Equals_WithSameValue_ReturnsTrue()
4353
{

0 commit comments

Comments
 (0)