get method returns `error` object on document not found
See original GitHub issueRather than reserving error
for internal, API and network errors, the get
API method triggers the error handling logic if the document with the specified ID is not found (due to a 404 status code being sent by Elasticsearch), despite the response actually containing some useful details.
This means you have to compare the error.message
with the string "Not Found"
, which makes error handling logic brittle and difficult to make generic.
(this is compounded by the fact that all errors share a default type, rather than the “not found” error being comparable as error instanceof NotFoundError
)
For now, I’m working around with issue by just using the Search endpoint with a match query, which I know is less than ideal for getting a specific document by id.
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
There is actually an error property exported by the elasticsearch module which includes all of the errors that the client sends back. In the case of 404 responses you can use either the
404
orNotFound
class:The error objects all expose their status code as well, so you can also check for 404’s with that property:
Finally, if you don’t want 404’s to create errors you can pass
ignore: [404]
and 404 repsonses will be treated as successful!Well, in an exceptional case like that here is what would happen:
ConnectionFault
error and passed back to the callerAll of this logic is implemented by the
Transport
class and more details can be found there.