|
Posted by Yarco on 08/20/07 05:42
But other Exception may also be thrown in adding record process...
On Aug 16, 10:51 pm, burgermeiste...@gmail.com wrote:
> On Aug 16, 2:38 am, Yarco <yarc...@gmail.com> wrote:
>
>
>
> > When i am coding something, i always need to do some error control on
> > it.
> > For example:
>
> > <?php
>
> > function check()
> > {
> > // the following vars from somewhere
> > if (empty($username))
> > {
> > $error['username'] = 'empty user name';
> > return false;
> > }
> > if (//...)
> > {
> > }
> > return false;
>
> > }
>
> > try
> > {
> > // to add a record}
>
> > catch(Exception $e) // but failed, because you can't add record again
> > some column is uniq.
> > {
> > $error['db'] = $e->getMessage();
>
> > }
>
> > ?>
>
> > So $error will contain something like:
> > 'db' => "SQLSTATE[23000]: Integrity constraint violation: 1062
> > Duplicate entry '1-5' for key 'rate_guid_accountId'"
>
> > But i want to let user know they do not do such thing but now such
> > professional words.
> > How can i do it?
>
> A good approach I've used in the past is to have two arrays for error
> message; one for developers and one for users. Then you can do
> something more like this:
>
> try
> {
> // to add a record
>
> }
>
> catch(Exception $e) // but failed, because you can't add record again
> some column is uniq.
> {
> $dev_error['db'] = $e->getMessage();
> $user_error['db'] = "I'm sorry, that user name is already taken.
> Please try again.";
>
> }
>
> That way in your debugging output you can see exactly what went wrong
> and at the same time you can have a more friendly message for end
> users, and without giving away too many details.
Navigation:
[Reply to this message]
|