Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Handle AbortError in VAudioTrack.vue (#2681)
Olga Bulatb4b46b903731
diff --git a/frontend/src/components/VAudioTrack/VAudioTrack.vue b/frontend/src/components/VAudioTrack/VAudioTrack.vueindex 37110363e..3ab57e982 100644--- a/frontend/src/components/VAudioTrack/VAudioTrack.vue+++ b/frontend/src/components/VAudioTrack/VAudioTrack.vue@@ -347,23 +347,29 @@ export default defineComponent({// Delay initializing the local audio element until playback is requestedif (!localAudio) initLocalAudio()+ const playPromise = localAudio?.play()// Check if the audio can be played successfully- localAudio?.play().catch((err) => {- let message: string- switch (err.name) {- case "NotAllowedError":- message = "err_unallowed"- break- case "NotSupportedError":- message = "err_unsupported"- break- default:- message = "err_unknown"- $sentry.captureException(err)- }- activeMediaStore.setMessage({ message })- localAudio?.pause()- })+ if (playPromise !== undefined) {+ playPromise.catch((err) => {+ let message: string+ switch (err.name) {+ case "NotAllowedError":+ message = "err_unallowed"+ break+ case "NotSupportedError":+ message = "err_unsupported"+ break+ case "AbortError":+ message = "err_aborted"+ break+ default:+ message = "err_unknown"+ $sentry.captureException(err)+ }+ activeMediaStore.setMessage({ message })+ localAudio?.pause()+ })+ }}const pause = () => localAudio?.pause()diff --git a/frontend/test/unit/specs/components/AudioTrack/v-audio-track.spec.js b/frontend/test/unit/specs/components/AudioTrack/v-audio-track.spec.jsindex 14aa9c03c..38b151c67 100644--- a/frontend/test/unit/specs/components/AudioTrack/v-audio-track.spec.js+++ b/frontend/test/unit/specs/components/AudioTrack/v-audio-track.spec.js@@ -3,6 +3,7 @@import { fireEvent } from "@testing-library/vue"import { render } from "~~/test/unit/test-utils/render"+import { i18n } from "~~/test/unit/test-utils/i18n"import { getAudioObj } from "~~/test/unit/fixtures/audio"import { useActiveMediaStore } from "~/stores/active-media"@@ -30,6 +31,10 @@ describe("AudioTrack", () => {let options = nulllet props = nulllet configureVue = null+ let captureExceptionMock = jest.fn()+ const $nuxt = {+ context: { i18n, $sentry: { captureException: captureExceptionMock } },+ }beforeEach(() => {props = {@@ -50,6 +55,7 @@ describe("AudioTrack", () => {options = {propsData: props,stubs,+ mocks: { $nuxt },}})@@ -78,29 +84,54 @@ describe("AudioTrack", () => {expect(element).toHaveAttribute("href", props.audio.creator_url)})- it("on play error displays a message instead of the waveform", async () => {- options.propsData.audio.url = "bad.url"- options.propsData.layout = "row"- options.stubs.VPlayPause = false- options.stubs.VWaveform = false- options.stubs.VAudioThumbnail = true- const pauseStub = jest- .spyOn(window.HTMLMediaElement.prototype, "pause")- .mockImplementation(() => undefined)-- const playStub = jest- .spyOn(window.HTMLMediaElement.prototype, "play")- .mockImplementation(() =>- Promise.reject(new DOMException("msg", "NotAllowedError"))+ it.each`+ errorType | errorText+ ${"NotAllowedError"} | ${/Reproduction not allowed./i}+ ${"NotSupportedError"} | ${/This audio format is not supported by your browser./i}+ ${"AbortError"} | ${/You aborted playback./i}+ ${"UnknownError"} | ${/An unexpected error has occurred./i}+ `(+ "on play error displays a message instead of the waveform",+ async ({ errorType, errorText }) => {+ options.propsData.audio.url = "bad.url"+ options.propsData.layout = "row"+ options.stubs.VPlayPause = false+ options.stubs.VWaveform = false+ options.stubs.VAudioThumbnail = true++ jest.clearAllMocks()++ const pauseStub = jest+ .spyOn(window.HTMLMediaElement.prototype, "pause")+ .mockImplementation(() => undefined)++ const playError = new DOMException("msg", errorType)++ const playStub = jest+ .spyOn(window.HTMLMediaElement.prototype, "play")+ .mockImplementation(() => Promise.reject(playError))++ const { getByRole, getByText } = render(+ VAudioTrack,+ options,+ configureVue)- const { getByRole, getByText } = render(VAudioTrack, options, configureVue)- await fireEvent.click(getByRole("button"))- expect(playStub).toHaveBeenCalledTimes(1)- expect(pauseStub).toHaveBeenCalledTimes(1)- expect(getByText(/Reproduction not allowed./i)).toBeVisible()- // It's not possible to get the vm to test that Sentry has been called- })+ await fireEvent.click(getByRole("button"))+ expect(playStub).toHaveBeenCalledTimes(1)+ expect(pauseStub).toHaveBeenCalledTimes(1)+ expect(getByText(errorText)).toBeVisible()++ // Only the UnknownError should be sent to Sentry.+ if (errorType === "UnknownError") {+ // eslint-disable-next-line jest/no-conditional-expect+ expect(captureExceptionMock).toHaveBeenCalledWith(playError)+ } else {+ // eslint-disable-next-line jest/no-conditional-expect+ expect(captureExceptionMock).not.toHaveBeenCalled()+ }+ }+ )it("has blurred title in box layout when audio is sensitive", async () => {options.propsData.audio.isSensitive = true