Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception handling in Type::getDocument() #693

Merged
merged 3 commits into from
Oct 8, 2014

Conversation

rmruano
Copy link
Contributor

@rmruano rmruano commented Oct 5, 2014

Requesting existing documents without providing a routing parameter (for types with parent docs) rise a NotFoundException which is confusing and makes debugging a lot harder.

@rmruano
Copy link
Contributor Author

rmruano commented Oct 5, 2014

Check issue #687

@rmruano rmruano changed the title #687 Remove exception catch in Type::getDocument() Remove exception catch in Type::getDocument() Oct 5, 2014
$response = $this->request($path, Request::GET, array(), $options);
$result = $response->getData();
} catch (ResponseException $e) {
throw new NotFoundException('doc id ' . $id . ' not found');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC break, you should be passing the previous exception to the new one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, I've upgraded it to the other proposal I did which doesn't break BC, but IMHO It doesn't seem appropiate to throw a NotFoundException for ResponseExceptions.

@rmruano rmruano changed the title Remove exception catch in Type::getDocument() Improve exception handling in Type::getDocument() Oct 5, 2014
@coveralls
Copy link

Coverage Status

Coverage remained the same when pulling c3dc16e on rmruano:type-get-document into ffbfa5d on ruflin:master.

@ruflin
Copy link
Owner

ruflin commented Oct 5, 2014

@rmruano I see your point. Is there an option we can actually make the difference between the two exceptions? Means, in case we get a response exception and it is a notfound, we throw this one, but in your case throw a response or a more specific exception. And if none both hits, we throw the general response exception.

@rmruano
Copy link
Contributor Author

rmruano commented Oct 5, 2014

@ruflin The thing is that ES returns a 404 response code for not founds (not sure about older versions) and it doesn't return an error response, so no ResponseException is triggered and no NotFound should be ever thrown in that case. If you don't want to break BC you can merge this PR as is right now, at least the exception includes now a better message and the original exception, but I still think the ResponseException should not be catched at all (like all other methods at the Type class do).

Having said that, what do you think about creating a BadRequestException extending (or not) ResponseException?, AbstractTransport::exec() methods could be updated to consider this case:

public function exec(Request $request, array $params)
{
    ....

    $transferInfo = curl_getinfo($conn);
    $response->setTransferInfo($transferInfo);

    if ($response->hasError()) {
        switch($transferInfo['http_code']) {
            case "400":
                throw new BadRequestException($request, $response);
                break;
            default:
                throw new ResponseException($request, $response);
                break;
        }
    }

    ...

    return $response;
}

And Type::getDocument() could detect it:

public function getDocument($id, $options = array())
{
    $path = urlencode($id);

    try {
        $response = $this->request($path, Request::GET, array(), $options);
        $result = $response->getData();
    } catch (ResponseException $e) {
        if ($e instanceof BadRequestException) {
            throw $e;
        } else {
            throw new NotFoundException('unable to retrieve doc id ' . $id. ': '.$e->getMessage(), $e->getCode(), $e);
        }
    }

    $info = $response->getTransferInfo();
    if ($info['http_code'] !== 200) {
        throw new NotFoundException('doc id ' . $id . ' not found');
    }

    ...
}
  • In case BadRequestException doesn't extend ResponseException no modification should be needed.

Anyway, I would still remove the whole try / catch :)

@ruflin ruflin merged commit c3dc16e into ruflin:master Oct 8, 2014
@rmruano rmruano deleted the type-get-document branch October 15, 2014 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants