Error Handling, Retries and Timeouts
Most interview designs are written for the happy path: the car parks, the payment goes through, the seat is booked. Then your interviewer asks "what if the spot is already taken?", "what if the bank doesn't answer?" or "what if the client sends the same request twice?" How your classes answer those questions is a big part of what separates a mid-level design from a senior one.
Good error handling is not about wrapping everything in try. It is a small set of decisions you make on purpose:
where bad input is rejected, how a method tells its caller something went wrong, what the outside world sees, how
long you wait for a slow dependency, and when it is safe to try again. This post goes through each decision with
small Bad/Good examples from booking and payment problems.
💡 The core idea: separate the errors that are the caller's fault (bad input, a rule broken), the errors that are nobody's fault and may go away (a timeout, a busy server), and bugs. Each kind gets a different response, and most mistakes come from treating them the same.
Reject Bad Input at the Edge
The cheapest error to handle is one that can't happen. If invalid values never get into your objects, the code inside doesn't need to check for them. The idea is to fail fast: validate once, at the point where data enters, and refuse to build an object that breaks the rules.
Bad:
class Booking:
def __init__(self, room_id: str, start: int, end: int, guests: int) -> None:
self.room_id, self.start, self.end, self.guests = room_id, start, end, guests
def nights(self) -> int:
if self.end <= self.start:
return 0 # silently "fixes" a broken booking
return (self.end - self.start) // 1440
def price(self, rate: int) -> int:
if self.guests <= 0:
raise ValueError("no guests") # checked here but not in nights()
return self.nights() * rate
What the bad version costs you. The broken booking (end before start) was created without complaint and
travels through the system. Each method decides on its own whether to check, and they disagree: nights hides
the problem by returning zero, so a booking can be saved, confirmed and billed at $0. When it finally fails, the
error is far from the line that caused it.
Good:
@dataclass(frozen=True)
class Stay:
start: int # minutes since epoch
end: int
def __post_init__(self) -> None:
if self.end <= self.start:
raise ValueError("checkout must be after check-in")
def nights(self) -> int:
return (self.end - self.start) // 1440
@dataclass(frozen=True)
class Booking:
room_id: str
stay: Stay
guests: int
def __post_init__(self) -> None:
if self.guests < 1:
raise ValueError("at least one guest")
A Stay that exists is valid, so nights has nothing to check. The rule is written once, the error names the
exact problem, and it fires at the line that built the bad value. Frozen dataclasses also can't be changed into
an invalid state later. These small immutable classes are called value objects, and they are the most useful
error-handling tool you have in LLD; the anti-patterns post shows more of them
under primitive obsession.