Skip to content

Commit 9c1bd06

Browse files
committed
Added logging to responders; Allow errors to return empty failures, used for saying 'skip this responder branch'; Have json serializer return an empty object when null is returned
1 parent 41b6e2f commit 9c1bd06

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

src/HyperSharp.Responders/ResponderCompiler.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,12 @@ public ResponderDelegate<TContext, TOutput> CompileResponders<TContext, TOutput>
143143
IEnumerable<ResponderBuilder> rootResponders = _builders.Where(builder => builder.RequiredBy.Count == 0);
144144
if (!rootResponders.Any())
145145
{
146+
_logger.LogWarning("No responders were found, returning an empty responder.");
146147
return (context, cancellationToken) => Result.Success<TOutput>();
147148
}
148149

149150
// Compile the root responders
151+
_logger.LogInformation("Compiling {Count:N0} top-level responders.", rootResponders.Count());
150152
List<ResponderDelegate<TContext, TOutput>> rootRespondersDelegates = [];
151153
foreach (ResponderBuilder builder in rootResponders)
152154
{
@@ -160,13 +162,15 @@ public ResponderDelegate<TContext, TOutput> CompileResponders<TContext, TOutput>
160162
foreach (ResponderDelegate<TContext, TOutput> responder in rootRespondersDelegates)
161163
{
162164
Result<TOutput> result = responder(context, cancellationToken);
163-
if (!result.IsSuccess || result.HasValue)
165+
if (result.HasValue)
164166
{
167+
_logger.LogTrace("Returning result: {Result}", result);
165168
return result;
166169
}
167170
}
168171

169-
return Result.Success<TOutput>();
172+
_logger.LogTrace("None of the {Count:N0} top-level responders returned a value.", rootRespondersDelegates.Count);
173+
return Result.Failure<TOutput>();
170174
}
171175
catch (Exception error)
172176
{
@@ -246,10 +250,12 @@ public ValueTaskResponderDelegate<TContext, TOutput> CompileAsyncResponders<TCon
246250
IEnumerable<ResponderBuilder> rootResponders = _builders.Where(builder => builder.RequiredBy.Count == 0);
247251
if (!rootResponders.Any())
248252
{
253+
_logger.LogWarning("No responders were found, returning an empty responder.");
249254
return (context, cancellationToken) => ValueTask.FromResult(Result.Success<TOutput>());
250255
}
251256

252257
// Compile the root responders
258+
_logger.LogInformation("Compiling {Count:N0} top-level responders.", rootResponders.Count());
253259
List<ValueTaskResponderDelegate<TContext, TOutput>> rootRespondersDelegates = [];
254260
foreach (ResponderBuilder builder in rootResponders)
255261
{
@@ -263,12 +269,15 @@ public ValueTaskResponderDelegate<TContext, TOutput> CompileAsyncResponders<TCon
263269
foreach (ValueTaskResponderDelegate<TContext, TOutput> responder in rootRespondersDelegates)
264270
{
265271
Result<TOutput> result = await responder(context, cancellationToken);
266-
if (!result.IsSuccess || result.HasValue)
272+
if (result.HasValue)
267273
{
274+
_logger.LogTrace("Returning result: {Result}", result);
268275
return result;
269276
}
277+
270278
}
271279

280+
_logger.LogTrace("None of the {Count:N0} top-level responders returned a value.", rootRespondersDelegates.Count);
272281
return Result.Success<TOutput>();
273282
}
274283
catch (Exception error)

src/HyperSharp.Results/Result.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public Result()
4848
Status = ResultStatus.IsSuccess;
4949
}
5050

51+
internal Result(ResultStatus status)
52+
{
53+
Value = null;
54+
Errors = _emptyErrors;
55+
Status = status;
56+
}
57+
5158
internal Result(object? value)
5259
{
5360
Value = value;
@@ -94,6 +101,11 @@ internal Result(object? value, IEnumerable<Error> errors)
94101
/// <param name="value">The value of the result.</param>
95102
public static Result Success(object? value) => new(value);
96103

104+
/// <summary>
105+
/// Returns a failed result with no error.
106+
/// </summary>
107+
public static Result Failure() => new(ResultStatus.None);
108+
97109
/// <summary>
98110
/// Creates a failed result with an error.
99111
/// </summary>
@@ -128,6 +140,9 @@ internal Result(object? value, IEnumerable<Error> errors)
128140
/// <inheritdoc cref="Success(object?)"/>
129141
public static Result<T> Success<T>(T value) => new(value);
130142

143+
/// <inheritdoc cref="Failure()"/>
144+
public static Result<T> Failure<T>() => new(ResultStatus.None);
145+
131146
/// <inheritdoc cref="Failure(string)"/>
132147
public static Result<T> Failure<T>(string error) => new(new Error(error));
133148

src/HyperSharp.Results/ResultStatus.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public enum ResultStatus
1111
/// <summary>
1212
/// The result has failed and has no value.
1313
/// </summary>
14-
None = 0 << 0,
14+
None = 0,
1515

1616
/// <summary>
1717
/// The result has succeeded.
@@ -21,6 +21,6 @@ public enum ResultStatus
2121
/// <summary>
2222
/// The result does not contain a value.
2323
/// </summary>
24-
HasValue = 1 << 1,
24+
HasValue = 1 << 1
2525
}
2626
}

src/HyperSharp.Results/Result`1.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public Result()
4646
Status = ResultStatus.IsSuccess;
4747
}
4848

49+
internal Result(ResultStatus status)
50+
{
51+
Value = default;
52+
Errors = Result._emptyErrors;
53+
Status = status;
54+
}
55+
4956
internal Result(T? value)
5057
{
5158
Value = value;

src/HyperSharp/HyperConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public HyperConfiguration(IServiceCollection serviceDescriptors, HyperConfigurat
7878
{
7979
ArgumentNullException.ThrowIfNull(serviceDescriptors, nameof(serviceDescriptors));
8080
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
81-
IServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();
8281

82+
IServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();
8383
if (!Uri.TryCreate($"http://{builder.ListeningEndpoint}/", UriKind.Absolute, out Uri? host))
8484
{
8585
throw new ArgumentException("The listening endpoint is invalid.", nameof(builder));

src/HyperSharp/Protocol/HyperSerializers/JsonAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static ValueTask<bool> JsonAsync(HyperContext context, HyperStatus status
2525

2626
// Write Content-Type header and beginning of Content-Length header
2727
context.Connection.StreamWriter.Write<byte>(_contentTypeJsonEncodingHeader);
28-
byte[] body = JsonSerializer.SerializeToUtf8Bytes(status.Body, context.Connection.Server.Configuration.JsonSerializerOptions);
28+
byte[] body = JsonSerializer.SerializeToUtf8Bytes(status.Body ?? new object(), context.Connection.Server.Configuration.JsonSerializerOptions);
2929

3030
// Finish the Content-Length header
3131
context.Connection.StreamWriter.Write<byte>(Encoding.ASCII.GetBytes(body.Length.ToString()));

0 commit comments

Comments
 (0)