Skip to content

Commit

Permalink
Merge pull request #492 from AgenceBio/fix/gestion_erreur_400
Browse files Browse the repository at this point in the history
fix: Dépile les requetes en erreur 400
  • Loading branch information
GregoireDucharme authored Jan 7, 2025
2 parents 572eb24 + c8d3f63 commit c6c4346
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
Expand Down
29 changes: 28 additions & 1 deletion src/stores/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -454,6 +456,10 @@ export const useCartoBioStorage = defineStore("storage", () => {
continue;
}

if (e.response?.status === 400) {
delete syncQueues.value[recordId];
}

throw e;
}
}
Expand All @@ -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
Expand Down
20 changes: 19 additions & 1 deletion src/stores/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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");
});
});
});

0 comments on commit c6c4346

Please sign in to comment.