improve error handling for 401 requests and fetch

This commit is contained in:
Gregor Vostrak
2024-06-01 00:12:03 +02:00
parent a64ee87d19
commit bae4265f70
13 changed files with 305 additions and 249 deletions

View File

@@ -24,6 +24,7 @@ async function invitePlaceholder(id: string) {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.invitePlaceholder( api.invitePlaceholder(
{}, {},
{ {

View File

@@ -10,6 +10,7 @@ import { getCurrentOrganizationId } from '@/utils/useUser';
import type { ImportReport, ImportType } from '@/utils/api'; import type { ImportReport, ImportType } from '@/utils/api';
import DialogModal from '@/Components/DialogModal.vue'; import DialogModal from '@/Components/DialogModal.vue';
import SecondaryButton from '@/Components/SecondaryButton.vue'; import SecondaryButton from '@/Components/SecondaryButton.vue';
import { initializeStores } from '@/utils/init';
const importTypeOptions = ref<ImportType[]>([]); const importTypeOptions = ref<ImportType[]>([]);
const { addNotification } = useNotificationsStore(); const { addNotification } = useNotificationsStore();
@@ -50,8 +51,9 @@ async function importData() {
if (organizationId !== null) { if (organizationId !== null) {
const { handleApiRequestNotifications } = useNotificationsStore(); const { handleApiRequestNotifications } = useNotificationsStore();
reportResult.value = await handleApiRequestNotifications( reportResult.value = await handleApiRequestNotifications(() => {
api.importData( if (importType.value) {
return api.importData(
{ {
type: importType.value.key, type: importType.value.key,
data: base64String, data: base64String,
@@ -61,8 +63,12 @@ async function importData() {
organization: organizationId, organization: organizationId,
}, },
} }
)
); );
}
return new Promise((resolve, reject) => {
reject('Import type is null');
});
});
if (reportResult.value) { if (reportResult.value) {
showResultModal.value = true; showResultModal.value = true;
} }

View File

@@ -37,13 +37,23 @@ export const useNotificationsStore = defineStore('notifications', () => {
} }
} }
async function fetchToken() {
return new Promise((resolve) => {
router.reload({
onFinish: () => {
resolve(null);
},
});
});
}
async function handleApiRequestNotifications<T>( async function handleApiRequestNotifications<T>(
apiRequest: Promise<T>, apiRequest: () => Promise<T>,
successMessage?: string, successMessage?: string,
errorMessage?: string errorMessage?: string
) { ) {
try { try {
const response = await apiRequest; const response = await apiRequest();
if (successMessage) { if (successMessage) {
addNotification('success', successMessage); addNotification('success', successMessage);
} }
@@ -65,7 +75,16 @@ export const useNotificationsStore = defineStore('notifications', () => {
const message = error.response.data.message; const message = error.response.data.message;
addNotification('error', message); addNotification('error', message);
} else if (error?.response?.status === 401) { } else if (error?.response?.status === 401) {
await fetchToken();
try {
const response = await apiRequest();
if (successMessage) {
addNotification('success', successMessage);
}
return response;
} catch (error) {
router.get(route('login')); router.get(route('login'));
}
} else { } else {
addNotification( addNotification(
'error', 'error',

View File

@@ -17,6 +17,7 @@ export const useClientsStore = defineStore('clients', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
clientResponse.value = await handleApiRequestNotifications( clientResponse.value = await handleApiRequestNotifications(
() =>
api.getClients({ api.getClients({
params: { params: {
organization: organization, organization: organization,
@@ -34,6 +35,7 @@ export const useClientsStore = defineStore('clients', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
const response = await handleApiRequestNotifications( const response = await handleApiRequestNotifications(
() =>
api.createClient(clientBody, { api.createClient(clientBody, {
params: { params: {
organization: organization, organization: organization,
@@ -51,6 +53,7 @@ export const useClientsStore = defineStore('clients', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.deleteClient( api.deleteClient(
{}, {},
{ {

View File

@@ -89,6 +89,7 @@ export const useCurrentTimeEntryStore = defineStore('currentTimeEntry', () => {
? currentTimeEntry.value.start ? currentTimeEntry.value.start
: dayjs().utc().format(); : dayjs().utc().format();
const response = await handleApiRequestNotifications( const response = await handleApiRequestNotifications(
() =>
api.createTimeEntry( api.createTimeEntry(
{ {
member_id: membership, member_id: membership,
@@ -119,6 +120,7 @@ export const useCurrentTimeEntryStore = defineStore('currentTimeEntry', () => {
if (organization) { if (organization) {
const currentDateTime = dayjs().utc().format(); const currentDateTime = dayjs().utc().format();
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.updateTimeEntry( api.updateTimeEntry(
{ {
user_id: user, user_id: user,
@@ -147,6 +149,7 @@ export const useCurrentTimeEntryStore = defineStore('currentTimeEntry', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
const response = await handleApiRequestNotifications( const response = await handleApiRequestNotifications(
() =>
api.updateTimeEntry( api.updateTimeEntry(
{ {
description: currentTimeEntry.value.description, description: currentTimeEntry.value.description,

View File

@@ -17,6 +17,7 @@ export const useInvitationsStore = defineStore('invitations', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
invitationsResponse.value = await handleApiRequestNotifications( invitationsResponse.value = await handleApiRequestNotifications(
() =>
api.getInvitations({ api.getInvitations({
params: { params: {
organization: organization, organization: organization,
@@ -34,6 +35,7 @@ export const useInvitationsStore = defineStore('invitations', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.invite(inviteBody, { api.invite(inviteBody, {
params: { params: {
organization: organization, organization: organization,

View File

@@ -13,6 +13,7 @@ export const useMembersStore = defineStore('members', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
membersResponse.value = await handleApiRequestNotifications( membersResponse.value = await handleApiRequestNotifications(
() =>
api.getMembers({ api.getMembers({
params: { params: {
organization: organization, organization: organization,
@@ -28,6 +29,7 @@ export const useMembersStore = defineStore('members', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.removeMember( api.removeMember(
{}, {},
{ {

View File

@@ -17,6 +17,7 @@ export const useProjectMembersStore = defineStore('project-members', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
projectMemberResponse.value = await handleApiRequestNotifications( projectMemberResponse.value = await handleApiRequestNotifications(
() =>
api.getProjectMembers({ api.getProjectMembers({
params: { params: {
organization: organization, organization: organization,
@@ -36,6 +37,7 @@ export const useProjectMembersStore = defineStore('project-members', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.createProjectMember(projectMemberBody, { api.createProjectMember(projectMemberBody, {
params: { params: {
organization: organization, organization: organization,
@@ -56,6 +58,7 @@ export const useProjectMembersStore = defineStore('project-members', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.deleteProjectMember( api.deleteProjectMember(
{}, {},
{ {

View File

@@ -17,6 +17,7 @@ export const useProjectsStore = defineStore('projects', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
projectResponse.value = await handleApiRequestNotifications( projectResponse.value = await handleApiRequestNotifications(
() =>
api.getProjects({ api.getProjects({
params: { params: {
organization: organization, organization: organization,
@@ -32,6 +33,7 @@ export const useProjectsStore = defineStore('projects', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.createProject(projectBody, { api.createProject(projectBody, {
params: { params: {
organization: organization, organization: organization,
@@ -49,6 +51,7 @@ export const useProjectsStore = defineStore('projects', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.deleteProject( api.deleteProject(
{}, {},
{ {
@@ -72,6 +75,7 @@ export const useProjectsStore = defineStore('projects', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.updateProject(updateProjectBody, { api.updateProject(updateProjectBody, {
params: { params: {
organization: organizationId, organization: organizationId,

View File

@@ -39,6 +39,7 @@ export const useReportingStore = defineStore('reporting', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
reportingGraphResponse.value = await handleApiRequestNotifications( reportingGraphResponse.value = await handleApiRequestNotifications(
() =>
api.getAggregatedTimeEntries({ api.getAggregatedTimeEntries({
params: { params: {
organization: organization, organization: organization,
@@ -57,6 +58,7 @@ export const useReportingStore = defineStore('reporting', () => {
const organization = getCurrentOrganizationId(); const organization = getCurrentOrganizationId();
if (organization) { if (organization) {
reportingTableResponse.value = await handleApiRequestNotifications( reportingTableResponse.value = await handleApiRequestNotifications(
() =>
api.getAggregatedTimeEntries({ api.getAggregatedTimeEntries({
params: { params: {
organization: organization, organization: organization,

View File

@@ -12,6 +12,7 @@ export const useTagsStore = defineStore('tags', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
const response = await handleApiRequestNotifications( const response = await handleApiRequestNotifications(
() =>
api.getTags({ api.getTags({
params: { params: {
organization: organizationId, organization: organizationId,
@@ -34,6 +35,7 @@ export const useTagsStore = defineStore('tags', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.deleteTag( api.deleteTag(
{}, {},
{ {
@@ -54,6 +56,7 @@ export const useTagsStore = defineStore('tags', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
const response = await handleApiRequestNotifications( const response = await handleApiRequestNotifications(
() =>
api.createTag( api.createTag(
{ {
name: name, name: name,

View File

@@ -12,7 +12,7 @@ export const useTasksStore = defineStore('tasks', () => {
async function fetchTasks() { async function fetchTasks() {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
const tasksResponse = await handleApiRequestNotifications( const tasksResponse = await handleApiRequestNotifications(() =>
api.getTasks({ api.getTasks({
params: { params: {
organization: organizationId, organization: organizationId,
@@ -29,6 +29,7 @@ export const useTasksStore = defineStore('tasks', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.updateTask(task, { api.updateTask(task, {
params: { params: {
organization: organizationId, organization: organizationId,
@@ -45,6 +46,7 @@ export const useTasksStore = defineStore('tasks', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.createTask(task, { api.createTask(task, {
params: { params: {
organization: organizationId, organization: organizationId,
@@ -61,6 +63,7 @@ export const useTasksStore = defineStore('tasks', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.deleteTask( api.deleteTask(
{}, {},
{ {

View File

@@ -21,6 +21,7 @@ export const useTimeEntriesStore = defineStore('timeEntries', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
const timeEntriesResponse = await handleApiRequestNotifications( const timeEntriesResponse = await handleApiRequestNotifications(
() =>
api.getTimeEntries({ api.getTimeEntries({
params: { params: {
organization: organizationId, organization: organizationId,
@@ -47,6 +48,7 @@ export const useTimeEntriesStore = defineStore('timeEntries', () => {
dayjs(latestTimeEntry.start).utc().format('YYYY-MM-DD'); dayjs(latestTimeEntry.start).utc().format('YYYY-MM-DD');
const timeEntriesResponse = await handleApiRequestNotifications( const timeEntriesResponse = await handleApiRequestNotifications(
() =>
api.getTimeEntries({ api.getTimeEntries({
params: { params: {
organization: organizationId, organization: organizationId,
@@ -77,6 +79,7 @@ export const useTimeEntriesStore = defineStore('timeEntries', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.updateTimeEntry(timeEntry, { api.updateTimeEntry(timeEntry, {
params: { params: {
organization: organizationId, organization: organizationId,
@@ -100,6 +103,7 @@ export const useTimeEntriesStore = defineStore('timeEntries', () => {
member_id: memberId, member_id: memberId,
} as CreateTimeEntryBody; } as CreateTimeEntryBody;
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.createTimeEntry(newTimeEntry, { api.createTimeEntry(newTimeEntry, {
params: { params: {
organization: organizationId, organization: organizationId,
@@ -116,6 +120,7 @@ export const useTimeEntriesStore = defineStore('timeEntries', () => {
const organizationId = getCurrentOrganizationId(); const organizationId = getCurrentOrganizationId();
if (organizationId) { if (organizationId) {
await handleApiRequestNotifications( await handleApiRequestNotifications(
() =>
api.deleteTimeEntry( api.deleteTimeEntry(
{}, {},
{ {