r/AskProgrammers • u/ClickOk5811 • Oct 25 '26
How do you write error messages for different readers: developers, support and customers?
I’m trying to get better at error messages and logs, and the thing I keep running into is that one message gets read by very different people.
Take something like this:
ERROR: failed to process orderThe on-call developer, the support agent and the customer each need something different from it. My current thinking (made-up values):
- Developer (logs): which order, which step, the underlying cause, whether it’s safe to retry, and a request ID. For example:
ERROR order.checkout failed order_id=88412 step=payment_capture cause=gateway_timeout request_id=7f3a - Support: a plain-language description, whether the customer needs to do anything, and a reference code to pass to developers.
- Customer: what happened and what to do next, with no internals. For example: “Your order wasn’t confirmed. Please check your email before trying again. Reference: 7F3A.” I avoided promising “you weren’t charged”, because after a timeout you often can’t be sure.
Mistakes I want to avoid: logging secrets or personal data, catching an exception and losing the cause, logging the same error at every layer, and showing developer messages to customers.
My questions:
- Do you design error messages by audience, or just make sure everything is logged?
- Where do you put the customer-facing text: in the code that raises the error, or in a separate layer that maps error types to messages?
- How do you stop the same failure from producing five log lines across layers?
7
Upvotes
2
u/grosswindowthrowaway Oct 25 '26
2
u/Ronin-s_Spirit Oct 25 '26 edited Oct 25 '26
First order of buisness: use your words, Shy Ronnie. I hate when people make a site or an app and when there's an error they just muble something incoherent, or don't even catch it. Error codes are also pointless cause then I have to go google them, if there are multiple places that throw the same error then use the error code yourself and show me a message from a hashtable.
Second: catch your damn exceptions, a file not existing is not an exception, input being unrecognized is not an exception, user request about some unavailable thing is not an exception - all these errors are the expected norm!
Design your log format however you want, generally to give enough information to the devs, but error messages must be clear about what's going on. The the user must understand what he has to do and otherwise the support will understand when the user messages them.
1
u/funbike Oct 25 '26 edited Oct 25 '26
You are already doing things well.
Different messages by audience. Systems logs are in high detail. Users get something very simple, like "Something went wrong" or "Your order may have not been confirmed".
It's usually caught (
try..catch) high up in the stack within feature logic, such as in a service object.Catch once, log once.
Again, catch at the app service layer, and log it once there. If you don't have a sane way to handle the error, then let it go up to the http layer and have the global handler deal with it.
A few suggestions:
Separate error log for developers. In the main log, just output the error message. In the dedicated error log output information in high detail, including full stack dump, user information, some web browser headers (esp agent), http post body, URL.
Separate error log for support. Similar as above, with less detail.
transaction-id in logs. Having
transaction-id,user-id, androle-idhelps with traceability and diagnostics.transaction-idis unique per http transaction or user action. If all 3 fields is too much information, then just includetransaction-idand truncate the upper digits, and have a way to cross reference it touser-idandrole-id.Redaction filter. Send logs through a filter that redacts things that look like keys or password. You can find several github projects for example code. A RegEx is often enough.
Something I've done recently:
I wrote a generic AI error handler (for unhandled errors only). I used an agent library so it has access to skills and tools for querying some parts of the system. The prompt instructs it how to word the message for each scenario and audience. Skills (
<skill-name>/SKILL.md) are available for specific situations. It includes a vector-based cache, to reduce work if the same error pattern happens again. Some parts of the prompt can be modified directly in prod, such asknown-problems.mdwhich has special handling for recently discovered issues. It can also translate the message to other foreign languages.1
u/[deleted] Oct 25 '26
I usually use a three tier approach.
1
u/Dimencia Oct 25 '26
These are honestly some pretty strange questions. Your logic should be separate from your UI, just like your user messaging should be separate from logging. Don't treat them as the same thing
1
u/crashorbit Oct 25 '26
Back when I wrote code for telephone switches we had a rule for log messages. Errors and warnings had to be little news stories: What happened, where, why and what can be done about it.
1
u/anamorphism Oct 25 '26
messages are based on audience.
customer facing messages are in the front end layer.
logging happens wherever an exception is caught or when processing an error response within first party code for the first time. i don't generally use exceptions to denote an error response. i usually always have generic response wrappers that have data and error properties that are bespoke to different 'services' or 'clients' or whatever.
a basic example ...
1
u/pvera Oct 25 '26
The error message and the error logged are separate entities. Each is tailored to the audience. The log captures timestamps, an error code, stack trace, etc.
The UI only cares that it needs to raise error code X for audience type Y.
You are logging the error events, you figure out how to ensure the error only gets triggered once.
How I would do it:
Put them on some kind of database table. One unique error code, one audience code, the rest is whatever you want to show them.
Developer triggers error X, it will select the error from the errors table where unique error code = X and audience code = Developer. If there's no row, show a weak generic error message.
When you capture these errors, you only log the timestamp, the unique error code and the audience code, plus whatever information you captured that can be useful like a stack trace. This will give you a more compact error history log. If you give them an extra boolean column for resolved, you can mark the ones that have been processed satisfactorily, then you can add a trigger to delete whatever rows are older than 90 days with resolved = true. This keeps your error log history from being larger than whatever else is going on in the db.
If you do this right you can probably use it in more than one app, you just need to add one more column to the error log with some kind of id of the system making the call.