|
14 | 14 | namespace Smdn.Reflection.ReverseGenerating.ListApi; |
15 | 15 |
|
16 | 16 | public class ApiListWriter { |
| 17 | + private static readonly Action<ILogger, string?, Exception?> loggerMessageGeneratorErrorOnType = LoggerMessage.Define<string?>( |
| 18 | + LogLevel.Error, |
| 19 | + new(1, nameof(loggerMessageGeneratorErrorOnType)), |
| 20 | + "generator error on type '{TypeFullName}'" |
| 21 | + ); |
| 22 | + private static readonly Action<ILogger, string?, string, Exception?> loggerMessageGeneratorErrorOnMember = LoggerMessage.Define<string?, string>( |
| 23 | + LogLevel.Error, |
| 24 | + new(1, nameof(loggerMessageGeneratorErrorOnMember)), |
| 25 | + "generator error on member '{TypeFullName}.{MemberName}'" |
| 26 | + ); |
| 27 | + |
17 | 28 | public TextWriter BaseWriter { get; } |
18 | 29 |
|
19 | 30 | private readonly Assembly assembly; |
@@ -305,7 +316,14 @@ options.TypeDeclaration.NullabilityInfoContext is null && |
305 | 316 | ) |
306 | 317 | ); |
307 | 318 | } |
| 319 | + catch (MemberDeclarationException) { |
| 320 | + // just rethrow since the log has been output already |
| 321 | + throw; |
| 322 | + } |
308 | 323 | catch (Exception ex) { |
| 324 | + if (logger is not null) |
| 325 | + loggerMessageGeneratorErrorOnType(logger, type.FullName, ex); |
| 326 | + |
309 | 327 | throw new InvalidOperationException($"generator error on type '{type.FullName}'", ex); |
310 | 328 | } |
311 | 329 |
|
@@ -406,6 +424,12 @@ assemblyNameOfTypeForwardedFrom is not null && |
406 | 424 | return ret.ToString(); |
407 | 425 | } |
408 | 426 |
|
| 427 | + private sealed class MemberDeclarationException : Exception { |
| 428 | + public MemberDeclarationException(string? message, Exception? innerException) |
| 429 | + : base(message, innerException) |
| 430 | + { } |
| 431 | + } |
| 432 | + |
409 | 433 | private static string GenerateTypeContentDeclarations( |
410 | 434 | int nestLevel, |
411 | 435 | Assembly assm, |
@@ -468,7 +492,10 @@ private static string GenerateTypeContentDeclarations( |
468 | 492 | declaration = Generator.GenerateMemberDeclaration(member, referencingNamespaces, options); |
469 | 493 | } |
470 | 494 | catch (Exception ex) { |
471 | | - throw new InvalidOperationException($"generator error on member '{t.FullName}.{member.Name}'", ex); |
| 495 | + if (logger is not null) |
| 496 | + loggerMessageGeneratorErrorOnMember(logger, t.FullName, member.Name, ex); |
| 497 | + |
| 498 | + throw new MemberDeclarationException($"generator error on member '{t.FullName}.{member.Name}'", ex); |
472 | 499 | } |
473 | 500 |
|
474 | 501 | if (declaration == null) |
|
0 commit comments