|
1 | | -import { CommandExecutionError } from '@jackwener/opencli/errors'; |
| 1 | +import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors'; |
| 2 | +import { unwrapEvaluateResult } from './evaluate-result.js'; |
| 3 | + |
| 4 | +function isAuthLikeError(code, message) { |
| 5 | + const text = String(message ?? ''); |
| 6 | + return code === 401 || code === 403 || /login|cookie|auth|captcha|verify|forbidden|permission|登录|登陆|权限|验证|验证码/i.test(text); |
| 7 | +} |
| 8 | + |
2 | 9 | /** |
3 | 10 | * Execute a fetch() call inside the Chrome browser context via page.evaluate. |
4 | 11 | * This ensures a_bogus signing and cookies are handled automatically by the browser. |
5 | 12 | */ |
6 | 13 | export async function browserFetch(page, method, url, options = {}) { |
7 | 14 | const js = ` |
8 | 15 | (async () => { |
9 | | - const res = await fetch(${JSON.stringify(url)}, { |
10 | | - method: ${JSON.stringify(method)}, |
11 | | - credentials: 'include', |
12 | | - headers: { |
13 | | - 'Content-Type': 'application/json', |
14 | | - ...${JSON.stringify(options.headers ?? {})} |
15 | | - }, |
16 | | - ${options.body ? `body: JSON.stringify(${JSON.stringify(options.body)}),` : ''} |
17 | | - }); |
18 | | - const text = await res.text(); |
19 | | - if (!text) return null; |
20 | | - return JSON.parse(text); |
| 16 | + const controller = new AbortController(); |
| 17 | + const timer = setTimeout(() => controller.abort(), ${Number(options.timeoutMs ?? 30000)}); |
| 18 | + try { |
| 19 | + const res = await fetch(${JSON.stringify(url)}, { |
| 20 | + method: ${JSON.stringify(method)}, |
| 21 | + credentials: 'include', |
| 22 | + signal: controller.signal, |
| 23 | + headers: { |
| 24 | + 'Content-Type': 'application/json', |
| 25 | + ...${JSON.stringify(options.headers ?? {})} |
| 26 | + }, |
| 27 | + ${options.body ? `body: JSON.stringify(${JSON.stringify(options.body)}),` : ''} |
| 28 | + }); |
| 29 | + const text = await res.text(); |
| 30 | + try { |
| 31 | + return JSON.parse(text); |
| 32 | + } catch (error) { |
| 33 | + return { status_code: res.ok ? -2 : res.status, status_msg: \`JSON parse failed: \${text.slice(0, 500) || String(error && error.message || error)}\` }; |
| 34 | + } |
| 35 | + } catch (error) { |
| 36 | + return { status_code: -1, status_msg: String(error && error.message || error) }; |
| 37 | + } finally { |
| 38 | + clearTimeout(timer); |
| 39 | + } |
21 | 40 | })() |
22 | 41 | `; |
23 | 42 | let result; |
24 | 43 | try { |
25 | | - result = await page.evaluate(js); |
| 44 | + result = unwrapEvaluateResult(await page.evaluate(js)); |
26 | 45 | } |
27 | 46 | catch (error) { |
28 | | - const message = error instanceof Error ? error.message : String(error); |
29 | | - throw new CommandExecutionError(`Douyin API request failed: ${message}`); |
| 47 | + throw new CommandExecutionError(`Douyin API request failed (${method} ${url}): ${error instanceof Error ? error.message : String(error)}`); |
| 48 | + } |
| 49 | + if (result == null) { |
| 50 | + throw new CommandExecutionError(`Empty response from Douyin API (${method} ${url})`); |
30 | 51 | } |
31 | | - if (result === null || result === undefined) { |
32 | | - throw new CommandExecutionError('Empty response from Douyin API'); |
| 52 | + if (Array.isArray(result) || typeof result !== 'object') { |
| 53 | + throw new CommandExecutionError(`Malformed response from Douyin API (${method} ${url})`); |
33 | 54 | } |
34 | 55 | if (result && typeof result === 'object' && 'status_code' in result) { |
35 | 56 | const code = result.status_code; |
36 | 57 | if (code !== 0) { |
37 | | - const msg = result.status_msg ?? 'unknown error'; |
38 | | - throw new CommandExecutionError(`Douyin API error ${code}: ${msg}`); |
| 58 | + const msg = result.status_msg ?? result.message ?? 'unknown error'; |
| 59 | + if (isAuthLikeError(code, msg)) { |
| 60 | + throw new AuthRequiredError('creator.douyin.com', `Douyin API auth/permission error ${code} at ${method} ${url}: ${msg}`); |
| 61 | + } |
| 62 | + throw new CommandExecutionError(`Douyin API error ${code} at ${method} ${url}: ${msg}`); |
39 | 63 | } |
40 | 64 | } |
41 | 65 | return result; |
|
0 commit comments