Skip to main content

Wrapper order changes observable behavior

What this concept is​

Decorators compose behavior but generally do not commute. Logging outside retry observes a logical operation; logging inside retry observes each attempt. Both can return the same value while producing different traces.

Why it matters here​

Reviewing only a final result misses duplicated calls, hidden retries and misleading monitoring. A bounded retry policy is part of the public contract.

Concrete example​

With a provider that raises TransientError once then returns a valid quote, logged(retry(adapter(provider),2)) records start, success. retry(logged(adapter(provider)),2) records start, error:TransientError, start, success. Both call the provider twice and return 1250. The number 2 means total attempts, not two retries after an initial attempt.

Common confusion / misconception​

Retrying every Exception can repeat permanent data errors or duplicate side effects. Logging is not automatically harmless: a failing log sink may replace an operation’s error. This lab uses a plain list whose append is assumed to succeed and excludes such sink failures from its contract.

How to use it​

Retry only the designated transient failure from a fake read-only provider, reset the budget per logical call, and re-raise the final exception unchanged. Conversion errors propagate immediately. Choose order at the composition root and test traces as well as values. No backoff, deadlines, concurrency or remote-effect guarantees are modeled here; adding them requires an expanded specification.

Check yourself​

If the provider returns the wrong currency, how many calls should either composition make, even with an attempt budget of five?

Mini drill or application​

Predict all-failure traces for both orders with two total attempts. Outer logging has start and one error; inner logging has two start/error pairs. Explain why copying this wrapper to a payment or message-send operation would require additional retry-safety evidence.

Read this only if stuck​

Use the existing reference map and composition source guide. The GoF book index supplies vocabulary; the lab’s contracts define the concrete behavior.