Skip to content

Commit

Permalink
Add better index-connection error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nonword committed Mar 18, 2024
1 parent e6c17dc commit a16cc26
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ nvm use; npm i
nvm use; ENV=qa npm start
```

Note that when developing locally, you may need to [add your IP to the access control policy of the relevant ES domain](https://github.com/NYPL/aws/blob/b5c0af0ec8357af9a645d8b47a5dbb0090966071/common/elasticsearch.md#2-make-the-domain-public-restrict-by-ip).
Note that when developing locally, if connecting to a IP ACL protected index (a practice we're currently deprecating), you may need to [add your IP to the access control policy of the relevant ES domain](https://github.com/NYPL/aws/blob/b5c0af0ec8357af9a645d8b47a5dbb0090966071/common/elasticsearch.md#2-make-the-domain-public-restrict-by-ip). If your IP has not been authorized, you will see errors such as the following in the application logs:

```
error: Error connecting to index: 403: {"Message":"User: anonymous is not authorized to perform: es:ESHttpPost because no resource-based policy allows the es:ESHttpPost action"}
```

### Using Docker

Expand Down
14 changes: 13 additions & 1 deletion lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ class NotFoundError extends Error {
this.message = message
}
}
module.exports = { InvalidParameterError, NotFoundError }

/**
* Thrown when there's an error connecting to the ES index
*/
class IndexConnectionError extends Error {
constructor (message) {
super()
this.name = 'IndexConnectionError'
this.message = message
}
}

module.exports = { InvalidParameterError, NotFoundError, IndexConnectionError }
9 changes: 9 additions & 0 deletions lib/es-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const elasticsearch = require('@elastic/elasticsearch')
const url = require('node:url')
const { deepValue } = require('./util')
const logger = require('./logger')
const { IndexConnectionError } = require('./errors')

const clientWrapper = {}

Expand Down Expand Up @@ -34,6 +35,14 @@ clientWrapper.search = function (body) {
index: process.env.RESOURCES_INDEX,
body
})
.catch((e) => {
if (e.statusCode === 403) {
logger.error(`Error connecting to index: ${e.statusCode}: ${JSON.stringify(e.body)}`)
throw new IndexConnectionError('Error connecting to index')
}

throw e
})
}.bind(clientWrapper)

/**
Expand Down

0 comments on commit a16cc26

Please sign in to comment.