Skip to content

Commit

Permalink
add: Global Error Response for clean error message response via JSON …
Browse files Browse the repository at this point in the history
…view
  • Loading branch information
nixhantb committed Mar 13, 2024
1 parent 25b5768 commit 71d566c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
23 changes: 19 additions & 4 deletions JobLeet.WebApi/JobLeet.Api/Controllers/BaseApiController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JobLeet.WebApi.JobLeet.Api.Logging;
using JobLeet.WebApi.JobLeet.Api.Exceptions;
using JobLeet.WebApi.JobLeet.Api.Logging;
using JobLeet.WebApi.JobLeet.Core.Interfaces;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -20,9 +21,23 @@ protected BaseApiController(TRepository repository, ILoggerManagerV1 logger)
[HttpGet]
public virtual async Task<IActionResult> GetAllAsync()
{
_logger.LogInfo("Triggering HTTP GET request");
var entities = await Repository.GetAllAsync();
return Ok(entities);
try
{
_logger.LogInfo("Triggering HTTP GET request");
var entities = await Repository.GetAllAsync();
return Ok(entities);
}
catch(Exception ex)
{
_logger.LogError($"An error occurred while fetching all entities: {ex.Message}");
var errorResponse = new GlobalErrorResponse
{
Error = "Internal Server Error",
Message = ex.Message
};
return StatusCode(500, errorResponse);
}

}

[HttpGet("{id}")]
Expand Down
8 changes: 8 additions & 0 deletions JobLeet.WebApi/JobLeet.Api/Exceptions/GlobalErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace JobLeet.WebApi.JobLeet.Api.Exceptions
{
public class GlobalErrorResponse
{
public string Error { get; set; }
public string Message { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ public EmailTypeRepository(BaseDBContext dbContext)
}
public async Task<EmailModel> GetByIdAsync(int id)
{
try
try
{
var email = await _dbContext.Emails
.Where(e => e.Id == id)
.Select(e => new EmailModel
{
var email = await _dbContext.Emails
.Where(e => e.Id == id)
.Select(e => new EmailModel
{
Id = e.Id,
EmailType = (EmailCategory)e.EmailType
})
.FirstOrDefaultAsync();
Id = e.Id,
EmailType = (EmailCategory)e.EmailType
})
.FirstOrDefaultAsync();

if (email == null)
throw new KeyNotFoundException($"Email with id {id} not found");
if (email == null)
throw new KeyNotFoundException($"Email with id {id} not found");

return email;
}
catch (Exception ex)
{
throw new Exception($"Error occurred while fetching email with id {id}: {ex.Message}");
}
return email;
}
catch (Exception ex)
{
throw new Exception($"Error occurred while fetching email with id {id}: {ex.Message}");
}
}

public async Task<List<EmailModel>> GetAllAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using JobLeet.WebApi.JobLeet.Api.Models.Common.V1;
using JobLeet.WebApi.JobLeet.Api.Exceptions;
using JobLeet.WebApi.JobLeet.Api.Models.Common.V1;
using JobLeet.WebApi.JobLeet.Core.Interfaces.Common.V1;
using JobLeet.WebApi.JobLeet.Infrastructure.Data.Contexts;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Data.Common;

namespace JobLeet.WebApi.JobLeet.Infrastructure.Repositories.Common.V1
{
Expand All @@ -12,6 +15,11 @@ public PersonNameRepository(BaseDBContext dbContext)
{
_dbContext = dbContext;
}

#region Retrieve Person Asynchronously
/// <returns>The list of person names.</returns>
/// <exception cref="Exception">Thrown when there is an error while fetching data from the database.</exception>
/// <remarks>This method fetches all person names from the database using Entity Framework Core.</remarks>
public async Task<List<PersonNameModel>> GetAllAsync()
{
try
Expand All @@ -26,11 +34,12 @@ public async Task<List<PersonNameModel>> GetAllAsync()
}).ToListAsync();
return result;
}
catch(DbUpdateException ex)
catch (Exception ex) when (ex is DbUpdateException || ex is DbException || ex is SqlException)
{
throw new Exception("Error while updating the database. Please try again later." + ex.Message);
throw new Exception("Error while fetching data from the database. Please try again later.");
}
}
#endregion

public Task<PersonNameModel> GetByIdAsync(int id)
{
Expand Down

0 comments on commit 71d566c

Please sign in to comment.