From d0a50f63db1afd06801b79959ecdff46e0450beb Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 10 Jun 2026 15:17:25 +0200 Subject: [PATCH] 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. --- .../Behaviors/AnnotationBehavior.cs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/AnnotationBehavior.cs b/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/AnnotationBehavior.cs index 1f369f16..4d59ef4e 100644 --- a/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/AnnotationBehavior.cs +++ b/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/AnnotationBehavior.cs @@ -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. /// -[Obsolete("This notification is deprecated. Use Signature.Commands.SignCommand instead.")] +[Obsolete("The PSPDFKit library is deprecated.")] public class AnnotationBehavior : IPipelineBehavior { private readonly IRepository _repo; /// - /// + /// Initializes a new instance of the class. /// /// public AnnotationBehavior(IRepository repository) @@ -29,13 +30,21 @@ public class AnnotationBehavior : IPipelineBehavior /// /// /// - /// + /// /// - public async Task Handle(SigningCommand request, RequestHandlerDelegate next, CancellationToken cancellationToken) + public async Task Handle(SigningCommand request, RequestHandlerDelegate 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); } }