Welcome Guest, Not a member yet? Register   Sign In
Add Exceptions to Config class
#1

[eluser]SerGz[/eluser]
Maybe, it's a bit silly, but does anybody thought about adding exceptions to Codeigniter Config Class(Library). Not sure, it is(will be) a good practice, but i suppose throwing an exception instead of just returning false is better.

For example, if we have something connected with user payments. Usually(?), we'll take some configurable data from codeigniter config files. Imagine, we've done a typical error specifying config index, and CI will return just false, which may cause some logic(!not syntax) error in this software.

One thing that i worry about is that to catch it, we'll have to write everywhere try-catch block, e.g.
Code:
try{
    $some_var = $this->config->item('smth_important);
}catch(Exception $e){
    // email about this, log exception or whatever you want
    die("An error occured".$e->getMessage());
}
and i don't know how to escape this Sad

So, do you think it'll be useful or it's just a dumb idea? Smile
#2

[eluser]danmontgomery[/eluser]
You'd rather rewrite the config library and any code that uses it to throw/catch exceptions than just put $this->config->item() in an if?
#3

[eluser]SerGz[/eluser]
noctrum,
1.First of all, i just asked an advice from codeigniter users.
2. If it's really good thought and if someone will use it, it'd be great if new version of CI will have this as an option(!).
Though, this will work well, only if we wouldn't write try-catch block, instead of IFs.(so, any thoughts how to avoid writing these blocks?)
3.As for me, some of my methods use a lot of calls to config items, and i don't image how ugly/not readable it would be with many additional IFs after each config call.
Thanks.
#4

[eluser]danmontgomery[/eluser]
You can extend the config library with your own alias for the item() function:

Code:
function try_item($item, $index = '') {
    $conf = $this->item($item, $index);
    if($conf == FALSE) {
        throw new Exception('Config Item Not Found.');
    }

    return $conf;
}
#5

[eluser]SerGz[/eluser]
noctrum, thanks.
But this is still require try-catch block while calling to config->my_item()
anyway, can it be done like this:

Code:
function try_item($item, $index=''){
    try{
        $conf = $this->_fetch_item($item, $index);
    }catch(Exception $e){
       die("erro: ".$e->getMessage());
    }
    return $conf;
}
function _fetch_item($item, $index=''){
    $conf = $this->item($item, $index);
    if($conf == FALSE) {
        throw new Exception('Config Item Not Found.');
    }
    return $conf;

}
//sorry, i can't test it right now - so, it's just a proposition
#6

[eluser]danmontgomery[/eluser]
Yes, but doing it that way you lose the ability to control the output on errors... You're going to just get a whitescreen with the "config item not found" text.
#7

[eluser]SerGz[/eluser]
noctrum, yes, but die() was just for example Smile
For real project, it will be probably right to do a redirect, show a message to user and break this action.
At the same time, method may log exception/sends notice to admin/or something else.

I've got one more question now - is it a right way to avoid errors/make application more stable? I think, this can be reached by simple(?) unit-testing(though unit-testing in MVC assumes testing only models, not controllers)(?)




Theme © iAndrew 2016 - Forum software by © MyBB