refactor: enhance error handling and logging in API requests
This commit is contained in:
@@ -100,11 +100,25 @@ const createDevice = async (queryParams) => {
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('createDevice full response:', response);
|
||||
// Return full response with statusCode
|
||||
console.log('createDevice payload sent:', queryParams);
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [device_object] }
|
||||
// Check if response is empty array (error from SendRequest)
|
||||
if (Array.isArray(response) && response.length === 0) {
|
||||
return {
|
||||
statusCode: 500,
|
||||
data: null,
|
||||
message: 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Extract first item from data array
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
};
|
||||
|
||||
@@ -115,11 +129,25 @@ const updateDevice = async (device_id, queryParams) => {
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('updateDevice full response:', response);
|
||||
// Return full response with statusCode
|
||||
console.log('updateDevice payload sent:', queryParams);
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [device_object] }
|
||||
// Check if response is empty array (error from SendRequest)
|
||||
if (Array.isArray(response) && response.length === 0) {
|
||||
return {
|
||||
statusCode: 500,
|
||||
data: null,
|
||||
message: 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Extract first item from data array
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
};
|
||||
|
||||
@@ -129,11 +157,12 @@ const deleteDevice = async (queryParams) => {
|
||||
prefix: `device/${queryParams}`,
|
||||
});
|
||||
console.log('deleteDevice full response:', response);
|
||||
// Return full response with statusCode
|
||||
// Backend returns: { statusCode, message, rows: null, data: true }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
178
src/api/master-plant-section.jsx
Normal file
178
src/api/master-plant-section.jsx
Normal file
@@ -0,0 +1,178 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllPlantSection = async (queryParams) => {
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `plant-sub-section?${queryParams.toString()}`,
|
||||
});
|
||||
console.log('getAllPlantSection response:', response);
|
||||
console.log('Query params:', queryParams.toString());
|
||||
|
||||
// Backend response structure:
|
||||
// {
|
||||
// statusCode: 200,
|
||||
// data: [...plantSections],
|
||||
// paging: {
|
||||
// current_page: 1,
|
||||
// current_limit: 10,
|
||||
// total_limit: 50,
|
||||
// total_page: 5
|
||||
// }
|
||||
// }
|
||||
|
||||
// Check if backend returns paginated data
|
||||
if (response.paging) {
|
||||
const totalData = response.data?.[0]?.total_data || response.rows || response.data?.length || 0;
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: {
|
||||
page: response.paging.current_page || 1,
|
||||
limit: response.paging.current_limit || 10,
|
||||
total: totalData,
|
||||
page_total: response.paging.total_page || Math.ceil(totalData / (response.paging.current_limit || 10))
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback: If backend returns all data without pagination (old behavior)
|
||||
const params = Object.fromEntries(queryParams);
|
||||
const currentPage = parseInt(params.page) || 1;
|
||||
const currentLimit = parseInt(params.limit) || 10;
|
||||
|
||||
const allData = response.data || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
// Client-side pagination
|
||||
const startIndex = (currentPage - 1) * currentLimit;
|
||||
const endIndex = startIndex + currentLimit;
|
||||
const paginatedData = allData.slice(startIndex, endIndex);
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: paginatedData,
|
||||
paging: {
|
||||
page: currentPage,
|
||||
limit: currentLimit,
|
||||
total: totalData,
|
||||
page_total: Math.ceil(totalData / currentLimit)
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('getAllPlantSection error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const getPlantSectionById = async (id) => {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `plant-sub-section/${id}`,
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const createPlantSection = async (queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'post',
|
||||
prefix: `plant-sub-section`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('createPlantSection full response:', response);
|
||||
console.log('createPlantSection payload sent:', queryParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [plantSection_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
};
|
||||
|
||||
const updatePlantSection = async (plant_section_id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `plant-sub-section/${plant_section_id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('updatePlantSection full response:', response);
|
||||
console.log('updatePlantSection payload sent:', queryParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [plantSection_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
};
|
||||
|
||||
const deletePlantSection = async (queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'delete',
|
||||
prefix: `plant-sub-section/${queryParams}`,
|
||||
});
|
||||
console.log('deletePlantSection full response:', response);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows: null, data: true }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
};
|
||||
|
||||
export { getAllPlantSection, getPlantSectionById, createPlantSection, updatePlantSection, deletePlantSection };
|
||||
185
src/api/master-tag.jsx
Normal file
185
src/api/master-tag.jsx
Normal file
@@ -0,0 +1,185 @@
|
||||
import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllTag = async (queryParams) => {
|
||||
try {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `tags?${queryParams.toString()}`,
|
||||
});
|
||||
console.log('getAllTag response:', response);
|
||||
console.log('Query params:', queryParams.toString());
|
||||
|
||||
// Check if response has error
|
||||
if (response.error) {
|
||||
console.error('getAllTag error response:', response);
|
||||
return {
|
||||
status: response.statusCode || 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: response.message
|
||||
};
|
||||
}
|
||||
|
||||
// Check if backend returns paginated data
|
||||
if (response.paging) {
|
||||
const totalData = response.data?.[0]?.total_data || response.rows || response.data?.length || 0;
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: {
|
||||
page: response.paging.current_page || 1,
|
||||
limit: response.paging.current_limit || 10,
|
||||
total: totalData,
|
||||
page_total: response.paging.total_page || Math.ceil(totalData / (response.paging.current_limit || 10))
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback: If backend returns all data without pagination (old behavior)
|
||||
const params = Object.fromEntries(queryParams);
|
||||
const currentPage = parseInt(params.page) || 1;
|
||||
const currentLimit = parseInt(params.limit) || 10;
|
||||
|
||||
const allData = response.data || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
// Client-side pagination
|
||||
const startIndex = (currentPage - 1) * currentLimit;
|
||||
const endIndex = startIndex + currentLimit;
|
||||
const paginatedData = allData.slice(startIndex, endIndex);
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: paginatedData,
|
||||
paging: {
|
||||
page: currentPage,
|
||||
limit: currentLimit,
|
||||
total: totalData,
|
||||
page_total: Math.ceil(totalData / currentLimit)
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('getAllTag catch error:', error);
|
||||
return {
|
||||
status: 500,
|
||||
data: {
|
||||
data: [],
|
||||
paging: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 0,
|
||||
page_total: 0
|
||||
},
|
||||
total: 0
|
||||
},
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const getTagById = async (id) => {
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `tags/${id}`,
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const createTag = async (queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'post',
|
||||
prefix: `tags`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('createTag full response:', response);
|
||||
console.log('createTag payload sent:', queryParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [tag_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
};
|
||||
|
||||
const updateTag = async (tag_id, queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'put',
|
||||
prefix: `tags/${tag_id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('updateTag full response:', response);
|
||||
console.log('updateTag payload sent:', queryParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows, data: [tag_object] }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data?.[0] || response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
};
|
||||
|
||||
const deleteTag = async (queryParams) => {
|
||||
const response = await SendRequest({
|
||||
method: 'delete',
|
||||
prefix: `tags/${queryParams}`,
|
||||
});
|
||||
console.log('deleteTag full response:', response);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
return {
|
||||
statusCode: response.statusCode || 500,
|
||||
data: null,
|
||||
message: response.message || 'Request failed',
|
||||
rows: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Backend returns: { statusCode, message, rows: null, data: true }
|
||||
return {
|
||||
statusCode: response.statusCode || 200,
|
||||
data: response.data,
|
||||
message: response.message,
|
||||
rows: response.rows
|
||||
};
|
||||
};
|
||||
|
||||
export { getAllTag, getTagById, createTag, updateTag, deleteTag };
|
||||
Reference in New Issue
Block a user