dynalite icon indicating copy to clipboard operation
dynalite copied to clipboard

Uncaught Error when closing during a write

Open reconbot opened this issue 4 years ago • 2 comments

If you shut down the server during a write you'll get an uncaught error. This comes up when using dynalite for testing. I'd expect it to swallow the error or emit it on the server object.

const dynalite = require('dynalite')
const AWS = require('aws-sdk')

/*
Start a write to dyanlite and then shut down the server
*/
async function run() {
  const server = dynalite({ createTableMs: 0 })
  server.on('error', (err) => console.log(`Server Error ${err}`))

  await new Promise(resolve => server.listen(4567, resolve))
  console.log('dynalite started')
  const dynamo = new AWS.DynamoDB({ endpoint: 'http://localhost:4567', region: 'US-EAST-1' })
  await dynamo.createTable({
    TableName: "Music",
    AttributeDefinitions: [
      { AttributeName: "Artist", AttributeType: "S" },
      { AttributeName: "SongTitle", AttributeType: "S" }
    ],
    KeySchema: [
      { AttributeName: "Artist", KeyType: "HASH" },
      { AttributeName: "SongTitle", KeyType: "RANGE" }],
    ProvisionedThroughput: {
      ReadCapacityUnits: 5,
      WriteCapacityUnits: 5
    },
  }).promise()
  console.log('table created')
  const writePromise = dynamo.putItem({
    TableName: "Music",
    Item: {
      "AlbumTitle": { S: "Somewhat Famous" },
      "Artist": { S: "No One You Know" },
      "SongTitle": { S: "Call Me Today" }
     },
  }).promise()
  server.on('connection', () => {
    console.log('closing server')
    server.close()
  })
  writePromise.then(() => console.log('write completed successfully'), () => console.log('write errored as expected'))
}

run().catch(err => console.log(`Run Error ${err}`))

This will error with

★  node dynalite-shutdown.js
dynalite started
table created
closing server

src/node_modules/dynalite/index.js:263
      if (err) throw err
               ^
Error [ReadError]: Database is not open
    at src/node_modules/levelup/lib/levelup.js:190:15
    at src/node_modules/encoding-down/index.js:75:21
    at processTicksAndRejections (internal/process/task_queues.js:81:21)

reconbot avatar Aug 19 '21 02:08 reconbot

It's probably this line that throws the non "statsCode" errors in the http handler. The handler is bound to null so it can't do a this.emit('error', error) instead of throwing. I notice there are a few other locations where there's uncaught throws too, so I'm guessing it was intentional.

Are you open for a change in this behavior?

reconbot avatar Aug 19 '21 03:08 reconbot

Yeah, I think I had it there as a "look, we don't know what this error is, we assume it's bad, let's make sure we crash the process". Definitely overkill if there are known errors that can be dealt with

mhart avatar Aug 19 '21 13:08 mhart