Writing good exception message

How often have you seen an exception Something went wrong. What could be the most useless and opaque exception message? It tells absolutely nothing and every time you see it you need to figure out by yourself what actually happened.

Every good exception message should consist of three parts:

  1. Firstly, log message should indicate what happened. If possible including the context of the exception (variables which led to this exception situations or were around this code at that time).

    Cannot read configuration file for DEV environment...

  2. Secondly, it should explain why the exception happened (or guess).

    File was missing or corrupted

  3. Lastly, it shows how to fix it

    Check that configuration file exists under config/dev folder

Don't hesitate to write long and detailed log messages. The more information you put into logs, the easier and faster it will be for you to understand what actually happened without looking into the code base. By doing so your debug tool will become Vim instead of IDE.

Get the latest posts delivered right to your inbox.