Add an onerror callback to close the connection on error