bug fixes

This commit is contained in:
nnajah1 2025-04-28 16:54:26 +08:00
parent 87e66e730b
commit f3e987a0a7
8 changed files with 106 additions and 92 deletions

Binary file not shown.

View File

@ -379,10 +379,12 @@ export default async function API(endpoint, method, headers, params, body, onSuc
callLogs(response.data, "success", "frontend", subject, action)
onSuccess(response.data)
}).catch(async (err) => {
const hasResponse = err?.response && err?.response?.data;
const errorToResponse = {
status: err?.response?.data?.status || err?.response?.status,
message: err?.response?.data?.message || err?.response?.message || "Something went wrong, please try again."
}
status: hasResponse ? err.response.data.status || err.response.status : null,
message: hasResponse ? err.response.data.message || err.response.message : "Something went wrong, please try again."
};
if (errorToResponse.status === 401) {
let tokens = await DB.get("token");
@ -407,13 +409,13 @@ export default async function API(endpoint, method, headers, params, body, onSuc
}
}
})
callLogs(err?.response, "error", "frontend", subject, action)
onError(err?.response)
callLogs(err?.response || err, "error", "frontend", subject, action);
onError(err?.response); // Always use errorToResponse
return;
}
callLogs(err?.response, "error", "frontend", subject, action);
onError(errorToResponse)
callLogs(err?.response || err, "error", "frontend", subject, action);
onError(errorToResponse);
})
return;
}
@ -429,10 +431,13 @@ export default async function API(endpoint, method, headers, params, body, onSuc
callLogs(response.data, "success", "frontend", subject, action)
onSuccess(response.data)
}).catch(async (err) => {
const hasResponse = err?.response && err?.response?.data;
const errorToResponse = {
status: err?.response?.data?.status || err?.response?.status,
message: err?.response?.data?.message || err?.response?.message || "Something went wrong, please try again."
}
status: hasResponse ? err.response.data.status || err.response.status : null,
message: hasResponse ? err.response.data.message || err.response.message : "Something went wrong, please try again."
};
if (errorToResponse.status === 401) {
let tokens = await DB.get("token");
@ -457,13 +462,14 @@ export default async function API(endpoint, method, headers, params, body, onSuc
}
}
})
callLogs(err?.response, "error", "frontend", subject, action)
onError(err?.response)
callLogs(err?.response || err, "error", "frontend", subject, action);
onError(err?.response); // Always use errorToResponse
return;
}
callLogs(err?.response, "error", "frontend", subject, action);
onError(errorToResponse)
callLogs(err?.response || err, "error", "frontend", subject, action);
onError(errorToResponse);
})
return;
}
@ -482,10 +488,13 @@ export default async function API(endpoint, method, headers, params, body, onSuc
onSuccess(response.data)
}).catch(async (err) => {
console.log(err)
const hasResponse = err?.response && err?.response?.data;
const errorToResponse = {
status: err?.response?.data?.status || err?.response?.status,
message: err?.response?.data?.message || err?.response?.message || "Something went wrong, please try again."
}
status: hasResponse ? err.response.data.status || err.response.status : null,
message: hasResponse ? err.response.data.message || err.response.message : "Something went wrong, please try again."
};
if (errorToResponse.status === 401) {
let tokens = await DB.get("token");
@ -510,13 +519,13 @@ export default async function API(endpoint, method, headers, params, body, onSuc
}
}
})
callLogs(err?.response, "error", "frontend", subject, action)
onError(err?.response)
callLogs(err?.response || err, "error", "frontend", subject, action);
onError(err?.response); // Always use errorToResponse
return;
}
callLogs(err?.response, "error", "frontend", subject, action);
onError(errorToResponse)
callLogs(err?.response || err, "error", "frontend", subject, action);
onError(errorToResponse);
})
return;
}
@ -532,10 +541,13 @@ export default async function API(endpoint, method, headers, params, body, onSuc
callLogs(response.data, "success", "frontend", subject, action)
onSuccess(response.data)
}).catch(async (err) => {
const hasResponse = err?.response && err?.response?.data;
const errorToResponse = {
status: err?.response?.data?.status || err?.response?.status,
message: err?.response?.data?.message || err?.response?.message || "Something went wrong, please try again."
}
status: hasResponse ? err.response.data.status || err.response.status : null,
message: hasResponse ? err.response.data.message || err.response.message : "Something went wrong, please try again."
};
if (errorToResponse.status === 401) {
let tokens = await DB.get("token");
@ -560,13 +572,13 @@ export default async function API(endpoint, method, headers, params, body, onSuc
}
}
})
callLogs(err?.response, "error", "frontend", subject, action)
onError(err?.response)
callLogs(err?.response || err, "error", "frontend", subject, action);
onError(err?.response); // Always use errorToResponse
return;
}
callLogs(err?.response, "error", "frontend", subject, action);
onError(errorToResponse)
callLogs(err?.response || err, "error", "frontend", subject, action);
onError(errorToResponse);
})
return;
}
@ -585,10 +597,13 @@ export default async function API(endpoint, method, headers, params, body, onSuc
callLogs(response.data, "success", "frontend", subject, action)
onSuccess(response.data)
}).catch(async (err) => {
const hasResponse = err?.response && err?.response?.data;
const errorToResponse = {
status: err?.response?.data?.status || err?.response?.status,
message: err?.response?.data?.message || err?.response?.message || "Something went wrong, please try again."
}
status: hasResponse ? err.response.data.status || err.response.status : null,
message: hasResponse ? err.response.data.message || err.response.message : "Something went wrong, please try again."
};
if (errorToResponse.status === 401) {
let tokens = await DB.get("token");
@ -613,13 +628,13 @@ export default async function API(endpoint, method, headers, params, body, onSuc
}
}
})
callLogs(err?.response, "error", "frontend", subject, action)
onError(err?.response)
callLogs(err?.response || err, "error", "frontend", subject, action);
onError(err?.response); // Always use errorToResponse
return;
}
callLogs(err?.response, "error", "frontend", subject, action);
onError(errorToResponse)
callLogs(err?.response || err, "error", "frontend", subject, action);
onError(errorToResponse);
})
return;

View File

@ -7,24 +7,23 @@ const DBName = "@UnioilLoyaltyApp_";
export const get = async (key) => {
try {
const result = await AsyncStorage.getItem(DBName + key);
return result ? JSON.parse(result) : null;
const result = await AsyncStorage.getItem(DBName + key)
return typeof result == 'object' ? JSON.parse(result) : result
} catch (error) {
console.error(`AsyncStorage get error for key ${key}:`, error);
return null;
return error
}
}
};
export const set = async (key, data, onSuccess, onError) => {
try {
const stringData = JSON.stringify(data);
await AsyncStorage.setItem(DBName + key, stringData);
onSuccess?.();
const stringifiedData = typeof data === 'string' ? data : JSON.stringify(data);
const result = await AsyncStorage.setItem(DBName + key, stringifiedData);
await onSuccess(result);
} catch (error) {
console.error(`AsyncStorage set error for key ${key}:`, error);
onError?.(error);
await onError(error);
}
};
}
export const reset = async () => {
try{