Refactor error handling in controllers
Removed try-catch blocks from various controller methods to simplify error handling and allow exceptions to propagate naturally. Streamlined error logging and response handling using the `ThenAsync` method, enhancing code readability and reducing redundancy. Adjusted conditional checks for improved clarity.
This commit is contained in:
parent
972b258706
commit
5ce6c25393
@ -117,18 +117,10 @@ public partial class AuthController : ControllerBase
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpPost("logout")]
|
[HttpPost("logout")]
|
||||||
public async Task<IActionResult> Logout()
|
public async Task<IActionResult> Logout()
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "Unexpected error occurred.\n{ErrorMessage}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Prüft, ob der Benutzer ein autorisiertes Token hat.
|
/// Prüft, ob der Benutzer ein autorisiertes Token hat.
|
||||||
|
|||||||
@ -62,8 +62,6 @@ public class EmailTemplateController : ControllerBase
|
|||||||
/// <response code="404">Wenn die gesuchte Abfrage nicht gefunden wird.</response>
|
/// <response code="404">Wenn die gesuchte Abfrage nicht gefunden wird.</response>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> Get([FromQuery] ReadEmailTemplateQuery? emailTemplate = null)
|
public async Task<IActionResult> Get([FromQuery] ReadEmailTemplateQuery? emailTemplate = null)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (emailTemplate is null || (emailTemplate.Id is null && emailTemplate.Type is null))
|
if (emailTemplate is null || (emailTemplate.Id is null && emailTemplate.Type is null))
|
||||||
{
|
{
|
||||||
@ -76,12 +74,6 @@ public class EmailTemplateController : ControllerBase
|
|||||||
return temp is null ? NotFound() : Ok(temp);
|
return temp is null ? NotFound() : Ok(temp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates an temp template or resets it if no update command is provided.
|
/// Updates an temp template or resets it if no update command is provided.
|
||||||
@ -106,15 +98,13 @@ public class EmailTemplateController : ControllerBase
|
|||||||
/// <response code="404">Wenn die gesuchte Abfrage nicht gefunden wird.</response>
|
/// <response code="404">Wenn die gesuchte Abfrage nicht gefunden wird.</response>
|
||||||
[HttpPut]
|
[HttpPut]
|
||||||
public async Task<IActionResult> Update([FromQuery] EmailTemplateQuery? temp = null, [FromBody] UpdateEmailTemplateCommand? update = null)
|
public async Task<IActionResult> Update([FromQuery] EmailTemplateQuery? temp = null, [FromBody] UpdateEmailTemplateCommand? update = null)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (update is null)
|
if (update is null)
|
||||||
{
|
{
|
||||||
await _mediator.Send(new ResetEmailTemplateCommand(temp));
|
await _mediator.Send(new ResetEmailTemplateCommand(temp));
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
else if(temp is null)
|
else if (temp is null)
|
||||||
{
|
{
|
||||||
return BadRequest("No both id and type");
|
return BadRequest("No both id and type");
|
||||||
}
|
}
|
||||||
@ -124,16 +114,5 @@ public class EmailTemplateController : ControllerBase
|
|||||||
await _mediator.Send(update);
|
await _mediator.Send(update);
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
catch(NotFoundException)
|
|
||||||
{
|
|
||||||
return BadRequest();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "An unexpected error occurred. {message}", ex.Message);
|
|
||||||
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,17 +60,15 @@ public class EnvelopeController : ControllerBase
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> GetAsync([FromQuery] ReadEnvelopeQuery envelope)
|
public async Task<IActionResult> GetAsync([FromQuery] ReadEnvelopeQuery envelope)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (User.GetId() is int intId)
|
if (User.GetId() is int intId)
|
||||||
return await _envelopeService.ReadByUserAsync(intId, min_status: envelope.Status, max_status: envelope.Status).ThenAsync(
|
return await _envelopeService.ReadByUserAsync(intId, min_status: envelope.Status, max_status: envelope.Status).ThenAsync(
|
||||||
Success: envelopes =>
|
Success: envelopes =>
|
||||||
{
|
{
|
||||||
if(envelope.Id is int id)
|
if (envelope.Id is int id)
|
||||||
envelopes = envelopes.Where(e => e.Id == id);
|
envelopes = envelopes.Where(e => e.Id == id);
|
||||||
|
|
||||||
if(envelope.Status is int status)
|
if (envelope.Status is int status)
|
||||||
envelopes = envelopes.Where(e => e.Status == status);
|
envelopes = envelopes.Where(e => e.Status == status);
|
||||||
|
|
||||||
if (envelope.Uuid is string uuid)
|
if (envelope.Uuid is string uuid)
|
||||||
@ -89,12 +87,6 @@ public class EnvelopeController : ControllerBase
|
|||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ruft das Ergebnis eines Dokuments basierend auf der ID ab.
|
/// Ruft das Ergebnis eines Dokuments basierend auf der ID ab.
|
||||||
@ -107,8 +99,6 @@ public class EnvelopeController : ControllerBase
|
|||||||
/// <response code="500">Ein unerwarteter Fehler ist aufgetreten.</response>
|
/// <response code="500">Ein unerwarteter Fehler ist aufgetreten.</response>
|
||||||
[HttpGet("doc-result")]
|
[HttpGet("doc-result")]
|
||||||
public async Task<IActionResult> GetDocResultAsync([FromQuery] int id, [FromQuery] bool view = false)
|
public async Task<IActionResult> GetDocResultAsync([FromQuery] int id, [FromQuery] bool view = false)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (User.GetId() is int intId)
|
if (User.GetId() is int intId)
|
||||||
return await _envelopeService.ReadByUserAsync(intId).ThenAsync(
|
return await _envelopeService.ReadByUserAsync(intId).ThenAsync(
|
||||||
@ -142,12 +132,6 @@ public class EnvelopeController : ControllerBase
|
|||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
@ -158,8 +142,6 @@ public class EnvelopeController : ControllerBase
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> CreateAsync([FromQuery] CreateEnvelopeCommand envelope)
|
public async Task<IActionResult> CreateAsync([FromQuery] CreateEnvelopeCommand envelope)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
envelope.UserId = User.GetId();
|
envelope.UserId = User.GetId();
|
||||||
var res = await _mediator.Send(envelope);
|
var res = await _mediator.Send(envelope);
|
||||||
@ -172,10 +154,4 @@ public class EnvelopeController : ControllerBase
|
|||||||
else
|
else
|
||||||
return Ok(res);
|
return Ok(res);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -83,8 +83,6 @@ public class EnvelopeReceiverController : ControllerBase
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> GetEnvelopeReceiver([FromQuery] ReadEnvelopeReceiverQuery envelopeReceiver)
|
public async Task<IActionResult> GetEnvelopeReceiver([FromQuery] ReadEnvelopeReceiverQuery envelopeReceiver)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
var username = User.GetUsernameOrDefault();
|
var username = User.GetUsernameOrDefault();
|
||||||
|
|
||||||
@ -98,7 +96,7 @@ public class EnvelopeReceiverController : ControllerBase
|
|||||||
return await _erService.ReadByUsernameAsync(
|
return await _erService.ReadByUsernameAsync(
|
||||||
username: username,
|
username: username,
|
||||||
min_status: envelopeReceiver.Status?.Min,
|
min_status: envelopeReceiver.Status?.Min,
|
||||||
max_status:envelopeReceiver.Status?.Max,
|
max_status: envelopeReceiver.Status?.Max,
|
||||||
envelopeQuery: envelopeReceiver.Envelope,
|
envelopeQuery: envelopeReceiver.Envelope,
|
||||||
receiverQuery: envelopeReceiver.Receiver,
|
receiverQuery: envelopeReceiver.Receiver,
|
||||||
ignore_statuses: envelopeReceiver.Status?.Ignore ?? Array.Empty<int>())
|
ignore_statuses: envelopeReceiver.Status?.Ignore ?? Array.Empty<int>())
|
||||||
@ -110,12 +108,6 @@ public class EnvelopeReceiverController : ControllerBase
|
|||||||
return StatusCode(StatusCodes.Status500InternalServerError, msg);
|
return StatusCode(StatusCodes.Status500InternalServerError, msg);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "An unexpected error occurred. {message}", ex.Message);
|
|
||||||
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ruft den Namen des zuletzt verwendeten Empfängers basierend auf der angegebenen E-Mail-Adresse ab.
|
/// Ruft den Namen des zuletzt verwendeten Empfängers basierend auf der angegebenen E-Mail-Adresse ab.
|
||||||
@ -132,8 +124,6 @@ public class EnvelopeReceiverController : ControllerBase
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpGet("salute")]
|
[HttpGet("salute")]
|
||||||
public async Task<IActionResult> GetReceiverName([FromQuery] ReadReceiverNameQuery receiverName)
|
public async Task<IActionResult> GetReceiverName([FromQuery] ReadReceiverNameQuery receiverName)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
return await _erService.ReadLastUsedReceiverNameByMail(receiverName.EmailAddress).ThenAsync(
|
return await _erService.ReadLastUsedReceiverNameByMail(receiverName.EmailAddress).ThenAsync(
|
||||||
Success: res => res is null ? Ok(string.Empty) : Ok(res),
|
Success: res => res is null ? Ok(string.Empty) : Ok(res),
|
||||||
@ -146,12 +136,6 @@ public class EnvelopeReceiverController : ControllerBase
|
|||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Datenübertragungsobjekt mit Informationen zu Umschlägen, Empfängern und Unterschriften.
|
/// Datenübertragungsobjekt mit Informationen zu Umschlägen, Empfängern und Unterschriften.
|
||||||
@ -193,8 +177,6 @@ public class EnvelopeReceiverController : ControllerBase
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> CreateAsync([FromBody] CreateEnvelopeReceiverCommand request)
|
public async Task<IActionResult> CreateAsync([FromBody] CreateEnvelopeReceiverCommand request)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
CancellationToken cancel = default;
|
CancellationToken cancel = default;
|
||||||
int userId = User.GetId();
|
int userId = User.GetId();
|
||||||
@ -225,7 +207,7 @@ public class EnvelopeReceiverController : ControllerBase
|
|||||||
#region Add document
|
#region Add document
|
||||||
var document = await _documentExecutor.CreateDocumentAsync(request.Document.DataAsBase64, envelope.Uuid, cancel);
|
var document = await _documentExecutor.CreateDocumentAsync(request.Document.DataAsBase64, envelope.Uuid, cancel);
|
||||||
|
|
||||||
if(document is null)
|
if (document is null)
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError, "Document creation is failed.");
|
return StatusCode(StatusCodes.Status500InternalServerError, "Document creation is failed.");
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -244,8 +226,8 @@ public class EnvelopeReceiverController : ControllerBase
|
|||||||
|
|
||||||
SELECT @OUT_SUCCESS as [@OUT_SUCCESS];";
|
SELECT @OUT_SUCCESS as [@OUT_SUCCESS];";
|
||||||
|
|
||||||
foreach(var rcv in res.SentReceiver)
|
foreach (var rcv in res.SentReceiver)
|
||||||
foreach(var sign in request.Receivers.Where(r => r.EmailAddress == rcv.EmailAddress).FirstOrDefault()?.Signatures ?? Array.Empty<Signature>())
|
foreach (var sign in request.Receivers.Where(r => r.EmailAddress == rcv.EmailAddress).FirstOrDefault()?.Signatures ?? Array.Empty<Signature>())
|
||||||
{
|
{
|
||||||
using (SqlConnection conn = new(_cnnStr))
|
using (SqlConnection conn = new(_cnnStr))
|
||||||
{
|
{
|
||||||
@ -301,12 +283,6 @@ public class EnvelopeReceiverController : ControllerBase
|
|||||||
|
|
||||||
return Ok(res);
|
return Ok(res);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
|
|||||||
@ -20,8 +20,6 @@ public class EnvelopeTypeController : ControllerBase
|
|||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> GetAllAsync()
|
public async Task<IActionResult> GetAllAsync()
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
return await _service.ReadAllAsync().ThenAsync(
|
return await _service.ReadAllAsync().ThenAsync(
|
||||||
Success: Ok,
|
Success: Ok,
|
||||||
@ -31,10 +29,4 @@ public class EnvelopeTypeController : ControllerBase
|
|||||||
return ntc.HasFlag(Flag.NotFound) ? NotFound() : StatusCode(StatusCodes.Status500InternalServerError);
|
return ntc.HasFlag(Flag.NotFound) ? NotFound() : StatusCode(StatusCodes.Status500InternalServerError);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -41,9 +41,7 @@ public class ReceiverController : CRUDControllerBaseWithErrorHandling<IReceiverS
|
|||||||
if (receiver.Id is null && receiver.EmailAddress is null && receiver.Signature is null)
|
if (receiver.Id is null && receiver.EmailAddress is null && receiver.Signature is null)
|
||||||
return await base.GetAll();
|
return await base.GetAll();
|
||||||
|
|
||||||
try
|
if (receiver.Id is int id)
|
||||||
{
|
|
||||||
if(receiver.Id is int id)
|
|
||||||
return await _service.ReadByIdAsync(id).ThenAsync(
|
return await _service.ReadByIdAsync(id).ThenAsync(
|
||||||
Success: Ok,
|
Success: Ok,
|
||||||
Fail: IActionResult (msg, ntc) =>
|
Fail: IActionResult (msg, ntc) =>
|
||||||
@ -58,12 +56,6 @@ public class ReceiverController : CRUDControllerBaseWithErrorHandling<IReceiverS
|
|||||||
return NotFound();
|
return NotFound();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Message}", ex.Message);
|
|
||||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#region REMOVED ENDPOINTS
|
#region REMOVED ENDPOINTS
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user