Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Handling play errors (#1724)

Ramadan Omarc9b2e6d8215a
diff --git a/README.md b/README.md
index 9803f010e..4bb566bad 100644
--- a/README.md
+++ b/README.md
@@ -142,14 +142,15 @@ The app will be available at <http://localhost:8443>.
> **Note**: If you are _not_ using HTTPS locally, do not use the URL provided by Nuxt - this will cause certificate errors because it gets rerouted to HTTPS.
> E.g. this link will not work:
+>
> ```
> $ docker run --rm -it -p 127.0.0.1:8443:8443/tcp openverse-frontend:latest
->
+>
> > nuxt start
->
+>
> ℹ Sentry reporting is disabled ("disabled" option has been set) nuxt:sentry 18:22:32
->
+>
> ╭─────────────────────────────────────────╮
> │ │
> │ Nuxt @ v2.15.8 │
diff --git a/src/components/VAudioTrack/VAudioTrack.vue b/src/components/VAudioTrack/VAudioTrack.vue
index 63ab641da..dcc38dc9c 100644
--- a/src/components/VAudioTrack/VAudioTrack.vue
+++ b/src/components/VAudioTrack/VAudioTrack.vue
@@ -25,7 +25,7 @@
:audio-id="audio.id"
:current-time="currentTime"
:duration="duration"
- :message="message ? $t(`audio-track.messages.${message}`) : null"
+ :message="message"
@seeked="handleSeeked"
@toggle-playback="handleToggle"
/>
@@ -50,6 +50,7 @@ import {
ref,
watch,
onUnmounted,
+ useContext,
useRoute,
PropType,
} from '@nuxtjs/composition-api'
@@ -132,6 +133,7 @@ export default defineComponent({
},
setup(props, { emit }) {
const i18n = useI18n()
+ const { $sentry } = useContext()
const activeMediaStore = useActiveMediaStore()
const route = useRoute()
@@ -295,7 +297,7 @@ export default defineComponent({
)
if (
- route.value.params.id === props.audio.id ||
+ route.value?.params?.id === props.audio.id ||
mediaStore.getItemById(AUDIO, props.audio.id)
) {
/**
@@ -322,9 +324,27 @@ export default defineComponent({
})
const play = () => {
- // delay initializing the local audio element until playback is requested
+ // Delay initializing the local audio element until playback is requested
if (!localAudio) initLocalAudio()
- localAudio?.play()
+
+ // Check if the audio can be played successfully
+ localAudio?.play().catch((err) => {
+ let errorMsg = ''
+ switch (err.name) {
+ case 'NotAllowedError':
+ errorMsg = 'err_unallowed'
+ break
+ case 'NotSupportedError':
+ errorMsg = 'err_unsupported'
+ break
+ default:
+ errorMsg = 'err_unknown'
+ $sentry.captureException(err)
+ }
+ errorMsg = i18n.t(`audio-track.messages.${errorMsg}`).toString()
+ activeMediaStore.setMessage({ message: errorMsg })
+ localAudio?.pause()
+ })
}
const pause = () => localAudio?.pause()
diff --git a/src/components/VAudioTrack/layouts/VGlobalLayout.vue b/src/components/VAudioTrack/layouts/VGlobalLayout.vue
index c592c6a49..424787127 100644
--- a/src/components/VAudioTrack/layouts/VGlobalLayout.vue
+++ b/src/components/VAudioTrack/layouts/VGlobalLayout.vue
@@ -2,7 +2,7 @@
<div class="global-track flex w-full flex-row">
<div class="flex-shrink-0">
<VAudioThumbnail :audio="audio" />
- <slot name="play-pause" :size="size" :layout="layout" />
+ <slot name="play-pause" size="medium" layout="global" />
</div>
<div class="relative flex-grow">
@@ -38,12 +38,6 @@ export default defineComponent({
required: true,
},
},
- setup() {
- const size = 'medium' as const
- const layout = 'global' as const
-
- return { size, layout }
- },
})
</script>
diff --git a/src/components/VContentReport/VContentReportForm.vue b/src/components/VContentReport/VContentReportForm.vue
index c7c78f135..83f0cf430 100644
--- a/src/components/VContentReport/VContentReportForm.vue
+++ b/src/components/VContentReport/VContentReportForm.vue
@@ -10,9 +10,11 @@
tag="p"
>
<template #source>
- <VLink :href="media.foreign_landing_url" class="text-pink hover:underline">{{
- providerName
- }}</VLink>
+ <VLink
+ :href="media.foreign_landing_url"
+ class="text-pink hover:underline"
+ >{{ providerName }}</VLink
+ >
</template>
</i18n>
</div>
diff --git a/src/locales/en.json b/src/locales/en.json
index a6484c8ac..a26832d68 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -575,6 +575,8 @@
"err_aborted": "You aborted playback.",
"err_network": "A network error occurred.",
"err_decode": "Could not decode audio.",
+ "err_unallowed": "Reproduction not allowed.",
+ "err_unknown": "An unexpected error has ocurred. Try again in a few minutes or report the item if the issue persists.",
"err_unsupported": "This audio format is not supported by your browser.",
"loading": "Loading..."
},
diff --git a/test/unit/fixtures/audio.js b/test/unit/fixtures/audio.js
index cf56a02e1..407c9d08c 100644
--- a/test/unit/fixtures/audio.js
+++ b/test/unit/fixtures/audio.js
@@ -30,6 +30,7 @@ export const getAudioObj = (overrides = {}) =>
name: 'cuivres',
},
],
+ duration: 270000,
alt_files: null,
attribution:
'"La vie des bêtes" by AS-POTIRONT! is licensed under CC BY-NC-SA 2.5. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/2.5/.',
diff --git a/test/unit/specs/components/AudioTrack/v-audio-track.spec.js b/test/unit/specs/components/AudioTrack/v-audio-track.spec.js
index 7be0943e3..67d2af256 100644
--- a/test/unit/specs/components/AudioTrack/v-audio-track.spec.js
+++ b/test/unit/specs/components/AudioTrack/v-audio-track.spec.js
@@ -1,4 +1,4 @@
-import { render } from '@testing-library/vue'
+import { fireEvent, render } from '@testing-library/vue'
import Vuei18n from 'vue-i18n'
import {
@@ -15,6 +15,10 @@ import VAudioTrack from '~/components/VAudioTrack/VAudioTrack.vue'
const enMessages = require('~/locales/en.json')
+window.HTMLMediaElement.prototype.play = () => {
+ /* mock */
+}
+
const mockI18n = new Vuei18n({
locale: 'en',
fallbackLocale: 'en',
@@ -26,7 +30,10 @@ jest.mock('~/composables/use-browser-detection', () => ({
}))
jest.mock('~/composables/use-i18n', () => ({
- useI18n: jest.fn(() => mockI18n),
+ useI18n: jest.fn(() => ({
+ t: jest.fn((val) => val),
+ tc: jest.fn((val) => val),
+ })),
}))
const useVueI18n = (vue) => {
@@ -84,9 +91,6 @@ describe('AudioTrack', () => {
options = {
propsData: props,
stubs,
- mocks: {
- $nuxt: { context: { i18n: { t: jest.fn() } } },
- },
}
setActivePinia(createPinia())
@@ -98,10 +102,7 @@ describe('AudioTrack', () => {
})
it('should render the row audio track component even without duration', () => {
- options.propsData = {
- ...options.propsData,
- layout: 'row',
- }
+ options.propsData.layout = 'row'
const { getByText } = render(VAudioTrack, options, configureVue)
getByText('by ' + props.audio.creator)
})
@@ -119,4 +120,28 @@ describe('AudioTrack', () => {
expect(element).toBeInstanceOf(HTMLAnchorElement)
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'))
+ )
+ const { getByRole, getByText } = render(VAudioTrack, options, configureVue)
+
+ await fireEvent.click(getByRole('button'))
+ await expect(playStub).toHaveBeenCalledTimes(1)
+ await expect(pauseStub).toHaveBeenCalledTimes(1)
+ await expect(getByText('audio-track.messages.err_unallowed')).toBeVisible()
+ // It's not possible to get the vm to test that Sentry has been called
+ })
})