Skip to content

Problem with the ErrorHandlers #4

Closed

Description

@marcaube

In one of my endpoints, I validate a date param with the following code:

public function index(Request $request)
{
    if ($date = $request->query->get('date')) {
        try {
            $date = new \DateTime($date);
        } catch (\Exception $e) {
            throw new Exception\BadRequest();
        }
    }

    // ...
}

If I make a request to /endpoint?date=foobar I get the following response:

{"code":0,"message":"Bad Request","details":[]}{"code":0,"message":"DateTime::__construct(): Failed to parse time string (foobar) at position 0 (f): The timezone could not be found in the database","details":[]}

There's the BadRequest exception I thrown followed by the one I'm supposed to have caught.

Am I doing it wrong?

Activity

  1. adammbalogh commented on Jan 7, 2015

    @adammbalogh
    Member

    What would you like to see as a response?

    The response you see is generated by the default exception handler. You throw a BadRequest exception and phprest generates a default wrapped response.

  2. marcaube commented on Jan 7, 2015

    @marcaube
    ContributorAuthor

    I only expected:

    {"code":0,"message":"Bad Request","details":[]}

    The api consumers don't need to know that I suck at parsing dates, and that's why I catch the exception and throw a generic one. When debug is false, these kind of error messages should not be returned to the users.

  3. adammbalogh commented on Jan 7, 2015

    @adammbalogh
    Member

    I've just seen you get an invalid json response.

    {"code":0,"message":"Bad Request","details":[]}{"code":0,"message":"DateTime::__construct(): Failed to parse time string (foobar) at position 0 (f): The timezone could not be found in the database","details":[]}

    Have you got exactly this response?

    And you're right when debug is false you should see that message. So there is a bug, I'll check it.

    Do you use the sample project?

  4. marcaube commented on Jan 7, 2015

    @marcaube
    ContributorAuthor

    Yeah I got exactly this response.

    I do not use the sample project, but it's pretty close. I use fractal instead of the serializer and generate api docs with swagger-php ... but that's about it.

    I can test with the sample project to see if the same issue exist.

  5. adammbalogh commented on Jan 7, 2015

    @adammbalogh
    Member

    I have to update the phprest\phprest dependency in the sample project.

    I am going to try to reproduce the error.

  6. marcaube commented on Jan 7, 2015

    @marcaube
    ContributorAuthor

    I just installed the sample project and tweaked the Camera->get() method to reproduce my problem

    /**
     * @Phprest\Route(method="GET", path="/camera")
     *
     * @Docs\Api(
     *      path="/camera",
     *      @Docs\Operation(
     *          method="GET", summary="Gets the camera",
     *          @Docs\Parameter(name="accept", required=false, type="string", paramType="header"),
     *          @Docs\Parameter(name="date", type="string", paramType="query"),
     *          @Docs\ResponseMessage(code=404, message="Not Found"),
     *          @Docs\ResponseMessage(code=200, message="Ok")
     *      )
     * )
     *
     * @param Request $request
     * @return Response\Ok
     * @throws Exception\BadRequest
     * @throws Exception\NotFound
     */
    public function get(Request $request)
    {
        if ($date = $request->query->get('date')) {
            try {
                $date = new \DateTime($date);
            } catch (\Exception $e) {
                throw new Exception\BadRequest();
            }
        }
    
        $repo = $this->serviceOrm()->getRepository('Api\Camera\Entity\Camera');
    
        if (is_null($camera = $repo->findOneBy([]))) {
            throw new Exception\NotFound();
        }
    
        return new Response\Ok($camera);
    }

    The response is still invalid JSON, but no mention of DateTime

    {"code":0,"message":"Server Error","details":[]}{"code":0,"message":"Server Error","details":[]}
  7. marcaube commented on Jan 7, 2015

    @marcaube
    ContributorAuthor

    It's the logger in the sample project that overwrites the DateTime error with 'Server Error'. If I disable the logger, I've got the exact same Response than in my project.

  8. adammbalogh commented on Jan 7, 2015

    @adammbalogh
    Member

    Could you test it please? It works for me now.

  9. marcaube commented on Jan 7, 2015

    @marcaube
    ContributorAuthor

    I updated and it works. What's up with the $config->setApiVersionHandler()? It was removed?

  10. adammbalogh commented on Jan 7, 2015

    @adammbalogh
    Member

    Yes it was. I've to find a better place for it.

  11. added a commit that references this issue on Apr 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions