0

probably a simple question for some of you to answer.

I'm trying to check the status of an online audio stream then utilize that stream in some code (https://api.tmw.media/ggradio/stream & https://api.tmw.media/ggradio/stream/ogg). I know I will get a 404 if the file doesn't exist, but I'm having issues where I get stuck awaiting the fetch to return if status 200. Any advice on how I should be doing this?

export async function startBroadcast(this: Root) {
  if (!this.extensions.broadcast) {
    this.extensions['broadcast'] = this.client.voice?.createBroadcast()
    if (this.config.radio?.streamLink) {
      if (this.extensions.broadcast) {
        const broadcast = this.extensions.broadcast as VoiceBroadcast
        let tested: number = 0
        for (const link of this.config.radio.streamLink) {
          const req = await this.fetch(link)
          console.log(req)
          if (!req.ok) {
            this.log(LoggingLevels.error, `${link} is 404`)
            tested++
            if (tested == this.config.radio.streamLink.length) {
              this.log(
                LoggingLevels.emergency,
                `All stream links failed. Radio is likely offline`
              )
              throw new Error('All stream links failed')
            }
            return
          }
          const broadcastOptions: StreamOptions = {
            highWaterMark: 50,
            volume: false
          }
          this.log(
            LoggingLevels.debug,
            `Starting up broadcast using stream link: ${link}`
          )

          broadcast.play(link, broadcastOptions)
          break
        }
      }
    }
  }
}

(this.fetch is https://www.npmjs.com/package/node-fetch)

  • The fetch request probably returns a promise which is fulfilled in the 200er case and rejected in any error case. If you use `await`, you need to catch the error with try/catch to handle it. So, only if it succeeds, the subsequent code is executed without try/catch. – ssc-hrep3 May 29 '21 at 14:27
  • @ssc-hrep3 thanks for the comment, its actually having issues the other way round. The fetch request is getting fulfilled with 404 when the file doesn't exist but hangs when the file does exist. So in other words, when the file exists the code never passes the await as it's not fulfilling – Jonathan Stevens May 30 '21 at 01:59

1 Answers1

0

I'm not entirely sure what I changed to make it work, however this is the working code.

export async function startBroadcast(this: Root) {
  if (!this.extensions.broadcast) {
    this.extensions['broadcast'] = this.client.voice?.createBroadcast()
    if (this.config.radio?.streamLink) {
      if (this.extensions.broadcast) {
        const broadcast = this.extensions.broadcast as VoiceBroadcast
        let tested: number = 0
        for (const link of this.config.radio.streamLink) {
          console.log(link)
          const req = await this.fetch(link)
          console.log(req)
          if (!req.ok) {
            this.log(LoggingLevels.alert, `${link} is 404`)
            tested++
            if (tested == this.config.radio.streamLink.length) {
              this.log(
                LoggingLevels.emergency,
                `All stream links failed. Radio is likely offline`
              )
              throw new Error('All stream links failed')
            }
          } else {
            const broadcastOptions: StreamOptions = {
              highWaterMark: 50,
              volume: false
            }
            this.log(
              LoggingLevels.debug,
              `Starting up broadcast using stream link: ${link}`
            )

            broadcast.play(link, broadcastOptions)
            break
          }
        }
      }
    }
  }
}

I think it was the way I was returning inside the loop that was actually returning the function.