views:

23

answers:

2

Hi, I'm new to Symfony and struggling to find an elegant solution for handling errors.

I know I can throw Exceptions and make my own Exception classes, but it's how to display the error to the user in the best possible fashion.

For example, I've got a Card class that charges credit and debit cards, there are a variety of different problems that can occur.

So should I make exceptions for the different problems, or use a generic CardException class, but where should I save the actual error messages, it's not good to hard code the error messages into the card class itself.

And then how to display them to the user.

I could do it but I get the feeling that it's going to be a messy solution and I'm looking for the correct, best practice, symfony way.

Any ideas?

Or should I be using the form framework, I am already for accepting the user's card details, but how to display an error related to a web service call and not a problem with the information they've submitted.

A: 

You can always use flash messages for errors that do not take part of validation.

$this->getUser()->setFlash('error', 'An error occurred while your transaction took place please try again later.');
mhitza
for that to work will I not need to redirect the user? if I do that it might be tricky to preserve the form values
McVeg
No you don't, you just have to have an area on the page where you show the flash. But generally is preferred to do a redirect after a post, unless it's an error.
mhitza
A: 

It only answers part of your question, (I'm just talking about Ajax errors) but what I do is to have a div somewhere on the page called 'error_area', and a Javscript function called displayAjaxError which adds the message to that div.

Colin Fine