views:

344

answers:

2

I'm wondering what is the best way to handle error messages in your application.

Currently I'm working on a web application and all error messages are stored in the database and we get them by ID, negative is an error ,positive are success messages.

the problem with that is hard to maintain and keep tracking of text messages.

What are your approaches ?

+3  A: 

Generally speaking, I keep the text of the error messages in resource files. If you're using .NET 2.0 or higher (Visual Studio 2005 or higher), resource files are automatically compiled into strongly-typed classes, making the code which accesses said messages much more clear and readable.

(EDIT: Having seen another comment which mentions localization, I feel honor-bound to mention it here as well: .NET resource files do an excellent job of localization. You can get the localized text of the resource via the exact same code, with just a few minor tweaks to provide CultureInfo.)

That said, it sounds like a part of this problem domain is a message number. Are these errors being thrown from the database (say, as part of stored procs or triggers)? In that case, then database storage most likely is the right place for them, if only because that documents them most closely to where the "magic numbers" are being used.

John Rudy
+1  A: 

If you're going to localize them, I would use the English text as the key, and then perform a lookup into your storage of choice (an SQL-based database, file store, or what have you), and then return the properly localized string.

Should the requested key not exist in the store, you could yield a plain-text error, perhaps e-mailing a developer?

For more inspiration, see GNU gettext.

Mikael Jansson