UNPKG

@tasolutions/express-core

Version:
48 lines (41 loc) 1.9 kB
'use strict'; const axios = require('axios'); const { clientInfo } = require('../../config'); const { sendMessage } = require('../discord'); module.exports = { doRequest: async (method = 'GET', path, payload = {}) => { if (clientInfo.userService.enable !== 'true') { console.warn('[UserService][doRequest] Client is disabled. Request execution skipped.'); return null; } if (!path) throw { message: '[UserService] doRequest field: path is required!' }; let headers = { 'Content-Type': 'application/json' }; const url = `${clientInfo.userService.endpoint}${path}`; console.log(`[UserService][doRequest] Sending ${method} request to ${url}`); // Log thông tin yêu cầu const req = method === 'GET' ? { url, method, headers } : { url, method, headers, data: payload }; try { const response = await axios(req); return response.data; } catch (error) { // Ghi log lỗi và chuẩn bị thông điệp để gửi đến Discord console.error('[UserService] Error received in doRequest:', { message: error.message, response: error.response?.data, // Dữ liệu phản hồi từ server config: error.config, // Cấu hình yêu cầu }); const errorDetails = { message: error.message, response: error.response ? error.response.data : null, config: error.config, }; // Gửi thông điệp lỗi đến Discord sendMessage('[UserService][ERROR] req', JSON.stringify(req, null, 2)); sendMessage('[UserService][ERROR] error', JSON.stringify(errorDetails, null, 2)); // Gửi chi tiết lỗi return false; } } };