Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Dépile les requetes en erreur 400 #491

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
});
});
Loading