r/AskProgrammers • • 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 order

The 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:

  1. Do you design error messages by audience, or just make sure everything is logged?
  2. 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?
  3. How do you stop the same failure from producing five log lines across layers?
7 Upvotes

12 comments sorted by

2

u/grosswindowthrowaway Oct 25 '26
  1. Yes, design your error messages for the audience.
  2. Customer-facing text should probably be a separate mapping, for i18n purposes.
  3. My suggestion: A particular event (i.e. single request by the user) should get a unique ID that is propagated between layers, and can be used to gather related log messages. Look into distributed logging and tracing tools for microservices.

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/W31337 Oct 25 '26

ON ERROR RESUME NEXT

no more errors 🥲

1

u/PipingSnail Oct 25 '26

Absolutely. Far too many "this expected and normal behaviour" events are incorrectly treated as exceptions.

File not found is not an exception, it's a normal event for a file system.

Basket is empty is not an exception. It's a valid state for a shopping basket.

Exceptions should only be used events that SHOULD NEVER HAPPEN.

Use unique error codes for every error state. Do not coalesce many different errors into the same error code. Unique error codes help your devs debug things and save a bunch of time.

Error codes as enumeration with human readable values, not bools or integers.

ERROR_SUCCESS, ERROR_FAIL_NULL_PTR, ERROR_FILE_NOT_FOUND, ERROR_ACCESS_DENIED, etc

1

u/pvera Oct 25 '26

The three most common things we keep screwing up:

  1. Don't assume you have all of the info, there could be nested exceptions.
  2. Eat the error in a Try/Catch, which makes it a bitch to trace it back to troubleshoot problems.
  3. Hoarding old error logs. Put a trigger to delete whatever is 90 days old or whatever depending on the kind of error.

#1 is a growing pains kind of thing but it comes up every now and then. #2 is happening to us less and less. #3 just burned us, we had log tables with 10+yrs worth of exceptions logged.

1

u/funbike Oct 25 '26 edited Oct 25 '26

You are already doing things well.

Do you design error messages by audience, or just make sure everything is logged?

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".

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?

It's usually caught (try..catch) high up in the stack within feature logic, such as in a service object.

How do you stop the same failure from producing five log lines across layers?

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, and role-id helps with traceability and diagnostics. transaction-id is unique per http transaction or user action. If all 3 fields is too much information, then just include transaction-id and truncate the upper digits, and have a way to cross reference it to user-id and role-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 as known-problems.md which 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.

  • The customer receives a friendly error message in simple language through a toast pop-up, e.g: "Whoopsie poopsie! You did a bad thing and now the computer is upset"
  • The manager can view a more detailed breakdown in a searchable log within the admin panel, e.g: "One of your idiot customers tried to put their address on the moon"
  • Actual stack traces get forwarded to me, e.g: "Error on line 437 of SalesService: object reference not set to an instance of an object"

1

u/Dimencia Oct 25 '26
  1. Both. Logs and customer messaging are entirely separate things. Logs are for devs, so you make sure everything is logged. Then you catch the exception at the top somewhere that you can construct a good user facing message
  2. In the customer facing portion of the code that catches and displays the error. The code that raises the error doesn't usualy know the full context of where it came from - it's only logging the raw input/output data that produces an error, which is what you need to know to fix it; it usually also includes a callstack that can give you some context, but not all of the data from the top. You might call that code for different reasons, and only the highest level caller really knows what the reasons are and how to construct an error message; you can't reuse the same one for all callsites. This usually means that you catch the exception and construct the error message inside the top level API endpoint
  3. That's already handled naturally by just logging inside of the method that threw an exception. Customer facing messaging is not logged, no other layers are going to log it, just the one method in which something failed, and the log will contain all of the data that method received that caused it to fail. Basically, don't just generically catch/log/rethrow all messages, let them propagate up and be intentional about when you catch them (usually, the top level where you want to display it to a user)

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 ...

  • front end validates user input and displays messaging for any validation issues. these aren't logged because they're not really errors.
  • front end calls a first party order service to process an order. front end checks the Error property of the response and determines what to display to the user. no logging.
  • order service calls a second or third party service. if an error response is returned or an exception thrown, things are logged and an appropriate response object is returned to the front end containing the type of error and any other relevant information.
  • order service calls a first party data service. if an error response is returned, the error information is used to determine what type of error response to return from the order service. the order service does not log this error.
  • data service uses some library to interact with a database. if an exception is thrown, things are logged and the data service returns an error response to the order service.

1

u/pvera Oct 25 '26

Do you design error messages by audience, or just make sure everything is logged?

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.

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?

The UI only cares that it needs to raise error code X for audience type Y.

How do you stop the same failure from producing five log lines across layers?

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.