c# - Localized error messages with parameters -



c# - Localized error messages with parameters -

scenario 1: imagine have complex check or calculation in business layer , calcuation fails because info wrong or missing. grab computation exception instance.

sceneraio 2: imagine query info layer (database) in business layer , grab missing record exception.

the task: in business layer, hanging in grab block yet unresolved exception. notify user there has been error because parameter x did not match parameter y , need localise error message (for simplicity have 2 languages).

the questions:

a) how propagate error presentation layer. b) how localise error , maintain parameters in message? e.g.: "dear user computation has failed because info x not match info y"

the proposed answers:

a) throw exception business layer having previous inner exception. b) this key reply looking for. let's assume can afford giving away parameter values because intranet application , user needs know parameters wrong.

the question application design - how "translate/localise exceptions" or showing user translated/localised error message when have point in code exception caught , there can multiple exceptions multiple messages.

the exception messages in unified language, assume english.

we in our application. approach take is:

catch no errors in service layer. exceptions allowed propagate presentation layer method made original call.

throw custom exceptions in info access layer. usually, when info access exception occurs when query database, database specific error thrown. grab exception in info access layer wrap in custom exception. have 1 called dataaccessexception has enumerated property indicates info access layer phone call made caused error , indicates if cause of error access related (insufficient privilege) or because database threw error.

catch errors in presentation layer. in presentation layer method called service layer, there try-catch block. there 1 catch clause each exception type expect thrown code in lower layers. in catch block, create localized message, log event log, if any, , display error user.

all localizable strings should resource strings. isn't hard , fast rule, using resource string approach means easy add together back upwards other languages windows supports in future. define resource identifier each string doesn't change. number of parameters each string takes doesn't change, either. changes wording , "{0}", "{1}", etc. parameters appear in string.

edit

i'm responding comment here because response longer can fit in 1 comment.

the localized message created based on exception i'm handling in particular catch block. seek create error message human readable. is, have assume reader ignorant of programming knowledge. describe went wrong in general terms; best not mention exact exception. example, message filenotfoundexception when seek open file should like, "the file named "{0}" not found. please create sure entered file name correctly." if there's in family isn't programmer, think how you'd explain problem them & you'll have message.

as resource key give it, utilize naming convention. our mvc web app, convention <controller><action><message name>. 1 time key defined, code uses key retrieve message requesting property same name key resourcemanager vs builds.

the resourcemanager class that's created vs build resource strings uses part settings determine local language message string retrieve. returned string, plus whatever parameters required, passed string.format , result displayed or logged.

we have rule in our code arguments message strings things don't need translated language. is, either user inputted strings, or numbers or dates ok, enumeration values not. that's because our web app displays audit messages generated , windows wpf application , doesn't know of enumerations in wpf app. since resourcemanager partial class, add together methods localize enumerations if wanted to. that's in control. phone call method localize enumerated value , pass string.format needed it.

c# asp.net exception-handling

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -