From 755103f4031d780ac1434d6d309037851a3dcf49 Mon Sep 17 00:00:00 2001 From: sunlei Date: Sun, 17 May 2026 19:28:52 +0800 Subject: [PATCH] =?UTF-8?q?fix(api):=20=E4=BF=AE=E5=A4=8DWordPress?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E5=BC=82=E5=B8=B8=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/wordpress/wordpress.service.ts | 39 +++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/wordpress/wordpress.service.ts b/src/wordpress/wordpress.service.ts index 20ad5b2..25d9e1d 100644 --- a/src/wordpress/wordpress.service.ts +++ b/src/wordpress/wordpress.service.ts @@ -1,4 +1,4 @@ -import { HttpStatus, Injectable } from '@nestjs/common'; +import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import type { Request, Response as ExpressResponse } from 'express'; import { throwVbenError } from '@/common'; @@ -366,11 +366,15 @@ export class WordpressService { throwVbenError('WordPress 请求失败', HttpStatus.BAD_GATEWAY); } catch (err) { + if (err instanceof HttpException) { + throw err; + } + if (err instanceof Error && err.name === 'AbortError') { throwVbenError('WordPress 请求超时', HttpStatus.GATEWAY_TIMEOUT); } - throw err; + this.throwWordpressNetworkError(err); } finally { clearTimeout(timer); } @@ -441,11 +445,15 @@ export class WordpressService { signal: controller.signal, }); } catch (err) { + if (err instanceof HttpException) { + throw err; + } + if (err instanceof Error && err.name === 'AbortError') { throwVbenError('WordPress 请求超时', HttpStatus.GATEWAY_TIMEOUT); } - throw err; + this.throwWordpressNetworkError(err); } finally { clearTimeout(timer); } @@ -616,6 +624,31 @@ export class WordpressService { return `WordPress 请求失败:${status}`; } + private throwWordpressNetworkError(err: unknown): never { + const message = err instanceof Error ? err.message : '未知错误'; + const cause = this.getErrorCause(err); + + // fetch 的 DNS、连接拒绝等底层异常不是 HttpException,需要统一转成业务响应。 + return throwVbenError( + cause + ? `WordPress 网络请求失败:${message}(${cause})` + : `WordPress 网络请求失败:${message}`, + HttpStatus.BAD_GATEWAY, + { + code: cause || 'WORDPRESS_NETWORK_ERROR', + message, + name: err instanceof Error ? err.name : 'Error', + }, + ); + } + + private getErrorCause(err: unknown) { + const cause = (err as { cause?: { code?: string; message?: string } }) + ?.cause; + + return cause?.code || cause?.message || ''; + } + private getLoginErrorMessage(html: string) { const match = html.match(/]*id=["']login_error["'][^>]*>([\s\S]*?)<\/div>/i);