question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

get method returns `error` object on document not found

See original GitHub issue

Rather 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:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
spalgercommented, Jan 14, 2016

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 or NotFound class:

var elasticsearch = require('elasticsearch');

var NotFoundError = elasticsearch.errors[404];
// or
var NotFoundError = elasticsearch.errors.NotFound;

The error objects all expose their status code as well, so you can also check for 404’s with that property:

client.get(...)
.catch(function (err) {
  if (err.status === 404) {
    // do something
  } else {
    throw err;
  }
});

Finally, if you don’t want 404’s to create errors you can pass ignore: [404] and 404 repsonses will be treated as successful!

client.get({
  index: ...
  type: ...
  id: ...
  ignore: 404
})
.then(function (resp) {
  if (!resp.found) {
    // ...
  } else {
    // ...
  }
})
0reactions
spalgercommented, Jan 16, 2016

Well, in an exceptional case like that here is what would happen:

  1. the host/es node that the request was directed at would be removed from the connection pool
  2. other nodes would be tried until:
    • a request succeeds (aka, a response was received, even 404 is considered a success at this point)
    • the connection pool is exhausted
    • the maxRetry count is reached
  3. if a request was successful
    1. the response is converted to either a user response or error based on the status code and response body
  4. if a request could not be completed
    1. the final error received is wrapped in a ConnectionFault error and passed back to the caller

All of this logic is implemented by the Transport class and more details can be found there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's the most appropriate HTTP status code for an "item ...
A 404 return code actually means 'resource not found', and applies to any entity for which a request was made but not satisfied....
Read more >
Error - JavaScript - MDN Web Docs - Mozilla
Returns a string representing the specified object. Overrides the Object.prototype.toString() method. Examples. Throwing a generic error.
Read more >
Handling the 'Object Does Not Exist' Error | TestComplete ...
The “Object Does Not Exist” message indicates that the tested application differs from the state it had during test recording or test creation....
Read more >
HTTP/1.1: Status Code Definitions
The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource....
Read more >
6 Deciphering Common R Errors | Getting Used to R, RStudio ...
6.1 Error: could not find function ... 6.2 Error: object not found. This error usually occurs when your R Markdown document refers to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found