Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm developing web site on self made MVC engine. I have actionController that handles operations like register, login, post submit and etc. actionController receives operation name and parameters. Of course it mast handle errors such user with same nick already exists or password is to short about which action handler have to notify user. The question is which is the best way to organize errors, such that View could easily get localized user notification message.

I see two ways

First one: define error constants like

ERR_NICK_BUSY = '1'
ERR_NICK_INVALID = '2'
...

and localization map

local[ERR_NICK_BUSY] = 'User with the same nick already registered'
local[ERR_NICK_INVALID ] = 'Nick, you entered is invalid'
...

And second one: define abstract constants like

ERR_FIELD_BUSY = '1'
ERR_FIELD_INVALID = '2'
...

and pass them with field name. In this case localization looks like

local['nick_'+ERR_FIELD_BUSY] = 'User with the same nick already registered'
...

I don't like both this methods. Can you advise something else?

share|improve this question

1 Answer 1

How about extending the Exception class and throwing your own exceptions.

You could have a yml file with the error code, type, default message.

try {

/* some code */

} catch(MyException $e) {

    echo $e->getError(2)['message'];    

}

class MyException extends Exception{


function getError($code){

/* the parsed errors from the yml file */

    return $this->error[$code];

}

}

The yml file should be something like this

600:
    type: 'Internal Error'
    message: 'An internal error occurred'
    code: 600

Cache the parsed yml if it gets to big

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.