Remove query emit 'end' event when query has error

Closes #547