fix(sdk): handle api error response code correctly and improve missing token hints
- Fixed an issue where the request function silently resolved on HTTP 200 even when the JSON body contained an API error (e.g. code 401). Now explicitly throws when code != 200. - Improved error messages when OpenAPI or Notify tokens are completely missing, explicitly instructing the user to configure them in the panel.
This commit is contained in:
@@ -8,7 +8,7 @@ const { URL } = require('url');
|
|||||||
function request(urlStr, method = 'GET', data = null) {
|
function request(urlStr, method = 'GET', data = null) {
|
||||||
const token = process.env.BHPKG_OPENAPI_TOKEN || process.env.OPENAPI_TOKEN || process.env.BHPKG_NOTIFY_TOKEN;
|
const token = process.env.BHPKG_OPENAPI_TOKEN || process.env.OPENAPI_TOKEN || process.env.BHPKG_NOTIFY_TOKEN;
|
||||||
if (!token) {
|
if (!token) {
|
||||||
throw new Error("缺少 BHPKG_OPENAPI_TOKEN 或 BHPKG_NOTIFY_TOKEN 环境变量,无法使用环境管理操作");
|
throw new Error("没有正确配置或缺少 BHPKG_OPENAPI_TOKEN 等环境变量,无法使用环境管理操作");
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsedUrl = new URL(urlStr);
|
const parsedUrl = new URL(urlStr);
|
||||||
@@ -41,7 +41,12 @@ function request(urlStr, method = 'GET', data = null) {
|
|||||||
res.on('end', () => {
|
res.on('end', () => {
|
||||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
try {
|
try {
|
||||||
resolve(body ? JSON.parse(body) : {});
|
const parsed = body ? JSON.parse(body) : {};
|
||||||
|
if (parsed && typeof parsed === 'object' && parsed.code !== undefined && parsed.code !== 200) {
|
||||||
|
reject(new Error(`请求失败 [${parsed.code}]: ${parsed.msg || parsed.message || '未知错误'}`));
|
||||||
|
} else {
|
||||||
|
resolve(parsed);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
resolve(body);
|
resolve(body);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ function notify(title, text, channelId) {
|
|||||||
const missing = [];
|
const missing = [];
|
||||||
if (!token) missing.push("BHPKG_NOTIFY_TOKEN");
|
if (!token) missing.push("BHPKG_NOTIFY_TOKEN");
|
||||||
if (!channel) missing.push("BHPKG_NOTIFY_CHANNEL");
|
if (!channel) missing.push("BHPKG_NOTIFY_CHANNEL");
|
||||||
throw new Error(`缺少必要的环境变量以使用 notify 函数: ${missing.join(", ")}。请在白虎面板的任务设置中配置这些 Key。`);
|
throw new Error(`没有正确配置或缺少 ${missing.join(" 和 ")} 环境变量以使用 notify 函数。请在白虎面板的任务设置中配置这些 Key。`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const notifyUrl = process.env.BHPKG_NOTIFY_URL || 'http://localhost:8052/api/v1/notify/send';
|
const notifyUrl = process.env.BHPKG_NOTIFY_URL || 'http://localhost:8052/api/v1/notify/send';
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ const { URL } = require('url');
|
|||||||
function request(urlStr, method = 'GET', data = null) {
|
function request(urlStr, method = 'GET', data = null) {
|
||||||
const token = process.env.BHPKG_OPENAPI_TOKEN || process.env.OPENAPI_TOKEN || process.env.BHPKG_NOTIFY_TOKEN;
|
const token = process.env.BHPKG_OPENAPI_TOKEN || process.env.OPENAPI_TOKEN || process.env.BHPKG_NOTIFY_TOKEN;
|
||||||
if (!token) {
|
if (!token) {
|
||||||
throw new Error("缺少 BHPKG_OPENAPI_TOKEN 或 BHPKG_NOTIFY_TOKEN 环境变量,无法使用任务管理操作");
|
throw new Error("没有正确配置或缺少 BHPKG_OPENAPI_TOKEN 等环境变量,无法使用任务管理操作");
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsedUrl = new URL(urlStr);
|
const parsedUrl = new URL(urlStr);
|
||||||
@@ -41,7 +41,12 @@ function request(urlStr, method = 'GET', data = null) {
|
|||||||
res.on('end', () => {
|
res.on('end', () => {
|
||||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
try {
|
try {
|
||||||
resolve(body ? JSON.parse(body) : {});
|
const parsed = body ? JSON.parse(body) : {};
|
||||||
|
if (parsed && typeof parsed === 'object' && parsed.code !== undefined && parsed.code !== 200) {
|
||||||
|
reject(new Error(`请求失败 [${parsed.code}]: ${parsed.msg || parsed.message || '未知错误'}`));
|
||||||
|
} else {
|
||||||
|
resolve(parsed);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
resolve(body);
|
resolve(body);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import urllib.error
|
|||||||
def _get_headers():
|
def _get_headers():
|
||||||
token = os.environ.get("BHPKG_OPENAPI_TOKEN") or os.environ.get("OPENAPI_TOKEN") or os.environ.get("BHPKG_NOTIFY_TOKEN")
|
token = os.environ.get("BHPKG_OPENAPI_TOKEN") or os.environ.get("OPENAPI_TOKEN") or os.environ.get("BHPKG_NOTIFY_TOKEN")
|
||||||
if not token:
|
if not token:
|
||||||
raise RuntimeError("缺少 BHPKG_OPENAPI_TOKEN 或 BHPKG_NOTIFY_TOKEN 环境变量,无法使用环境管理操作")
|
raise RuntimeError("没有正确配置或缺少 BHPKG_OPENAPI_TOKEN 等环境变量,无法使用环境管理操作")
|
||||||
return {
|
return {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Bearer {token}"
|
"Authorization": f"Bearer {token}"
|
||||||
@@ -36,7 +36,11 @@ def _request(url, method="GET", data=None):
|
|||||||
body = resp.read().decode("utf-8")
|
body = resp.read().decode("utf-8")
|
||||||
if not body:
|
if not body:
|
||||||
return {}
|
return {}
|
||||||
return json.loads(body)
|
parsed = json.loads(body)
|
||||||
|
if isinstance(parsed, dict) and parsed.get("code") is not None and parsed.get("code") != 200:
|
||||||
|
msg = parsed.get("msg") or parsed.get("message") or "未知错误"
|
||||||
|
raise RuntimeError(f"请求失败 [{parsed.get('code')}]: {msg}")
|
||||||
|
return parsed
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
err_body = e.read().decode("utf-8")
|
err_body = e.read().decode("utf-8")
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import urllib.error
|
|||||||
def _get_headers():
|
def _get_headers():
|
||||||
token = os.environ.get("BHPKG_OPENAPI_TOKEN") or os.environ.get("OPENAPI_TOKEN") or os.environ.get("BHPKG_NOTIFY_TOKEN")
|
token = os.environ.get("BHPKG_OPENAPI_TOKEN") or os.environ.get("OPENAPI_TOKEN") or os.environ.get("BHPKG_NOTIFY_TOKEN")
|
||||||
if not token:
|
if not token:
|
||||||
raise RuntimeError("缺少 BHPKG_OPENAPI_TOKEN 或 BHPKG_NOTIFY_TOKEN 环境变量,无法使用任务管理操作")
|
raise RuntimeError("没有正确配置或缺少 BHPKG_OPENAPI_TOKEN 等环境变量,无法使用任务管理操作")
|
||||||
return {
|
return {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": f"Bearer {token}"
|
"Authorization": f"Bearer {token}"
|
||||||
@@ -41,7 +41,11 @@ def _request(url, method="GET", data=None):
|
|||||||
body = resp.read().decode("utf-8")
|
body = resp.read().decode("utf-8")
|
||||||
if not body:
|
if not body:
|
||||||
return {}
|
return {}
|
||||||
return json.loads(body)
|
parsed = json.loads(body)
|
||||||
|
if isinstance(parsed, dict) and parsed.get("code") is not None and parsed.get("code") != 200:
|
||||||
|
msg = parsed.get("msg") or parsed.get("message") or "未知错误"
|
||||||
|
raise RuntimeError(f"请求失败 [{parsed.get('code')}]: {msg}")
|
||||||
|
return parsed
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
err_body = e.read().decode("utf-8")
|
err_body = e.read().decode("utf-8")
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user