Love your error conditions
Let's hammer out some code!
The above code looks fine and reads nicely - "if we don't have any app settings key called HttpBaseUrlSettingsKey, ... throw a configuration errors exception that explains what's wrong". In reality, however, what comes out, is an ArgumentNullException - AppSettings.Get returns null if it cannot find the requested key. This might not be so bad if you're consuming this code from the same solution. However, in a dependent solution, you get very sparse information about what went wrong:
Not quite as nice as the intended "Missing keys 'HttpBaseUrlSettingsKey' ... in appSettings element" message.
A correct implementation in this case could look like this:
However, the moral of this story is: Love your error conditions and test them. Don't just run down the happy path, leaving your carefully crafted checks behind!
Comments