Graceful Communication between Services
Learn the significance of clear error messages, custom exceptions, and a unified error-handling framework with HTTP return codes.
We'll cover the following
Everyone at some point has experienced an issue with a website or application where an error message will pop up that means absolutely nothing to the end user. Sometimes, there’s no error message at all. Other times, it can be a complete stack trace dump that appears in the error message. Either of these scenarios is far from optimal because the impact on the user is detrimental and could potentially share information about the code base if messages aren’t adjusted.
Implementation of a common error-handling framework
The same can be true for messages tracked in logs. If no information is sent about a potential error or exception, finding out what happened and how to fix it becomes very challenging. It’s important not only to the usability of the system but the maintenance of it as well to have clear, meaningful messages meant for each type of audience.
A common way to manage exception messaging is through the development of exception types that derive from System.Exception
. For example, the NullReferenceException
, FileNotFoundException
, and InvalidOperationException
types are all derived from the base Exception
class. A common library of Exception
types could be made available to all development teams using a shared library, or each team could create and maintain those custom types.
In most cases, however, this still might not be the preferred approach to communicating with end users. Often, microservice developers will make use of custom HTTP return codes to denote specific results back to the user. Whether that return code is caught on the frontend and rendered to the user with a client-side library or caught on the backend and translated into a meaningful message, the onus is on the developer to ensure a human-friendly way of conveying an issue with the application.
Common results
Let’s revisit the BaseCommandController
file and jump back into the RaiseEvent
method. We’ve already seen that the following two error codes are being accounted for during the method’s execution.
Get hands-on with 1400+ tech skills courses.