c# - Exceptions vs Error codes -


right have set of called 'services' - classes, have methods common signature: result type have property t? error, t enum. , have separate enum each method set of values defined specific method.

this works rather long final place use these services' methods controllers' actions — these errors returned client, handled javascript.

but sometime want compose methods of calls of other services' methods, , place seem have problem. let's have service's method a(), has error of type aerror. , a() method calls internally method b() has error of type berror.

first of all, have map possible berror aerror. , possible forget inspect b's error, , presence remain unobserved.

of cource know common use exceptions indicate method has failed. right controllers have filter intercepts unhandled exceptions , returns answer single property error value 'internalservererror'. if start use exceptions loose 1 feature consider important: possible set of method's errors explicitly specified in signature, , lost in case of use exceptions. know there tag in xml-documentation list exception types, documentation, not checked compiler.

also not understand how use exceptions on our code: let's have method first checks order's status. right returns 'invalidorderstatus' error if order's status invalid current action. if use exceptions, can create exception invalidorderstatusexception, how can know code call internally throws it?

we can create mnemonic rule: method should have error type aerror, , inside should throw generic exception (let's say, errorexception<>), parametrized aerror. , can intercept generic exception errorexception<aerror> in a's calls , observe error code. not checked compiler: method can throw other exception, or errorexception<>, parametrized other error code.

so question is: best way a) know kind of exceptions method can throw , kind of errors can return, , b) not able forget observe method's result error?

how exchanging enum aerror this:

class errorholder<t> // t aerror, berror {    t errorcode {get;}    object[] innererrors {get;}    // other payload go here inner exceptions etc. } 

thus have enumerated error codes can somehow checked plus can add whatever payload need.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -