Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid the Assertion error? #244

Open
5nyper opened this issue May 3, 2017 · 2 comments
Open

Avoid the Assertion error? #244

5nyper opened this issue May 3, 2017 · 2 comments

Comments

@5nyper
Copy link

5nyper commented May 3, 2017

At times I would get this error:

assert.js:85
  throw new assert.AssertionError({
  ^
AssertionError: false == true
    at Connection._send (/Users/vikaton/Desktop/speech2text/node_modules/http2/lib/protocol/connection.js:326:9)
    at runCallback (timers.js:651:20)
    at tryOnImmediate (timers.js:624:5)
    at processImmediate [as _immediateCallback] (timers.js:596:5)

when running this function

function post(audioBuffer) {
  return new Promise((resolve, reject) => {
   // some headers and content
  let req = require('http2').request(options, function(res) {
      console.log("CALLBACK HAS BEEN CALLED BACK")
      console.log(res.statusCode)
      if (res.statusCode > 204) {
          console.log("ERR with return code " + res.statusCode)
          req.end('', function(){
            console.log('ended request cause of bad code')
          })
          reject('BAD CODE')
      }
      else if (res.statusCode == 204) {
        in_session = false
        player.play('204.mp3', function(err) {
          if (err) throw err
        })
        reject('NO REPSONSE') . // usually this doesnt fire cause I get The assertion error here
      }
  		streamToBuffer(res, function(err, buffer) {
  			console.log('response', buffer.length);
  			if (err) {
  				console.error('error', err);
  				return false;
  			}

  			let parsedMessage = httpParser(buffer);
  			var multipart = parsedMessage.multipart;

  			if (Array.isArray(multipart)) {
  				multipart.forEach(function(part) {
  					//console.log(part)
  					var headers = part.headers;
  					var bodyBuffer = part.body;
  					var contentType = _.get(headers, 'Content-Type');

  					if (bodyBuffer) {
  						if (contentType === 'application/octet-stream') {
  							fs.writeFileSync('audio.mp3', bodyBuffer)
  							player.play('audio.mp3', function(err) {
  								if (err) throw err
                  in_session = false
                  resolve('SUCCESS')
  							})
  						} else if (contentType === 'application/json') {
  							var body = JSON.parse(bodyBuffer.toString('utf8'));
  							console.log(body) //bookmark
  						}
  					}
  				});
  			}
  		});

  		req.on('error', function(e) {
  			console.log('problem with request: ' + e.message);
  		});
      req.setTimeout(5000, function(){
        in_session = false
        console.log('timed out')
      })
  	});
    console.log('REQ IS ' + req)
  	if (isStream(audioBuffer)) {
  		streamToBuffer(audioBuffer, function(error, buffer) {
  			if (error) {
  				console.error(error);
  				return false;
  			}
  			sendRequest(buffer); //check before this
  		});
  	} else if (isBuffer(audioBuffer)) {
  		sendRequest(audioBuffer);
  	} else {
  		console.error('Audio buffer invalid');
  	}

  	function sendRequest(audBuffer) {
      console.log('should sent data')
  		req.write(postDataStart);
  		console.log(postDataStart)
  		req.write(audBuffer);
  		console.log(audBuffer)
  		req.write(postDataEnd);
  		console.log(postDataEnd)
  		req.end(null, function(){
        console.log('ended request and sent data')
      })
  	}
  })
}

My question is, what is the underlying cause for this error in the code (if its even the code thats the problem) and how can I avoid it?

@5nyper
Copy link
Author

5nyper commented May 3, 2017

Apologies for the horrible formatting :/

@zaknuces
Copy link

#239

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants