refactor(Document-, EnvelopeController): Globale try-catch-Anweisungen entfernen

This commit is contained in:
tekh 2025-08-21 11:03:41 +02:00
parent 730a318b56
commit 903e4678ed
2 changed files with 80 additions and 120 deletions

View File

@ -30,26 +30,18 @@ public class DocumentController : BaseController
[NonAction]
public async Task<IActionResult> 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<IActionResult> 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());
}
}

View File

@ -47,28 +47,20 @@ public class EnvelopeController : BaseController
[Obsolete("Use MediatR")]
public async Task<IActionResult> Get([FromRoute] string envelopeKey)
{
try
envelopeKey = _urlEncoder.Encode(envelopeKey);
// Validate Envelope Key and load envelope
envelopeService.EnsureValidEnvelopeKey(envelopeKey);
EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
if (envelopeService.ReceiverAlreadySigned(response.Envelope, response.Receiver.Id) == true)
{
envelopeKey = _urlEncoder.Encode(envelopeKey);
// Validate Envelope Key and load envelope
envelopeService.EnsureValidEnvelopeKey(envelopeKey);
EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
if (envelopeService.ReceiverAlreadySigned(response.Envelope, response.Receiver.Id) == true)
{
return Problem(statusCode: 403);
}
_logger.LogInformation("Loaded envelope [{0}] for receiver [{1}]", response.Envelope.Id, response.Envelope.Id);
return Json(response);
}
catch (Exception e)
{
_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<IActionResult> Update(string envelopeKey, int index)
{
try
envelopeKey = _urlEncoder.Encode(envelopeKey);
var authSignature = this.GetAuthReceiverSignature();
if (authSignature != envelopeKey.GetReceiverSignature())
return Unauthorized();
// 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)
{
envelopeKey = _urlEncoder.Encode(envelopeKey);
var authSignature = this.GetAuthReceiverSignature();
if (authSignature != envelopeKey.GetReceiverSignature())
return Unauthorized();
// 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);
}
var document = envelopeService.GetDocument(index, envelopeKey);
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
});
var signResult = actionService?.SignEnvelope(response.Envelope, response.Receiver);
return Ok(new object());
return Problem(statusCode: 403);
}
catch (Exception e)
var document = envelopeService.GetDocument(index, envelopeKey);
string? annotationData = await envelopeService.EnsureValidAnnotationData(Request);
envelopeService.InsertDocumentStatus(new Domain.Entities.DocumentStatus()
{
_logger.LogError(e, "{Message}", e.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
EnvelopeId = response.Envelope.Id,
ReceiverId = response.Receiver.Id,
Value = annotationData,
Status = Constants.DocumentStatus.Signed
});
var signResult = actionService?.SignEnvelope(response.Envelope, response.Receiver);
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<IActionResult> 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();
}
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();
});
_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();
}
catch (Exception e)
var envRcvRes = await _envRcvService.ReadByUuidSignatureAsync(uuid: uuid, signature: signature);
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();
});
}
}