php - Convert API errors codes to exceptions -


i'm working on php interface api can return many (up 1 hundred) different error codes. interface supposed throw exception when such code encountered. php bit rusty, therefore i'm unsure suitable option present day php programmer:

  • have separate myapithisandthaterror class each error code?
  • or have generic class myapierror , provide api error code argument?

i'm open alternative suggestions.

tldr: use generic myapiexception class includes api error code.

ralph schindler wrote nice article on general topic @ http://ralphschindler.com/2010/09/15/exception-best-practices-in-php-5-3

if read whole thing, see section best practices in library code demonstrates (i've modified example context):

// usage of bracket syntax brevity namespace mycompany\component {      interface exception     {}      class myapiexception          extends \runtimeexception          implements exception     {}      class component     {         public static function dosomething()         {             $myapicall->dosomthing();             if ($myapicall->haserror) {                 throw new myapiexception($myapicall->getmessage(), $myapicall->geterrorcode);             }         }     } } 

assuming above code, if 1 execute mycompany\component\component::dosomething(), exception emitted dosomething() method can caught of following types: php’s exception, spl’s runtimeexception component’s mycompany\component\myapiexception, or component’s mycompany\component\exception. affords caller number of opportunities catch exception emanates given component within library, instigated api error returned. furthermore, analyzing types make exception, more semantic meaning can given exceptional situation occurred.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -