From 903e4678ed5602962bb50458e7a737543865c181 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 21 Aug 2025 11:03:41 +0200 Subject: [PATCH] refactor(Document-, EnvelopeController): Globale try-catch-Anweisungen entfernen --- .../Controllers/DocumentController.cs | 50 +++---- .../Controllers/EnvelopeController.cs | 130 +++++++----------- 2 files changed, 70 insertions(+), 110 deletions(-) diff --git a/EnvelopeGenerator.Web/Controllers/DocumentController.cs b/EnvelopeGenerator.Web/Controllers/DocumentController.cs index 52fb384c..edd06761 100644 --- a/EnvelopeGenerator.Web/Controllers/DocumentController.cs +++ b/EnvelopeGenerator.Web/Controllers/DocumentController.cs @@ -30,26 +30,18 @@ public class DocumentController : BaseController [NonAction] public async Task Get([FromRoute] string envelopeKey, [FromQuery] int index) { - try - { - // Validate Envelope Key and load envelope - envelopeService.EnsureValidEnvelopeKey(envelopeKey); - EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey); + // Validate Envelope Key and load envelope + envelopeService.EnsureValidEnvelopeKey(envelopeKey); + EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey); - // Load document info - var document = await envelopeService.GetDocument(index, envelopeKey); + // Load document info + var document = await envelopeService.GetDocument(index, envelopeKey); - // Load the document from disk - var bytes = await envelopeService.GetDocumentContents(document); + // Load the document from disk + var bytes = await envelopeService.GetDocumentContents(document); - // Return the document as bytes - return File(bytes, "application/octet-stream"); - } - catch(Exception ex) - { - _logger.LogError(ex, "{Message}", ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError); - } + // Return the document as bytes + return File(bytes, "application/octet-stream"); } [Authorize(Roles = ReceiverRole.FullyAuth)] @@ -57,25 +49,17 @@ public class DocumentController : BaseController [Obsolete("Use MediatR")] public async Task Open(string envelopeKey) { - try - { - var authSignature = this.GetAuthReceiverSignature(); + var authSignature = this.GetAuthReceiverSignature(); - if (authSignature != envelopeKey.GetReceiverSignature()) - return Forbid(); + if (authSignature != envelopeKey.GetReceiverSignature()) + return Forbid(); - // Validate Envelope Key and load envelope - envelopeService.EnsureValidEnvelopeKey(envelopeKey); - EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey); + // Validate Envelope Key and load envelope + envelopeService.EnsureValidEnvelopeKey(envelopeKey); + EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey); - actionService?.OpenEnvelope(response.Envelope, response.Receiver); + actionService?.OpenEnvelope(response.Envelope, response.Receiver); - return Ok(new object()); - } - catch(Exception ex) - { - _logger.LogError(ex, "{Message}", ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError); - } + return Ok(new object()); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs b/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs index 3d2870c6..e1de9784 100644 --- a/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs +++ b/EnvelopeGenerator.Web/Controllers/EnvelopeController.cs @@ -47,28 +47,20 @@ public class EnvelopeController : BaseController [Obsolete("Use MediatR")] public async Task Get([FromRoute] string envelopeKey) { - try - { - envelopeKey = _urlEncoder.Encode(envelopeKey); + envelopeKey = _urlEncoder.Encode(envelopeKey); - // Validate Envelope Key and load envelope - envelopeService.EnsureValidEnvelopeKey(envelopeKey); - - EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey); + // Validate Envelope Key and load envelope + envelopeService.EnsureValidEnvelopeKey(envelopeKey); - if (envelopeService.ReceiverAlreadySigned(response.Envelope, response.Receiver.Id) == true) - { - return Problem(statusCode: 403); - } + EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey); - _logger.LogInformation("Loaded envelope [{0}] for receiver [{1}]", response.Envelope.Id, response.Envelope.Id); - return Json(response); - } - catch (Exception e) + if (envelopeService.ReceiverAlreadySigned(response.Envelope, response.Receiver.Id) == true) { - _logger.LogError(e, "{Message}", e.Message); - return StatusCode(StatusCodes.Status500InternalServerError); + return Problem(statusCode: 403); } + + _logger.LogInformation("Loaded envelope [{0}] for receiver [{1}]", response.Envelope.Id, response.Envelope.Id); + return Json(response); } [Authorize(Roles = ReceiverRole.FullyAuth)] @@ -76,46 +68,38 @@ public class EnvelopeController : BaseController [Obsolete("Use MediatR")] public async Task Update(string envelopeKey, int index) { - try - { - envelopeKey = _urlEncoder.Encode(envelopeKey); + envelopeKey = _urlEncoder.Encode(envelopeKey); - var authSignature = this.GetAuthReceiverSignature(); + var authSignature = this.GetAuthReceiverSignature(); - if (authSignature != envelopeKey.GetReceiverSignature()) - return Unauthorized(); + if (authSignature != envelopeKey.GetReceiverSignature()) + return Unauthorized(); - // Validate Envelope Key and load envelope - envelopeService.EnsureValidEnvelopeKey(envelopeKey); - EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey); + // Validate Envelope Key and load envelope + envelopeService.EnsureValidEnvelopeKey(envelopeKey); + EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey); - // Again check if receiver has already signed - if (envelopeService.ReceiverAlreadySigned(response.Envelope, response.Receiver.Id) == true) - { - return Problem(statusCode: 403); - } + // Again check if receiver has already signed + if (envelopeService.ReceiverAlreadySigned(response.Envelope, response.Receiver.Id) == true) + { + return Problem(statusCode: 403); + } - var document = envelopeService.GetDocument(index, envelopeKey); + var document = envelopeService.GetDocument(index, envelopeKey); - string? annotationData = await envelopeService.EnsureValidAnnotationData(Request); + string? annotationData = await envelopeService.EnsureValidAnnotationData(Request); - envelopeService.InsertDocumentStatus(new Domain.Entities.DocumentStatus() - { - EnvelopeId = response.Envelope.Id, - ReceiverId = response.Receiver.Id, - Value = annotationData, - Status = Constants.DocumentStatus.Signed - }); + envelopeService.InsertDocumentStatus(new Domain.Entities.DocumentStatus() + { + EnvelopeId = response.Envelope.Id, + ReceiverId = response.Receiver.Id, + Value = annotationData, + Status = Constants.DocumentStatus.Signed + }); - var signResult = actionService?.SignEnvelope(response.Envelope, response.Receiver); + var signResult = actionService?.SignEnvelope(response.Envelope, response.Receiver); - return Ok(new object()); - } - catch (Exception e) - { - _logger.LogError(e, "{Message}", e.Message); - return StatusCode(StatusCodes.Status500InternalServerError); - } + return Ok(new object()); } [Authorize(Roles = ReceiverRole.FullyAuth)] @@ -123,39 +107,31 @@ public class EnvelopeController : BaseController [Obsolete("Use DigitalData.Core.Exceptions and .Middleware")] public async Task Reject([FromBody] string? reason = null) { - try + var signature = this.GetAuthReceiverSignature(); + var uuid = this.GetAuthEnvelopeUuid(); + var mail = this.GetAuthReceiverMail(); + if (uuid is null || signature is null || mail is null) { - var signature = this.GetAuthReceiverSignature(); - var uuid = this.GetAuthEnvelopeUuid(); - var mail = this.GetAuthReceiverMail(); - if(uuid is null || signature is null || mail is null) - { - _logger.LogEnvelopeError(uuid: uuid, signature: signature, - message: @$"Unauthorized POST request in api\envelope\reject. One of claims, Envelope, signature or mail ({mail}) is null."); - return Unauthorized(); - } + _logger.LogEnvelopeError(uuid: uuid, signature: signature, + message: @$"Unauthorized POST request in api\envelope\reject. One of claims, Envelope, signature or mail ({mail}) is null."); + return Unauthorized(); + } - var envRcvRes = await _envRcvService.ReadByUuidSignatureAsync(uuid: uuid, signature: signature); + var envRcvRes = await _envRcvService.ReadByUuidSignatureAsync(uuid: uuid, signature: signature); - if (envRcvRes.IsFailed) - { - _logger.LogNotice(envRcvRes.Notices); - return Unauthorized("you are not authirized"); - } - - return await _histService.RecordAsync(envRcvRes.Data.EnvelopeId, userReference: mail, EnvelopeStatus.DocumentRejected, comment: reason).ThenAsync( - Success: id => NoContent(), - Fail: IActionResult (mssg, ntc) => - { - _logger.LogEnvelopeError(uuid: uuid, signature: signature, message: "Unexpected error happend in api/envelope/reject"); - _logger.LogNotice(ntc); - return this.ViewInnerServiceError(); - }); - } - catch (Exception e) + if (envRcvRes.IsFailed) { - _logger.LogError(e, "{Message}", e.Message); - return StatusCode(StatusCodes.Status500InternalServerError); + _logger.LogNotice(envRcvRes.Notices); + return Unauthorized("you are not authirized"); } + + return await _histService.RecordAsync(envRcvRes.Data.EnvelopeId, userReference: mail, EnvelopeStatus.DocumentRejected, comment: reason).ThenAsync( + Success: id => NoContent(), + Fail: IActionResult (mssg, ntc) => + { + _logger.LogEnvelopeError(uuid: uuid, signature: signature, message: "Unexpected error happend in api/envelope/reject"); + _logger.LogNotice(ntc); + return this.ViewInnerServiceError(); + }); } } \ No newline at end of file