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] [NonAction]
public async Task<IActionResult> Get([FromRoute] string envelopeKey, [FromQuery] int index) public async Task<IActionResult> Get([FromRoute] string envelopeKey, [FromQuery] int index)
{ {
try // Validate Envelope Key and load envelope
{ envelopeService.EnsureValidEnvelopeKey(envelopeKey);
// Validate Envelope Key and load envelope EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
envelopeService.EnsureValidEnvelopeKey(envelopeKey);
EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
// Load document info // Load document info
var document = await envelopeService.GetDocument(index, envelopeKey); var document = await envelopeService.GetDocument(index, envelopeKey);
// Load the document from disk // Load the document from disk
var bytes = await envelopeService.GetDocumentContents(document); var bytes = await envelopeService.GetDocumentContents(document);
// Return the document as bytes // Return the document as bytes
return File(bytes, "application/octet-stream"); return File(bytes, "application/octet-stream");
}
catch(Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
} }
[Authorize(Roles = ReceiverRole.FullyAuth)] [Authorize(Roles = ReceiverRole.FullyAuth)]
@ -57,25 +49,17 @@ public class DocumentController : BaseController
[Obsolete("Use MediatR")] [Obsolete("Use MediatR")]
public async Task<IActionResult> Open(string envelopeKey) public async Task<IActionResult> Open(string envelopeKey)
{ {
try var authSignature = this.GetAuthReceiverSignature();
{
var authSignature = this.GetAuthReceiverSignature();
if (authSignature != envelopeKey.GetReceiverSignature()) if (authSignature != envelopeKey.GetReceiverSignature())
return Forbid(); return Forbid();
// Validate Envelope Key and load envelope // Validate Envelope Key and load envelope
envelopeService.EnsureValidEnvelopeKey(envelopeKey); envelopeService.EnsureValidEnvelopeKey(envelopeKey);
EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey); EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
actionService?.OpenEnvelope(response.Envelope, response.Receiver); actionService?.OpenEnvelope(response.Envelope, response.Receiver);
return Ok(new object()); return Ok(new object());
}
catch(Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
} }
} }

View File

@ -47,28 +47,20 @@ public class EnvelopeController : BaseController
[Obsolete("Use MediatR")] [Obsolete("Use MediatR")]
public async Task<IActionResult> Get([FromRoute] string envelopeKey) 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); return Problem(statusCode: 403);
// 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);
} }
_logger.LogInformation("Loaded envelope [{0}] for receiver [{1}]", response.Envelope.Id, response.Envelope.Id);
return Json(response);
} }
[Authorize(Roles = ReceiverRole.FullyAuth)] [Authorize(Roles = ReceiverRole.FullyAuth)]
@ -76,46 +68,38 @@ public class EnvelopeController : BaseController
[Obsolete("Use MediatR")] [Obsolete("Use MediatR")]
public async Task<IActionResult> Update(string envelopeKey, int index) 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); return Problem(statusCode: 403);
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());
} }
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); EnvelopeId = response.Envelope.Id,
return StatusCode(StatusCodes.Status500InternalServerError); 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)] [Authorize(Roles = ReceiverRole.FullyAuth)]
@ -123,39 +107,31 @@ public class EnvelopeController : BaseController
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")] [Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
public async Task<IActionResult> Reject([FromBody] string? reason = null) 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(); _logger.LogEnvelopeError(uuid: uuid, signature: signature,
var uuid = this.GetAuthEnvelopeUuid(); message: @$"Unauthorized POST request in api\envelope\reject. One of claims, Envelope, signature or mail ({mail}) is null.");
var mail = this.GetAuthReceiverMail(); return Unauthorized();
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();
});
} }
catch (Exception e)
var envRcvRes = await _envRcvService.ReadByUuidSignatureAsync(uuid: uuid, signature: signature);
if (envRcvRes.IsFailed)
{ {
_logger.LogError(e, "{Message}", e.Message); _logger.LogNotice(envRcvRes.Notices);
return StatusCode(StatusCodes.Status500InternalServerError); 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();
});
} }
} }