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

Show generic error messages when server returns no response #1055

Closed
wants to merge 2 commits into from

Conversation

mlambley
Copy link
Contributor

@mlambley mlambley commented Aug 6, 2020

Although I just noticed you might want me to target an earlier branch. 6.x perhaps? Let me know if you wish me to remake this PR.

@elasticmachine
Copy link
Collaborator

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?

Copy link
Contributor

@ezimuel ezimuel left a comment

Choose a reason for hiding this comment

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

I like the idea of using a default unknown message but I would like to refactor a little bit the PR. Please, have a look at the specific comments. Thanks!

//Provide a generic error message when the server returns no response
$exception = new BadRequest400Exception("Unknown {$statusCode} error from elasticsearch server", $statusCode);
} elseif ($statusCode === 403) {
$responseBody = "Unknown {$statusCode} error from elasticsearch server " . $exception->getMessage();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can move that part into a private function in order to convert $responseBody into a string, using the "Unknown message" as you proposed in case of empty (or null) value. I'm thinking to something like this:

private function convertBodyToString(
    $body, 
    int $statusCode, 
    ElasticsearchException $exception
) : string {
    if (empty($body)) {
        return sprintf(
            "Unknown %d error from Elasticsearch %s",
            $statusCode,
            $exception->getMessage()
        );
    }
    // if body is not string, we convert it so it can be used as Exception message
    if (!is_string($body)) {
        return json_encode($body);
    }
    return $body;
}

We should apply this in process4xxError and process5xxError as well.
@mlambley can you apply this refactor and send the PR against 7.x branch? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remade the PR for 7.x
Please see #1056

@mlambley
Copy link
Contributor Author

Closed in favour of #1056

@mlambley mlambley closed this Aug 20, 2020
@mlambley mlambley deleted the null-error-messages branch August 20, 2020 11: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.

3 participants