How to use the network/error.NetworkError function in network

To help you get started, we’ve selected a few network examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github samuelmaddock / metastream / packages / metastream-app / src / platform / web / rtc-coordinator.ts View on Github external
).then(() => {
      if (this.sessionClient) {
        console.debug('Reconnected to signal server')
        this.emit('error', new NetworkError(NetworkErrorCode.SignalServerReconnect))
      }
    })
  }
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / rtc-coordinator.ts View on Github external
private async authenticatePeer(peer: SimplePeer.Instance, peerId?: string) {
    const peerPublicKey = peerId ? sodium.from_hex(peerId) : undefined
    const userPublicKey = await mutualHandshake(peer, localUser().id, peerPublicKey)

    if (!userPublicKey) {
      const addr = `${(peer as any).remoteAddress}:${(peer as any).remotePort}`
      peer.destroy()
      throw new NetworkError(
        NetworkErrorCode.PeerAuthenticationFailure,
        `Failed to authenticate with peer [${addr}]`
      )
    }

    const userId = sodium.to_hex(userPublicKey)
    const netId = new NetUniqueId(userPublicKey)
    const conn = new RTCPeerConn(netId, peer)

    console.debug(`Authenticated peer ${userId}`, conn)
    this.emit('connection', conn)
  }
}
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / rtc-coordinator.ts View on Github external
let client

    try {
      client = await this.getClient()
    } catch {
      throw new NetworkError(NetworkErrorCode.SignalServerConnectionFailure)
    }

    try {
      await client.createRoom({
        publicKey: localUser().id.publicKey,
        privateKey: localUser().id.privateKey!
      })
    } catch (e) {
      console.error(e)
      throw new NetworkError(
        NetworkErrorCode.SignalServerConnectionFailure,
        'Failed to create room'
      )
    }

    client.on('peer', this.authenticatePeer)
    client.on('close', this.serverConnectionClosed)

    this.sessionClient = client
    this.intervalId = setInterval(this.keepAlive, KEEP_ALIVE_INTERVAL) as any

    console.debug('Created signal client session')
  }
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / rtc-coordinator.ts View on Github external
private async joinSession(hostId: string) {
    let client
    try {
      client = await this.getClient()
    } catch {
      throw new NetworkError(NetworkErrorCode.SignalServerConnectionFailure)
    }

    let peer
    try {
      peer = await client.joinRoom(hostId)
    } catch (e) {
      if (e.code === SignalErrorCode.RoomNotFound) {
        throw new NetworkError(NetworkErrorCode.SignalServerSessionNotFound, 'Session not found')
      } else {
        console.error(e)
        throw new NetworkError(
          NetworkErrorCode.SignalServerConnectionFailure,
          'Failed to join room'
        )
      }
    } finally {
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / rtc-coordinator.ts View on Github external
private async joinSession(hostId: string) {
    let client
    try {
      client = await this.getClient()
    } catch {
      throw new NetworkError(NetworkErrorCode.SignalServerConnectionFailure)
    }

    let peer
    try {
      peer = await client.joinRoom(hostId)
    } catch (e) {
      if (e.code === SignalErrorCode.RoomNotFound) {
        throw new NetworkError(NetworkErrorCode.SignalServerSessionNotFound, 'Session not found')
      } else {
        console.error(e)
        throw new NetworkError(
          NetworkErrorCode.SignalServerConnectionFailure,
          'Failed to join room'
        )
      }
    } finally {
      client.close()
    }

    console.debug('Joined signal client session', peer)
    await this.authenticatePeer(peer, hostId)
  }
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / rtc-coordinator.ts View on Github external
let client
    try {
      client = await this.getClient()
    } catch {
      throw new NetworkError(NetworkErrorCode.SignalServerConnectionFailure)
    }

    let peer
    try {
      peer = await client.joinRoom(hostId)
    } catch (e) {
      if (e.code === SignalErrorCode.RoomNotFound) {
        throw new NetworkError(NetworkErrorCode.SignalServerSessionNotFound, 'Session not found')
      } else {
        console.error(e)
        throw new NetworkError(
          NetworkErrorCode.SignalServerConnectionFailure,
          'Failed to join room'
        )
      }
    } finally {
      client.close()
    }

    console.debug('Joined signal client session', peer)
    await this.authenticatePeer(peer, hostId)
  }
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / rtc-coordinator.ts View on Github external
private async createSession() {
    let client

    try {
      client = await this.getClient()
    } catch {
      throw new NetworkError(NetworkErrorCode.SignalServerConnectionFailure)
    }

    try {
      await client.createRoom({
        publicKey: localUser().id.publicKey,
        privateKey: localUser().id.privateKey!
      })
    } catch (e) {
      console.error(e)
      throw new NetworkError(
        NetworkErrorCode.SignalServerConnectionFailure,
        'Failed to create room'
      )
    }

    client.on('peer', this.authenticatePeer)
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / rtc-coordinator.ts View on Github external
private serverConnectionClosed = () => {
    if (this.closed) return
    this.emit('error', new NetworkError(NetworkErrorCode.SignalServerDisconnect))
    this.reconnect()
  }