phly / PhlyRestfully

ZF2 module for creating RESTful JSON APIs using HAL and API-Problem
108 stars 45 forks source link

Exception handling for all http methods (like update) #36

Closed normanbraun closed 11 years ago

normanbraun commented 11 years ago

Is there a reason that exception handling is only implemented for update, create, replaceList and patch methods?

    public function getList()
    {
        if (!$this->isMethodAllowedForCollection()) {
            return $this->createMethodNotAllowedResponse($this->collectionHttpOptions);
        }

        $events = $this->getEventManager();
        $events->trigger('getList.pre', $this, array());

        $collection = $this->resource->fetchAll();

        // exception handling...???

        if (!$collection instanceof HalCollection) {
            $collection = new HalCollection($collection);
        }
        $this->injectSelfLink($collection);
        $collection->setCollectionRoute($this->route);
        $collection->setResourceRoute($this->route);
        $collection->setPage($this->getRequest()->getQuery('page', 1));
        $collection->setPageSize($this->pageSize);
        $collection->setCollectionName($this->collectionName);

        $events->trigger('getList.post', $this, array('collection' => $collection));
        return $collection;
    }
macnibblet commented 11 years ago

I discussed this with matthew yesterday, the plan is were going to change all the the methods to listen on Exceptions, ApiProblem and ApiProblemInterface.

normanbraun commented 11 years ago

ah ok. sounds good. thank you for the information.

weierophinney commented 11 years ago

One note: in all cases of the methods that currently have exception handling, I have a documented exception type declared to allow listeners to raise specific exceptions that map 1:1 with specific API Problem and HTTP statuses. In the methods that do not have exception handling currently, there is no clear 1:1 mapping of error to HTTP status; we can really only have a 500 status in such cases.

As such, I'll simply catch the global \Exception class, and pass it to a new ApiProblem instance in order to return it.