function failWebsocketConnection (handler, code, reason, cause) {
  // If _The WebSocket Connection is Established_ prior to the point where
  // the endpoint is required to _Fail the WebSocket Connection_, the
  // endpoint SHOULD send a Close frame with an appropriate status code
  // (Section 7.4) before proceeding to _Close the WebSocket Connection_.
  if (isEstablished(handler.readyState)) {
    closeWebSocketConnection(handler, code, reason, false)
  }

  handler.controller.abort()

  if (handler.socket?.destroyed === false) {
    handler.socket.destroy()
  }

  handler.onFail(code, reason, cause)
}
@param {import('./websocket').Handler} handler
@param {number} code
@param {string|undefined} reason
@param {unknown} cause
@returns {void}