锁定移动壳网络状态失败响应

移动端 network.status 读取失败返回稳定 host_error 响应

移动壳网络测试覆盖 Expo Network 异常不透出原生消息

移动壳配置检查反查网络状态失败边界
This commit is contained in:
2026-06-20 13:54:19 +08:00
parent 1016472e2e
commit 1e19ebb4d9
3 changed files with 45 additions and 8 deletions
+25 -2
View File
@@ -2227,6 +2227,27 @@ for (const snippet of [
}
}
for (const snippet of [
'try {',
'return ok(request, await getMobileNetworkStatus())',
'return failure(request, {',
"code: 'host_error'",
"message: 'network status unavailable'",
]) {
if (!hostBridgeNetworkSource.includes(snippet)) {
throw new Error(`mobile shell network HostBridge must stabilize native failures: ${snippet}`);
}
}
for (const snippet of [
'converts native network failures to a stable host_error response',
"new Error('network query failed')",
"message: 'network status unavailable'",
]) {
if (!hostBridgeNetworkTestSource.includes(snippet)) {
throw new Error(`mobile shell network tests must cover stable native failure responses: ${snippet}`);
}
}
if (
!hapticsSource.includes('normalizeHostBridgeHapticsImpactStyle(rawStyle)') ||
!hapticsSource.includes('type HapticsImpactPayload') ||
@@ -2670,7 +2691,8 @@ if (
for (const snippet of [
'getMobileHostBridgeNetworkStatus',
'request: HostBridgeRequest',
'ok(request, await getMobileNetworkStatus())',
'return ok(request, await getMobileNetworkStatus())',
'return failure(request, {',
'getMobileNetworkStatus()',
]) {
if (!hostBridgeNetworkSource.includes(snippet)) {
@@ -2680,10 +2702,11 @@ for (const snippet of [
for (const snippet of [
'wraps Expo Network status in the HostBridge response shape',
'normalizes disconnected network state before wrapping response',
'lets protocol layer convert native network failures to host errors',
'converts native network failures to a stable host_error response',
'connectionType: \'cellular\'',
'connectionType: \'none\'',
'network query failed',
'network status unavailable',
]) {
if (!hostBridgeNetworkTestSource.includes(snippet)) {
throw new Error(`mobile shell network tests missing ${snippet}`);
@@ -71,13 +71,20 @@ describe('mobile HostBridge network helper', () => {
});
});
test('lets protocol layer convert native network failures to host errors', async () => {
test('converts native network failures to a stable host_error response', async () => {
vi.mocked(Network.getNetworkStateAsync).mockRejectedValue(
new Error('network query failed'),
);
await expect(getMobileHostBridgeNetworkStatus(request())).rejects.toThrow(
'network query failed',
);
await expect(getMobileHostBridgeNetworkStatus(request())).resolves.toEqual({
bridge: HOST_BRIDGE_PROTOCOL,
version: HOST_BRIDGE_VERSION,
id: 'network-request',
ok: false,
error: {
code: 'host_error',
message: 'network status unavailable',
},
});
});
});
+9 -2
View File
@@ -1,9 +1,16 @@
import { getMobileNetworkStatus } from '../shell/network';
import { type HostBridgeRequest } from '../../../../packages/shared/src/contracts/hostBridge';
import { ok } from './protocol';
import { failure, ok } from './protocol';
export async function getMobileHostBridgeNetworkStatus(
request: HostBridgeRequest,
) {
return ok(request, await getMobileNetworkStatus());
try {
return ok(request, await getMobileNetworkStatus());
} catch {
return failure(request, {
code: 'host_error',
message: 'network status unavailable',
});
}
}