13

How, when developing a medium sized project, do you identify, create and maintain error codes?

I for the life of me can't think of a simple and clean method of doing so. Some of my ideas convert class names and method name into an integer string, but that is way to long to display to the user on top of the fact that method names and class names may change (hopefully not!). Others are just using an incrementing log system (ie. when ever I create a new error message, just add 1 to the last error message id). But that is just completely unorganized.

To be more specific I am talking about error code such as:

Error 401 Unauthorized.

Robert Harvey
  • 200,592
ahodder
  • 801

5 Answers5

18

No.

Error codes are an anachronism, they stem from ye olden days when output was really hard and expensive, and the only way to signal an error condition may have been through a bunch of front panel lights: pdp11/70 front panel

These days, we have mature exception handling built into pretty much every mainstream language. Use it. Give the user information they can work with; don't bother them with technical blah-blah, but rather tell them roughly what went wrong and what they can do about it. For logging, just give your exceptions descriptive names, and log the name. Easier to remember, and also easier to find using grep or similar search tools.

The exception is, of course, when you're programming for situations where output is still hard and expensive, such as embedded systems or network protocols. HTTP still uses numeric response codes because they are extremely easy to parse efficiently - in some situations, reading just the first digit can tell you enough already, and you can discard the rest of the packet.

tdammers
  • 52,936
7

You should check out how error/status codes are organized in common protocols such as HTTP. They reserve distinct ranges for different types of statuses/errors. This makes it easier both for users to identify an unknown status code, and for developers to assign a code for a new kind of error which hasn't been handled before.

3

I'm going to assume a procedural context (C). If you have objects an error object is usually better, whether exception or not.

You should use error codes local to each module. For a library you can have a special header listing the error codes, with number 1, 2 etc (or -1, -2 if you prefer). Make sure to always return one of these codes, e.g. translate errno into your own codes. If you have multiple layers of modules, translate at each step (or predefine a range for the deeper error, e.g. values 1001 - 1050 is from that other module).

It is also important that you provide a means for translating the code into a string. You should never report only the code, that only leads to frustration. Actually pretty much any code in your application should come with a string translation function. For example libc typically has strerror and strsignal, but sadly lacks strwaitstatus.

3

Sorry, why using error codes at all?
Catch the exception, log it and offer to send a report if the program can't recover.

(Assuming your language supports exceptions.)

The only relevant information that might help you fix the bug is the stack trace which you don't get with an error code. (I'm also assuming you want to use error codes for error reports and not to throw them into a user's face.)

Dan
  • 2,902
-1

Here is my unique system for ErrorCodes: I actually thinks that ErrorCodes are good. You still need to have some text description for the error, but Error Code help you to find the place in the code itself that catches this error.

I use this method here https://breakpo.blogspot.com/2020/05/simple-system-to-track-errors-in-code.html

YYYYMMDD.XXXHHII

This give a full uniqueness for the Error Code number among the team. XXX is a unique number each one of the developers in the team have.

So an error code could be: 20190412.1001643 For programmer number 100, on the 12th of April 2019 at 4:43PM

This allows me to know how old is the code, and who to blame in no time :-)

aviv
  • 99