protected void checkResponse(HttpRequest request, HttpResponse response) {
        boolean hasError = false;

        try {
            StatusLine status = response.getStatusLine();
            int statusCode = status.getStatusCode();

            if (statusCode == 404) {
                hasError = true;
                throw new NotFoundException(status.getReasonPhrase());
            } else if ((statusCode < 200) || (statusCode > 299)) {
                hasError = true;
                StringBuilder msg = new StringBuilder();
                msg.append("statusCode=" + statusCode);
                msg.append("\n");
                msg.append("method=" + request.getRequestLine().getMethod());
                msg.append("\n");
                msg.append(request.getRequestLine().getUri());
                HttpEntity responseEntity = response.getEntity();

                try {
                    String responseBody = EntityUtils.toString(responseEntity);
                    msg.append("\n");
                    msg.append("responseBody=" + responseBody);
                } catch (Exception ignored) {
                    // ignored
                }

                throw new RuntimeException("unexpected response\n" + msg);
            }
        } finally {
            if (hasError) {
                close(response);
            }
        }
    }