fix(desktop): align haha-oauth handler with sibling error-handling convention

Code review follow-up on 142eb9cd:
- 加 outer try/catch + errorResponse wrapper (parity with providers.ts)
- 400 响应改用 ApiError.badRequest (shape 跟其他 handler 一致: {error: 'BAD_REQUEST', message})
- test 加上对 error shape 的断言

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-04-17 20:06:22 +08:00
parent 142eb9cd11
commit 7421db2857
2 changed files with 65 additions and 61 deletions

View File

@ -64,6 +64,8 @@ describe('POST /api/haha-oauth/start', () => {
const { req, url, segments } = buildReq('POST', '/api/haha-oauth/start', {}) const { req, url, segments } = buildReq('POST', '/api/haha-oauth/start', {})
const res = await handleHahaOAuthApi(req, url, segments) const res = await handleHahaOAuthApi(req, url, segments)
expect(res.status).toBe(400) expect(res.status).toBe(400)
const body = (await res.json()) as { error: string; message?: string }
expect(body.error).toBe('BAD_REQUEST')
}) })
}) })

View File

@ -10,6 +10,7 @@
import { z } from 'zod' import { z } from 'zod'
import { hahaOAuthService } from '../services/hahaOAuthService.js' import { hahaOAuthService } from '../services/hahaOAuthService.js'
import { ApiError, errorResponse } from '../middleware/errorHandler.js'
const StartRequestSchema = z.object({ const StartRequestSchema = z.object({
serverPort: z.number().int().positive(), serverPort: z.number().int().positive(),
@ -27,6 +28,7 @@ export async function handleHahaOAuthApi(
url: URL, url: URL,
segments: string[], segments: string[],
): Promise<Response> { ): Promise<Response> {
try {
const action = segments[2] // segments: ['api', 'haha-oauth', <action?>] const action = segments[2] // segments: ['api', 'haha-oauth', <action?>]
if (action === 'start' && req.method === 'POST') { if (action === 'start' && req.method === 'POST') {
@ -34,14 +36,11 @@ export async function handleHahaOAuthApi(
try { try {
body = await req.json() body = await req.json()
} catch { } catch {
return Response.json({ error: 'Invalid JSON body' }, { status: 400 }) throw ApiError.badRequest('Invalid JSON body')
} }
const parsed = StartRequestSchema.safeParse(body) const parsed = StartRequestSchema.safeParse(body)
if (!parsed.success) { if (!parsed.success) {
return Response.json( throw ApiError.badRequest('serverPort (positive integer) required')
{ error: 'serverPort (positive integer) required' },
{ status: 400 },
)
} }
const session = hahaOAuthService.startSession({ const session = hahaOAuthService.startSession({
serverPort: parsed.data.serverPort, serverPort: parsed.data.serverPort,
@ -92,6 +91,9 @@ export async function handleHahaOAuthApi(
} }
return Response.json({ error: 'Not Found' }, { status: 404 }) return Response.json({ error: 'Not Found' }, { status: 404 })
} catch (error) {
return errorResponse(error)
}
} }
function renderCallbackPage(success: boolean, errorMsg: string | null): string { function renderCallbackPage(success: boolean, errorMsg: string | null): string {