Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Refactor the error page (#2845)
Olga Bulata2b00fa7618b
diff --git a/frontend/src/components/VErrorSection/VErrorImage.vue b/frontend/src/components/VErrorSection/VErrorImage.vueindex edf701eec..3f09a47b7 100644--- a/frontend/src/components/VErrorSection/VErrorImage.vue+++ b/frontend/src/components/VErrorSection/VErrorImage.vue@@ -15,7 +15,6 @@import { computed, defineComponent, PropType } from "vue"import type { License, LicenseVersion } from "~/constants/license"-import type { ErrorCode } from "~/constants/errors"import { AttributableMedia, getAttribution } from "~/utils/attribution-html"import { useI18n } from "~/composables/use-i18n"@@ -38,7 +37,7 @@ export default defineComponent({* the code of the error, used to identify and render the appropriate image*/errorCode: {- type: String as PropType<ErrorCode>,+ type: String as PropType<"NO_RESULT" | "SERVER_TIMEOUT">,required: true,},},@@ -71,12 +70,13 @@ export default defineComponent({<style scoped>::v-deep(.attribution) {- @apply text-dark-charcoal-70;+ @apply mt-4 text-sr text-dark-charcoal-70;}-::v-deep(a) {+::v-deep(.attribution a) {@apply text-current underline;}-::v-deep(img) {- @apply opacity-70; /* to match the text color */+/* license icons should match the text color */+::v-deep(.attribution img) {+ @apply opacity-70;}</style>diff --git a/frontend/src/components/VErrorSection/VErrorSection.vue b/frontend/src/components/VErrorSection/VErrorSection.vueindex 0402bbdb6..42363862e 100644--- a/frontend/src/components/VErrorSection/VErrorSection.vue+++ b/frontend/src/components/VErrorSection/VErrorSection.vue@@ -1,21 +1,63 @@<template><div class="error-section mx-auto flex max-w-screen-xl flex-row items-center"><div class="image-pane hidden max-w-[432px] flex-grow md:block">- <!-- @slot Error image goes here -->- <slot name="image" />+ <VErrorImage class="hidden md:block" :error-code="errorCode" /></div><div class="flex-grow p-4 md:p-20">- <!-- @slot Main content goes here -->- <slot />+ <VNoResults v-if="errorCode === NO_RESULT" :search-term="searchTerm" />+ <div v-else>+ <h1 class="heading-4 md:heading-2 text-center !font-semibold">+ {{+ isTimeout ? $t("serverTimeout.heading") : $t("unknownError.heading")+ }}+ </h1>+ </div></div></div></template>+<script lang="ts">+import { computed, defineComponent, PropType } from "vue"-<script>-/**- * This component sets the layout for showing error messages.- */-export default {- name: "VErrorSection",-}+import { NO_RESULT, SERVER_TIMEOUT } from "~/constants/errors"+import { useSearchStore } from "~/stores/search"++import type { NuxtError } from "@nuxt/types"++export default defineComponent({+ components: {+ VNoResults: () => import("~/components/VErrorSection/VNoResults.vue"),+ VErrorImage: () => import("~/components/VErrorSection/VErrorImage.vue"),+ },+ props: {+ fetchingError: {+ type: Object as PropType<NuxtError>,+ required: true,+ },+ },+ setup(props) {+ const searchStore = useSearchStore()+ const searchTerm = computed(() => searchStore.searchTerm)+ /**+ * The code used for the error page image.+ * For now, NO_RESULT image is used for searches without result,+ * and SERVER_TIMEOUT image is used as a fall-back for all other errors.+ */+ const errorCode = computed(() => {+ return props.fetchingError?.message?.includes(NO_RESULT)+ ? NO_RESULT+ : SERVER_TIMEOUT+ })++ const isTimeout = computed(() =>+ props.fetchingError?.message?.toLowerCase().includes("timeout")+ )++ return {+ errorCode,+ isTimeout,+ NO_RESULT,+ searchTerm,+ }+ },+})</script>diff --git a/frontend/src/components/VErrorSection/VServerTimeout.vue b/frontend/src/components/VErrorSection/VServerTimeout.vuedeleted file mode 100644index 0892d3d24..000000000--- a/frontend/src/components/VErrorSection/VServerTimeout.vue+++ /dev/null@@ -1,11 +0,0 @@-<template>- <div class="server-timeout">- <h4 class="text-5xl">{{ $t("serverTimeout.heading") }}</h4>- </div>-</template>--<script>-export default {- name: "VServerTimeout",-}-</script>diff --git a/frontend/src/components/VErrorSection/meta/VErrorSection.stories.mdx b/frontend/src/components/VErrorSection/meta/VErrorSection.stories.mdxindex 9af1bc9a3..31215a7d1 100644--- a/frontend/src/components/VErrorSection/meta/VErrorSection.stories.mdx+++ b/frontend/src/components/VErrorSection/meta/VErrorSection.stories.mdx@@ -7,11 +7,9 @@ import {} from "@storybook/addon-docs"import VErrorSection from "~/components/VErrorSection/VErrorSection.vue"-import VErrorImage from "~/components/VErrorSection/VErrorImage.vue"-import VNoResults from "~/components/VErrorSection/VNoResults.vue"-import VServerTimeout from "~/components/VErrorSection/VServerTimeout.vue"-import { NO_RESULT, SERVER_TIMEOUT } from "~/constants/errors"+import { NO_RESULT } from "~/constants/errors"+import { useSearchStore } from "@/stores/search"<Meta title="Components/VErrorSection" component={VErrorSection} />@@ -25,17 +23,14 @@ import { NO_RESULT, SERVER_TIMEOUT } from "~/constants/errors"This section appears when a search returns no hits.-export const NoResultsTemplate = (args) => ({- template: `- <VErrorSection>- <template #image>- <VErrorImage :error-code="args.errorCode" />- </template>- <VNoResults :media-type="args.type" :search-term="args.searchTerm" />- </VErrorSection>- `,- components: { VErrorSection, VErrorImage, VNoResults },+export const ErrorPageTemplate = (args) => ({+ template: `<VErrorSection v-bind="args" />`,+ components: { VErrorSection },setup() {+ const searchStore = useSearchStore()+ searchStore.setSearchTerm(+ args.fetchingError.details?.searchTerm ?? "sad person"+ )return { args }},})@@ -44,12 +39,15 @@ export const NoResultsTemplate = (args) => ({<Storyname="No result"args={{- errorCode: NO_RESULT,- type: "image",- searchTerm: "sad person",+ fetchingError: {+ message: NO_RESULT,+ requestKind: "search",+ searchType: "image",+ details: { searchTerm: "sad person" },+ },}}>- {NoResultsTemplate.bind({})}+ {ErrorPageTemplate.bind({})}</Story></Canvas>@@ -57,27 +55,34 @@ export const NoResultsTemplate = (args) => ({This result appears when an API request times out.-export const ServerTimeoutTemplate = (args) => ({- template: `- <VErrorSection>- <template #image>- <!-- On large screens, image is in the left pane. -->- <VErrorImage class="hidden md:block" :error-code="args.errorCode" />- </template>- <VServerTimeout/>- <!-- On small screens, image is below error message. -->- <VErrorImage class="md:hidden mt-10" :error-code="args.errorCode" />- </VErrorSection>- `,- data: () => ({ SERVER_TIMEOUT }),- components: { VErrorSection, VErrorImage, VServerTimeout },- setup() {- return { args }- },-})-<Canvas>- <Story name="Server timeout" args={{ errorCode: SERVER_TIMEOUT }}>- {ServerTimeoutTemplate.bind({})}+ <Story+ name="Server timeout"+ args={{+ fetchingError: {+ message: "server timeout",+ requestKind: "search",+ searchType: "image",+ },+ }}+ >+ {ErrorPageTemplate.bind({})}</Story></Canvas>++## Unknown error++This page is shown when there is some other error.++<Story+ name="Unknown error"+ args={{+ fetchingError: {+ message: "Unknown error",+ requestKind: "search",+ searchType: "image",+ },+ }}+>+ {ErrorPageTemplate.bind({})}+</Story>diff --git a/frontend/src/locales/scripts/en.json5 b/frontend/src/locales/scripts/en.json5index 134ef55ea..22124e492 100644--- a/frontend/src/locales/scripts/en.json5+++ b/frontend/src/locales/scripts/en.json5@@ -687,6 +687,9 @@serverTimeout: {heading: "Whoops, it looks like that request took too long to complete. Please try again.",},+ unknownError: {+ heading: "Whoops, it looks like something went wrong. Please try again.",+ },searchType: {image: "Images",audio: "Audio",diff --git a/frontend/src/middleware/search.ts b/frontend/src/middleware/search.tsindex e9864a694..f0f2cd78c 100644--- a/frontend/src/middleware/search.ts+++ b/frontend/src/middleware/search.ts@@ -44,11 +44,12 @@ export const searchMiddleware: Middleware = async ({const results = await mediaStore.fetchMedia()const fetchingError = mediaStore.fetchState.fetchingError- // NO_RESULTS is handled client-side, for other errors show server error page+ // NO_RESULTS and timeout are handled client-side, for other errors show server error pageif (!results &&fetchingError &&- !fetchingError?.message?.includes(NO_RESULT)+ !fetchingError?.message?.includes(NO_RESULT) &&+ !fetchingError?.message?.includes("timeout")) {nuxtError(fetchingError)}diff --git a/frontend/src/pages/search.vue b/frontend/src/pages/search.vueindex 13f1e19cf..570020361 100644--- a/frontend/src/pages/search.vue+++ b/frontend/src/pages/search.vue@@ -4,12 +4,11 @@tabindex="-1"class="browse-page flex w-full flex-col px-6 lg:px-10">- <VErrorSection v-if="fetchingError" class="w-full py-10">- <template #image>- <VErrorImage error-code="NO_RESULT" />- </template>- <VNoResults :search-term="searchTerm" />- </VErrorSection>+ <VErrorSection+ v-if="fetchingError"+ :fetching-error="fetchingError"+ class="w-full py-10"+ /><section v-else><header v-if="query.q && supported" class="my-0 md:mb-8 md:mt-4"><VSearchResultsTitle :size="isAllView ? 'large' : 'default'">{{@@ -66,19 +65,15 @@ import { skipToContentTargetId } from "~/constants/window"import { IsSidebarVisibleKey, ShowScrollButtonKey } from "~/types/provides"import { areQueriesEqual } from "~/utils/search-query-transform"+import VErrorSection from "~/components/VErrorSection/VErrorSection.vue"import VScrollButton from "~/components/VScrollButton.vue"import VExternalSearchForm from "~/components/VExternalSearch/VExternalSearchForm.vue"import VSearchResultsTitle from "~/components/VSearchResultsTitle.vue"-import VNoResults from "~/components/VErrorSection/VNoResults.vue"-import VErrorSection from "~/components/VErrorSection/VErrorSection.vue"-import VErrorImage from "~/components/VErrorSection/VErrorImage.vue"export default defineComponent({name: "BrowsePage",components: {- VErrorImage,VErrorSection,- VNoResults,VSearchResultsTitle,VExternalSearchForm,VScrollButton,diff --git a/frontend/src/stores/media/index.ts b/frontend/src/stores/media/index.tsindex 7651359c7..2cbbb1267 100644--- a/frontend/src/stores/media/index.ts+++ b/frontend/src/stores/media/index.ts@@ -531,7 +531,11 @@ export const getNuxtErrorData = (errorData.statusCode = error.response.status} else if (error.request) {// The request was sent, but the response was never received.- errorData.message += `No response received from the server`+ if (error.code === "ECONNABORTED") {+ errorData.message += `Timeout error`+ } else {+ errorData.message += `No response received from the server`+ }} else {// Something happened during the request setup.errorData.message += `Unknown Axios error`diff --git a/frontend/test/locales/ar.json b/frontend/test/locales/ar.jsonindex 6a8d7afdd..a7037ed67 100644--- a/frontend/test/locales/ar.json+++ b/frontend/test/locales/ar.json@@ -699,5 +699,8 @@},"none": "لا توجد عمليات بحث حديثة لعرضها.","disclaimer": "لا يقوم Openverse بتخزين عمليات البحث الأخيرة ، يتم الاحتفاظ بهذه المعلومات محليًا في متصفحك."+ },+ "unknownError": {+ "heading": "عفوًا ، يبدو أنه حدث خطأ ما. حاول مرة اخرى."}}diff --git a/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts b/frontend/test/playwright/visual-regression/components/global-audio-player.spec.tsindex 40b5dca5a..9cd5d8c4d 100644--- a/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts+++ b/frontend/test/playwright/visual-regression/components/global-audio-player.spec.ts@@ -5,6 +5,7 @@ import {dismissAnalyticsBanner,languageDirections,pathWithDir,+ t,} from "~~/test/playwright/utils/navigation"import breakpoints from "~~/test/playwright/utils/breakpoints"import audio from "~~/test/playwright/utils/audio"@@ -21,7 +22,10 @@ for (const dir of languageDirections) {)const audioRow = await audio.getNthAudioRow(page, 2)await audio.play(audioRow, dir)- await audio.pause(audioRow, dir)+ await page+ .locator(".global-track")+ .getByRole("button", { name: t("playPause.pause", dir) })+ .click()// To make the tests consistent, set the played area to the same positionawait page.mouse.click(170, 650)await expectSnapshot(`global-audio-player-on-search-${dir}.png`, page)diff --git a/frontend/test/playwright/visual-regression/pages/errors.spec.ts b/frontend/test/playwright/visual-regression/pages/errors.spec.tsindex 72b7e799e..a1a69f5f2 100644--- a/frontend/test/playwright/visual-regression/pages/errors.spec.ts+++ b/frontend/test/playwright/visual-regression/pages/errors.spec.ts@@ -124,14 +124,14 @@ for (const searchType of supportedSearchTypes) {})await goToSearchTerm(page, "cat", { dir, searchType, mode: "CSR" })- // TODO: uncomment when the timeout page is implemented- // await setViewportToFullHeight(page)+ await setViewportToFullHeight(page)await page.mouse.move(0, 82)- await expectSnapshot(`search-result-timeout-${dir}`, page, {- fullPage: true,- })+ await expectSnapshot(+ `search-result-timeout-${dir}`,+ page.locator("#main-page")+ )})}})diff --git a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-audio-no-results-ltr-xl-linux.png b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-audio-no-results-ltr-xl-linux.pngindex ba030ce42..e064b4eec 100644Binary files a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-audio-no-results-ltr-xl-linux.png and b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-audio-no-results-ltr-xl-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-audio-no-results-rtl-xl-linux.png b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-audio-no-results-rtl-xl-linux.pngindex c684b6d4b..58a711bcc 100644Binary files a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-audio-no-results-rtl-xl-linux.png and b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-audio-no-results-rtl-xl-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-image-no-results-ltr-xl-linux.png b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-image-no-results-ltr-xl-linux.pngindex 3f4d444e8..1e01d8230 100644Binary files a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-image-no-results-ltr-xl-linux.png and b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-image-no-results-ltr-xl-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-image-no-results-rtl-xl-linux.png b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-image-no-results-rtl-xl-linux.pngindex b9fd7c42e..e2bc4e086 100644Binary files a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-image-no-results-rtl-xl-linux.png and b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-image-no-results-rtl-xl-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-ltr-sm-linux.png b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-ltr-sm-linux.pngindex 824dda742..79f7784f0 100644Binary files a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-ltr-sm-linux.png and b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-ltr-sm-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-ltr-xl-linux.png b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-ltr-xl-linux.pngindex 60d576452..efc8fc93a 100644Binary files a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-ltr-xl-linux.png and b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-ltr-xl-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-rtl-sm-linux.png b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-rtl-sm-linux.pngindex 4120a950b..ccfff4dcc 100644Binary files a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-rtl-sm-linux.png and b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-rtl-sm-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-rtl-xl-linux.png b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-rtl-xl-linux.pngindex 1b3c3e663..76bcb7063 100644Binary files a/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-rtl-xl-linux.png and b/frontend/test/playwright/visual-regression/pages/errors.spec.ts-snapshots/search-result-timeout-rtl-xl-linux.png differ