diff --git a/src/main.js b/src/main.js index 443db82a..005160c0 100644 --- a/src/main.js +++ b/src/main.js @@ -218,17 +218,17 @@ router.afterEach(async () => { try { await storage.sync(); } catch (error) { - if ( - error.name === "AxiosError" && - [AxiosError.ETIMEDOUT, AxiosError.ECONNABORTED, AxiosError.ERR_NETWORK].includes(error.code) - ) { - toast.error( - "Une erreur de réseau est survenue. Si votre connexion " + - "est instable, vous pouvez passer en mode hors-ligne.", - ); - return; + if (error.name === "AxiosError") { + if ([AxiosError.ETIMEDOUT, AxiosError.ECONNABORTED, AxiosError.ERR_NETWORK].includes(error.code)) { + toast.error( + "Une erreur de réseau est survenue. Si votre connexion " + + "est instable, vous pouvez passer en mode hors-ligne.", + ); + return; + } else if (error.code === AxiosError.ERR_BAD_REQUEST) { + toast.error("Un problème technique est survenu. Veuillez réessayer ultérieurement."); + } } - throw error; } }); diff --git a/src/stores/storage.js b/src/stores/storage.js index c428fcdd..59442b0e 100644 --- a/src/stores/storage.js +++ b/src/stores/storage.js @@ -3,6 +3,8 @@ import { computed, ref, watch } from "vue"; import { useLocalStorage, useOnline } from "@vueuse/core"; import { apiClient, createOperatorRecord } from "@/cartobio-api.js"; import { legalProjectionSurface } from "@/utils/features.js"; +import { AxiosError } from "axios"; +import toast from "@/utils/toast.js"; /** * @typedef {import('@agencebio/cartobio-types').AgenceBioNormalizedOperator} AgenceBioNormalizedOperator @@ -454,6 +456,10 @@ export const useCartoBioStorage = defineStore("storage", () => { continue; } + if (e.response?.status === 400) { + delete syncQueues.value[recordId]; + } + throw e; } } @@ -462,7 +468,28 @@ export const useCartoBioStorage = defineStore("storage", () => { } } - watch(() => [online, syncQueues], sync, { deep: true }); + watch( + () => [online, syncQueues], + async () => { + try { + await sync(); + } catch (error) { + if (error.name === "AxiosError") { + if ([AxiosError.ETIMEDOUT, AxiosError.ECONNABORTED, AxiosError.ERR_NETWORK].includes(error.code)) { + toast.error( + "Une erreur de réseau est survenue. Si votre connexion " + + "est instable, vous pouvez passer en mode hors-ligne.", + ); + return; + } else if (error.code === AxiosError.ERR_BAD_REQUEST) { + toast.error("Un problème technique est survenu. Veuillez réessayer ultérieurement."); + } + } + throw error; + } + }, + { deep: true }, + ); return { // storage ref diff --git a/src/stores/storage.test.js b/src/stores/storage.test.js index 9284ab36..cd50c783 100644 --- a/src/stores/storage.test.js +++ b/src/stores/storage.test.js @@ -3,7 +3,7 @@ import { createTestingPinia } from "@pinia/testing"; import { SyncOperation, useCartoBioStorage } from "@/stores/storage.js"; import record from "@/utils/__fixtures__/record-with-features.json" assert { type: "json" }; import axios from "axios"; - +import axios, { AxiosError } from "axios"; const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false }); beforeEach(() => { @@ -95,4 +95,22 @@ describe("storage", () => { ]); }); }); + + describe("handling of errors", () => { + it("should pop operation if 400 code is returned", async () => { + const recordId = record.record_id; + await storage.addRecord(recordId); + + const error = new AxiosError("BAD_REQUEST_AXIOS"); + error.response = { status: 400, data: { message: "bad request" } }; + axios.__createMock.patch.mockRejectedValueOnce(error); + + storage.addSyncOperation( + recordId, + new SyncOperation(SyncOperation.ACTIONS.RECORD_INFO, { version_name: "Version test au nom changé" }), + ); + + expect(storage.sync).rejects.toThrow("BAD_REQUEST_AXIOS"); + }); + }); });