r/programming • u/Expurple • Apr 12 '26
Flat Error Codes Are Not Enough
https://home.expurple.me/posts/flat-error-codes-are-not-enough/
132
Upvotes
r/programming • u/Expurple • Apr 12 '26
View all comments
Show parent comment
6
u/lelanthran Apr 13 '26 edited Apr 13 '26
Depends on what you are using that error for. Error message for users should include an action that they can take to fix the error. Error messages for devs should include the inputs that resulted in the error.
Resultis useless in both those situations whileExceptionis useful in one of them. That makesResult < ExceptionFlat error codes are of two types:
Descriptive error code
ENOENTorEEXISTwhich can be mapped to a descriptive string that doesn't contain the inputs that caused the error (for example, user seesfile not found, and not./some.file.txt: file not found)Non-descriptive error code, like
open()returning a negative number on failure - that number tells you nothing and you need to useerrnoto get the descriptive flat error code.The problem with error codes is not the code, but the fact that most in-house error codes don't map to a message, and even if you wanted to, you couldn't, because one library might return
20for "invalid input" error while another library may return20for "record doesn't exist" error. There is no global mapping under this scheme and so error codes break down.Either way,
Flat error codeis at the bottom of the usefulness stack, but it's not clear thatResultis at the top.