fix(api): 修复WordPress网络异常响应

This commit is contained in:
sunlei 2026-05-17 19:28:52 +08:00
parent c8541facd4
commit 755103f403

View File

@ -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(/<div[^>]*id=["']login_error["'][^>]*>([\s\S]*?)<\/div>/i);