6

Looking around at examples on the web, I am guessing that getStatus() returns a zero for success, and that most failures will manifest as an exception, rather than a non-zero error code.

Is that true? Is it safe/correct to throw an error when getStatus() returns non-zero? What non-zero values might getStatus() return, and what would those values signify?

4

2 回答 2

3

我不确定,如果您在 solrj 中看到一个非零代码,因为org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpRequestBase, ResponseParser):491对于每个非 ok 状态代码,都会抛出一个 SolrException (sorlj 5.3.0) 。

可能的值是(根据this,Solr 1.x)在org.apache.solr.core.SolrCoreMethod 中设置postDecorateResponse(Solr 5.2.1,在此之前它曾经是 Method setResponseHeaderValues),它将500用于一般 Exceptions 或codeof SolrException(参见 Enum SolrException.ErrorCode) :

400 - BAD_REQUEST
401 - UNAUTHORIZED
403 - FORBIDDEN
404 - NOT_FOUND
409 - CONFLICT
415 - UNSUPPORTED_MEDIA_TYPE
500 - SERVER_ERROR
503 - SERVICE_UNAVAILABLE
510 - INVALID_STATE
  0 - UNKNOWN

我最终将每个响应传递给一个检查方法,这将引发一个异常:

private void checkResponse(SolrResponseBase response){
    if(response.getStatus() != 0){
        throw new RuntimeException(String.format("Solr-Response has error code %s",response.getStatus()));
    }
}
于 2016-12-13T06:45:48.153 回答
1

根据文章Indexing with SolrJ中 getStatus 的示例用法,我同意您可以假设当 getStatus() 返回非零时抛出和出错是安全/正确的。不幸的是,我无法找到任何指示可能从 getStatus() 返回的非零值的引用。

于 2012-10-23T01:29:56.540 回答