Improve validation and error handling in AnnotationBehavior

Updated `using` directives to include `DigitalData.Core.Exceptions` for enhanced exception handling. Updated the `[Obsolete]` attribute message to reflect PSPDFKit library deprecation. Renamed `cancellationToken` to `cancel` for consistency.

Added validation to ensure `PsPdfKitAnnotation` is only supported for the `LegacyWeb` receiver type. Introduced stricter checks for missing or invalid annotation data, throwing `BadRequestException` when necessary. Updated `await next` calls to use the renamed parameter.

These changes improve code clarity, enforce stricter validation, and enhance error handling.
This commit is contained in:
2026-06-10 15:17:25 +02:00
parent 9d20ba1987
commit d0a50f63db

View File

@@ -1,4 +1,5 @@
using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.Core.Exceptions;
using EnvelopeGenerator.Application.Common.Dto;
using EnvelopeGenerator.Application.DocReceiverElements.Commands;
using EnvelopeGenerator.Domain.Entities;
@@ -10,13 +11,13 @@ namespace EnvelopeGenerator.Application.DocReceiverElements.Behaviors;
/// Pipeline behavior that saves annotations.
/// Executes first in the signing process.
/// </summary>
[Obsolete("This notification is deprecated. Use Signature.Commands.SignCommand instead.")]
[Obsolete("The PSPDFKit library is deprecated.")]
public class AnnotationBehavior : IPipelineBehavior<SigningCommand, Unit>
{
private readonly IRepository<ElementAnnotation> _repo;
/// <summary>
///
/// Initializes a new instance of the <see cref="AnnotationBehavior"/> class.
/// </summary>
/// <param name="repository"></param>
public AnnotationBehavior(IRepository<ElementAnnotation> repository)
@@ -29,13 +30,21 @@ public class AnnotationBehavior : IPipelineBehavior<SigningCommand, Unit>
/// </summary>
/// <param name="request"></param>
/// <param name="next"></param>
/// <param name="cancellationToken"></param>
/// <param name="cancel"></param>
/// <returns></returns>
public async Task<Unit> Handle(SigningCommand request, RequestHandlerDelegate<Unit> next, CancellationToken cancellationToken)
public async Task<Unit> Handle(SigningCommand request, RequestHandlerDelegate<Unit> next, CancellationToken cancel)
{
if (request.PsPdfKitAnnotation is PsPdfKitAnnotation annot)
await _repo.CreateAsync(annot.Structured, cancellationToken);
if(request.ReceiverAppType != ReceiverAppType.LegacyWeb)
if(request.PsPdfKitAnnotation is null)
return await next(cancel);
else
throw new BadRequestException("PsPdfKit Annotation are only supported for the legacy web receiver type.");
return await next(cancellationToken);
if (request.PsPdfKitAnnotation is PsPdfKitAnnotation annot)
await _repo.CreateAsync(annot.Structured, cancel);
else
throw new BadRequestException("Annotation data is missing or invalid.");
return await next(cancel);
}
}