JennyAndersen / MovieCastHub

3 stars 3 forks source link

Implement Advanced Error Handling and Exception Throws #84

Open JennyAndersen opened 8 months ago

JennyAndersen commented 8 months ago

Apply advanced error handling techniques in the following projects:

Error Logging: Integrate with logging (e.g., Serilog) to capture detailed error information.

Implement Serilog logging using the _logger.Information method consistently across the entire project.

Apply logging in the following projects:

EXAMPLE: public class AnimalRepository : IAnimalRepository { private readonly AnimalDbContext _context; private readonly ILogger _logger;

public AnimalRepository(AnimalDbContext context)
{
    _context = context;
    _logger = Log.ForContext<AnimalRepository>();
}

public async Task<Animal> GetByIdAsync(Guid Animalid)
{
    try
    {
        _logger.Information($"Getting animal by ID {Animalid}...");
        var animal = await _context.Animals.FindAsync(Animalid) ?? throw new ArgumentNullException(nameof(Animal), "Cannot find null animal.");
        _logger.Information($"Animal with ID {animal.AnimalId} found.");
        return animal!;
    }
    catch (Exception ex)
    {
        _logger.Error(ex, $"An error occurred while getting animal by ID {Animalid}.");
        throw;
    }
}

Custom Exception Classes: Introduce custom exception classes to provide more context-specific information. User-Friendly Messages: Ensure user-friendly error messages where applicable. Error Codes: Consider introducing error codes for easier tracking and identification.