Controller Service Repository Error handling

I am currently working on a ASP.NET 8 MVC project where we decided to use the Controller-Service-Repository pattern.

At the moment there is not a lot of business logic yet in the MVC Service layer, so mostly it calls the repository and nothing else.

The repository calls an API. This is a SOAP API, so I cannot rely on HTTP errors there. I only get a “Code” where 0 is OK.

A lot of API methods have rather complex business logic, so it is a lot of work to implement the same logic in the MVC Service layer, if not impossible because we simple cannot get all the data from the API. So we rely on the API mostly for business logic errors.

When I believe there is an error in the API I throw a RepositoryException.

Then however I am uncertain what would be the best approach to send this to the client.

Should I handle repository exceptions in the Service layer with a try catch?

Should I then do the same thing to the Controller with the ServiceException (which does not exist yet) and send this to the View?

I could use some advice regarding this.

This is an example of how I am handling it at the moment.

The repository:

public class ContactPersonRepository : SoapRepository, IContactPersonRepository
{
    public ContactPersonRepository(ISoapService client, ISoapAuthentication authentication) : base(client, authentication)
    {
    }

    public async Task<ContactPerson> UpdateAsync(ContactPerson contactPerson)
    {
        var response = await client.UpdateContactPersonAsync(DeviceId, AuthToken, contactPerson);

        if (response.HasErrors())
        {
            throw new SoapRepositoryException(response);
        }

        return response.ContactPerson;
    }
}

The HasErrors() class is an extension method which simply filters out errors by Code. For example, Code 2 means that there are no results in the list. I don’t see this as a real error and ignore it.

The Extension method HasErrors():

public static class ServiceResponseExtensions
{
    public static bool HasErrors(this ServiceResponse response)
    {
        int[] errorCodeWhitelist = [0, 1, 2, 10];

        return !errorCodeWhitelist.Contains(response.Error);
    }
}

The Service:

public class ContactPersonService : IContactPersonService
{
private IContactPersonRepository contactPersonRepository;

public ContactPersonService(IContactPersonRepository contactPersonRepository)
{
    this.contactPersonRepository = contactPersonRepository;
}

public async Task<ContactPerson> UpdateAsync(ContactPerson contactPerson)
{
    return await contactPersonRepository.UpdateAsync(contactPerson);
}

}


The Controller:

public class ContactPersonController : BaseController
{
    private IContactPersonService contactPersonService;

    public ContactPersonController(IContactPersonService contactPersonService)
    {
        this.contactPersonService = contactPersonService;
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(ContactPersonEditModel model)
    {
        if (ModelState.IsValid)
        {
            ContactPerson toUpdate = model.MapTo<ContactPerson>();

            await contactPersonService.UpdateAsync(toUpdate);

            return RedirectToAction("Index");
        }

        return View(model);
    }
}

New contributor

Valition is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật