fix: log unexpected bird failures
This commit is contained in:
@@ -118,4 +118,37 @@ describe('searchPage', () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('logs unexpected details without returning them', async () => {
|
||||||
|
const client = reader()
|
||||||
|
const privateDetail = 'TASK4_PRIVATE_RELAY_MARKER_9e0b'
|
||||||
|
const upstreamError = new Error(privateDetail)
|
||||||
|
vi.mocked(client.getAllSearchResults).mockRejectedValue(upstreamError)
|
||||||
|
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await searchPage(client, {
|
||||||
|
query: 'AI',
|
||||||
|
product: 'Latest',
|
||||||
|
following: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(errorSpy).toHaveBeenCalledTimes(1)
|
||||||
|
expect(errorSpy).toHaveBeenCalledWith(
|
||||||
|
'Unexpected Bird failure:',
|
||||||
|
upstreamError,
|
||||||
|
)
|
||||||
|
expect(result).toEqual({
|
||||||
|
ok: false,
|
||||||
|
error: {
|
||||||
|
code: 'upstream',
|
||||||
|
message: 'X から投稿を取得できませんでした。',
|
||||||
|
retryable: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
expect(JSON.stringify(result)).not.toContain(privateDetail)
|
||||||
|
} finally {
|
||||||
|
errorSpy.mockRestore()
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -41,7 +41,13 @@ const failure = (
|
|||||||
retryable: boolean,
|
retryable: boolean,
|
||||||
): LoadResult => ({ ok: false, error: { code, message, retryable } })
|
): LoadResult => ({ ok: false, error: { code, message, retryable } })
|
||||||
|
|
||||||
function upstreamFailure(message = ''): LoadResult {
|
function upstreamFailure(diagnostic: unknown = ''): LoadResult {
|
||||||
|
const message =
|
||||||
|
diagnostic instanceof Error
|
||||||
|
? diagnostic.message
|
||||||
|
: typeof diagnostic === 'string'
|
||||||
|
? diagnostic
|
||||||
|
: ''
|
||||||
const lower = message.toLowerCase()
|
const lower = message.toLowerCase()
|
||||||
if (lower.includes('timeout') || lower.includes('aborted')) {
|
if (lower.includes('timeout') || lower.includes('aborted')) {
|
||||||
return failure('timeout', '取得がタイムアウトしました。', true)
|
return failure('timeout', '取得がタイムアウトしました。', true)
|
||||||
@@ -56,6 +62,7 @@ function upstreamFailure(message = ''): LoadResult {
|
|||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
console.error('Unexpected Bird failure:', diagnostic)
|
||||||
return failure('upstream', 'X から投稿を取得できませんでした。', true)
|
return failure('upstream', 'X から投稿を取得できませんでした。', true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +111,7 @@ export async function loadUserPage(
|
|||||||
if (error instanceof InputError) {
|
if (error instanceof InputError) {
|
||||||
return failure('invalid-input', error.message, false)
|
return failure('invalid-input', error.message, false)
|
||||||
}
|
}
|
||||||
return upstreamFailure(error instanceof Error ? error.message : '')
|
return upstreamFailure(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,6 +137,6 @@ export async function searchPage(
|
|||||||
if (error instanceof InputError) {
|
if (error instanceof InputError) {
|
||||||
return failure('invalid-input', error.message, false)
|
return failure('invalid-input', error.message, false)
|
||||||
}
|
}
|
||||||
return upstreamFailure(error instanceof Error ? error.message : '')
|
return upstreamFailure(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,17 @@ import { searchPageInputSchema, userPageInputSchema } from './inputs'
|
|||||||
import { loadUserPage, searchPage } from './post-service'
|
import { loadUserPage, searchPage } from './post-service'
|
||||||
import type { LoadResult } from './types'
|
import type { LoadResult } from './types'
|
||||||
|
|
||||||
const configFailure = (): LoadResult => ({
|
const configFailure = (diagnostic: unknown): LoadResult => {
|
||||||
ok: false,
|
console.error('Bird client configuration failed:', diagnostic)
|
||||||
error: {
|
return {
|
||||||
code: 'relay-config',
|
ok: false,
|
||||||
message: 'TWITTER_RELAY_BASE_URL を設定してください。',
|
error: {
|
||||||
retryable: false,
|
code: 'relay-config',
|
||||||
},
|
message: 'TWITTER_RELAY_BASE_URL を設定してください。',
|
||||||
})
|
retryable: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function reader() {
|
async function reader() {
|
||||||
const { getBirdReader } = await import('./bird-client.server')
|
const { getBirdReader } = await import('./bird-client.server')
|
||||||
@@ -22,8 +25,8 @@ export const loadUserPosts = createServerFn({ method: 'GET' })
|
|||||||
.handler(async ({ data }) => {
|
.handler(async ({ data }) => {
|
||||||
try {
|
try {
|
||||||
return await loadUserPage(await reader(), data)
|
return await loadUserPage(await reader(), data)
|
||||||
} catch {
|
} catch (error) {
|
||||||
return configFailure()
|
return configFailure(error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -32,7 +35,7 @@ export const searchPosts = createServerFn({ method: 'GET' })
|
|||||||
.handler(async ({ data }) => {
|
.handler(async ({ data }) => {
|
||||||
try {
|
try {
|
||||||
return await searchPage(await reader(), data)
|
return await searchPage(await reader(), data)
|
||||||
} catch {
|
} catch (error) {
|
||||||
return configFailure()
|
return configFailure(error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user