Question

Context: We have a page which is composed of multiple sections.
Consider for e.g: A product page with Product details, prices, images & offers.
All the sections are shown one after the other in the same order in which they are mentioned.

Our pseudo code to fetch all this data is as shown below:

public Page GetPageDetails()
{
    pageDto.Details = GetDetails();
    pageDto.Prices = GetPrices();
    pageDto.Images = GetImages();
    pageDto.Offers = GetOffers();

    private Details GetDetails()
    {
        var details = GetDetailsFromDb();
        //Perform some manipulations on "details" object
        return details;
    }

    private Details GetPrices()
    {
        var prices = GetPricesFromDb();
        //Perform some manipulations on "prices" object
        return prices;
    }

    and so on for other types...

    return pageDto;
}

Even though we have handled various types of errors possible while doing manipulations (null checks, index checks, etc...) we can't afford any type of exception here because if an exception occurs in lets say prices manipulation, it breaks entire page and results in direct monetary loss.

Ideally we would want that if an exception occurs in any section, only that section is not shown and page keeps functioning as usual.

Question:
What would be the right way to achieve our goal?

One of the suggestion by the team was to wrap every private function like GetDetails/GetPrices in try-catch and return null in case of error.

Was it helpful?

Solution

The suggestion of the team to catch the unexpected exceptions in the private functions that each retrieve a section of the page is a very good way to ensure that a failure in one section does not affect the displaying of the other sections.

As you are trying to hide these failures from the end-user and as they should be rare, be sure to add sufficient logging to the exception handler that you can reconstruct what happened without having to search for that once-in-a-lifetime set of conditions that triggered the failure.

Licensed under: CC-BY-SA with attribution
scroll top