0

I encountered the error below with NodeJS module for Amazon S3: Knox

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: socket hang up
    at SecurePair.error (tls.js:934:23)
    at CleartextStream.read [as _read] (tls.js:432:17)
    at CleartextStream.Readable.read (_stream_readable.js:320:10)
    at EncryptedStream.write [as _write] (tls.js:345:25)
    at doWrite (_stream_writable.js:219:10)
    at writeOrBuffer (_stream_writable.js:209:5)
    at EncryptedStream.Writable.write (_stream_writable.js:180:11)
    at write (_stream_readable.js:573:24)
    at flow (_stream_readable.js:582:7)
    at Socket.pipeOnReadable (_stream_readable.js:614:5)
    at Socket.EventEmitter.emit (events.js:92:17)

After enabling longjohn, I can say that the error is on route for display image from Amazon S3.

exports.image = function(req, res) {
    var type = req.params.type;
    var id = req.params.id;
    var file = req.params.file;
    var url = '/' + type + '/' + id + '/' + file;


    var data = '';
    knoxClient.get(url).on('response', function(s3res) {
        s3res.setEncoding('binary');
        s3res.on('data', function(chunk){
            data += chunk;
        });
        s3res.on('end', function() {
            res.write(data, encoding='binary');
            res.end();
        });
    }).end();
};

How to handle the error such that the server will not crash ?

ben75
  • 29,217
  • 10
  • 88
  • 134
JR Galia
  • 17,229
  • 19
  • 92
  • 144

1 Answers1

0

You most likely forgot to add a handler to the 'error' event of your server socket.

The reason why the stack does not contain any reference to your code is because of the evented nature of node.js. Whenever an event fires, the stack is restarted from scratch. Because of this, it's a bit hard to debug async calls.

You can try using longjohn during development.

Laurent Perrin
  • 14,671
  • 5
  • 50
  • 49
  • thanks, for `longjohn`, is `require('longjohn');` enough? Or need some lines of code to use it. Thanks – JR Galia Sep 30 '13 at 08:57
  • You just need to require it at the beginning of your main file. Then, your stack traces will become much more useful. It will increase memory usage and slightly slow down your server. – Laurent Perrin Sep 30 '13 at 09:13
  • i figured out where the error came from, it's on Knox S3 module for Amazon S3 – JR Galia Oct 01 '13 at 12:21
  • Amazon now has an official node.js module with S3 support which works great. Another issue you might have with Knox is that it doesn't handle HTTP redirections: https://github.com/LearnBoost/knox/issues/178. – Laurent Perrin Oct 01 '13 at 13:08