zfcampus / zf-rest

BSD 3-Clause "New" or "Revised" License
32 stars 37 forks source link

Reporting complex errors #10

Closed mtymek closed 10 years ago

mtymek commented 10 years ago

I'm playing around with zf-rest and other modules related to Apigility (great work BTW!). I'm currently stuck when trying to report detailed errors from REST resource. I want to use InputFilter to validate an entity, then report back exactly which fields were filled incorrectly.

Take a look at my current code:

class TaskResource extends AbstractResourceListener
{
    public function create($data)
    {
        $filter = $this->inputFilter;
        $filter->setData((array)$data);
        if (!$filter->isValid()) {
            throw new \Exception("Invalid data!");            
        }
        $entity = new Task();
        $this->hydrator->hydrate($filter->getValues(), $entity);
        $this->repository->save($entity);
    }
}

Now, instead of throwing an exception when something is wrong, I'd like to somehow return all messages from input filter. Is there any standard way to do that? Or should I just "invent" my own format, like this:

    return array(
        'success' => false,
        'errors' => $filter->getMessages();
    );
weierophinney commented 10 years ago

@mtymek First, please ask questions on the mailing list, not the issue trackers.

Second, as of 2 weeks ago, we released 0.8.0 of Apigility, which includes and integrates a new module, zf-content-validation, which allows you to create input filters that you attach to specific services; if data passed to the service does not validate, it will return an API-Problem response with validation error messages.

Third, if you want to report errors yourself, there are a couple ways to do this.

@danizord Those docs are out-of-date, and from PhlyRestfully. Thanks for the reminder that I need to remove them!

danizord commented 10 years ago

@weierophinney Sorry, I got confused with the module names. Going to remove my comment.

mtymek commented 10 years ago

Allright, thanks for the tips!