Press enter or click to view image in full size
Some 3 years ago we had this constellation of PHP microservices we were building for a small bank and often caught ourselves not feeling very DRY writing the same exception handling code:
- see if this exception needs to be reported at all?
- reported how? Just forward to our ELK stack or also email someone?
- send along the stack trace or not? Only sometimes? With secret variables obfuscated?
- serialize to JSON before sending, ofc.
- set some extra parameters for the log analyser.
- do we redirect the client that got the exception? Show some message? In what language?
- is it a machine-to-machine exception that should return some explanatory JSON, like schema validation errors? How do we detect AJAX context?
So, of course, we did what any sane developer does when he catches himself duplicating code — we made a common library to install into all our microservices. Common libraries in microservices is a known very bad idea I won’t dwell on here.
The framework we were using all around, Laravel, enables you to register Handlers for exceptions so they may be intercepted and ‘handled’. Nifty. More experienced devs reading this probably already have their ‘GOTO considered harmful’ light bulb…