Handle CurlException

Issue #19 resolved
Michael Johnson created an issue

When there is a problem on the connection itself (timeout, connection reset, partial download), an exception of Guzzle\Http\Exception\CurlException is thrown. We should catch that in the code and form an appropriate error to return to the client, possibly including the exception message, which will have more detail and might be of some use to the requester.

Currently, we just send back a blank 500 error, which should be sufficient, although not ideal.

Comments (4)

  1. Ratan Dhawtal

    Is the response really blank? I got a message last week that contained something like error 500 and internal server error (I think it was a timeout).

    We can catch the CurlException. That wouldn't be a problem, we can add a try block at the 'fetch' function. To get the message we can use $e->getMessage() and let it return to the controller. The only thing is that we must recognise the exception at the controller.

    Does the exception messages contain a specific message for all these messages (like error, exception or code) ?

  2. Michael Johnson reporter

    The response we send back depends on if you're in prod or dev environments. For prod, it's blank to prevent information leakage.

    We should catch the error at the call, not within the fetch. We'd have to handle the issue higher up and we might as well catch the exception at that point. The only time we should ever do string checks is when we have content indicting an error but no exception thrown.

    The CurlException can have a few different messages depending on the underlying problem that libcurl experienced, from a timeout to partial content. The code doesn't need to care about the specific problem, so just catch the CurlException and respond with an unknown-error JSON response containing the contents of the exception message.

  3. Ratan Dhawtal

    So if I am right you mean something like this.

    AnimeController.php (raw example)

    try{
        $animedetails = $downloader->fetch('/anime/' . $id);
    
        if (strpos($animedetails,'No series found') !== false){
            return $this->view(Array('error' => 'No series found, check the series id and try again.'), 404);
        }else{
                $anime = AnimeParser::parse($animedetails);
                return $anime;
        }
    } catch (\Guzzle\Http\Exception\CurlException $e) {
        return $this->view(Array('unknown-error' => $e->getMessage()), 500);
    }
    
  4. Michael Johnson reporter

    Catch CurlException

    Wrap our communicator calls in a try block so we can catch CurlException, which sometimes occurs under heavy load on the MAL side.

    This fixes #19

    Additionally, adjust the Try/Catch blocks to match the K&R indent style the rest of the code is tending to use.

    → <<cset 614863486f61>>

  5. Log in to comment