游标不匹配
CURSOR_MISMATCH 与 HTTP 400 一起返回。游标是有效的,但您的请求不是游标所对应的那次搜索:排序方式或过滤条件发生了变化。
含义
游标属于某一次搜索。API 会记住排序方式以及过滤条件的指纹。在下一页时,它会把这些与您的请求进行比较。如果不同,游标就无法继续那次搜索,API 会以这个错误应答。
翻页之间只有 cursor、limit 和 include 可以改变。其他一切都必须与第一页完全相同地再次发送。
响应示例
第一页使用 "query": "ticker:NVDA" 请求。第二页使用它的游标,但查询不同:
cURL
curl -X POST 'https://api.finlight.me/v3/articles/search' \
-H 'Content-Type: application/json' \
-H 'X-API-KEY: YOUR_API_KEY' \
-H 'X-Request-Id: req-123' \
-d '{ "query": "ticker:AMD", "limit": 1, "cursor": "eyJ2IjoxLCJr..." }'
Response (400): filters changed
{
"type": "https://docs.finlight.me/errors/cursor-mismatch",
"title": "Cursor mismatch",
"status": 400,
"detail": "The cursor was issued for a different set of filters. Start a new search without a cursor.",
"code": "CURSOR_MISMATCH",
"requestId": "req-123",
"errors": [
{ "field": "cursor", "issue": "The cursor was issued for a different set of filters. Start a new search without a cursor." }
]
}
当排序方式发生变化时,detail 会列出两种排序:
Response (400): sort order changed
{
"type": "https://docs.finlight.me/errors/cursor-mismatch",
"title": "Cursor mismatch",
"status": 400,
"detail": "The cursor was issued for sort order 'publishedAt:desc', the request asks for 'publishedAt:asc'.",
"code": "CURSOR_MISMATCH",
"requestId": "req-123",
"errors": [
{ "field": "cursor", "issue": "The cursor was issued for sort order 'publishedAt:desc', the request asks for 'publishedAt:asc'." }
]
}
常见原因
- 在后续页上使用了不同的
orderBy或order。 - 修改了过滤条件,例如不同的
query、tickers或sources。 - 在后续页上添加
to。 一个常见的错误是在每一页都把to设为当前时间。不带to的第一页和带to的第二页是不同的搜索。请不要传to,或保持第一页的确切值。 - 添加默认值。 缺少某个字段与使用其默认值不同。如果第一页没有
languages,那么第二页上的"languages": ["en"]就是另一次搜索,尽管["en"]是默认值。只有orderBy和order会在填入默认值后再比较,所以在第 2 页添加"order": "desc"没有问题。 - 取值的写法不同。 取值不会被规范化:
["EN"]不等于["en"]。
列表中取值的顺序无关紧要,重复的取值也无关紧要。["de", "en"] 和 ["en", "de"] 是同一个过滤条件。
如何修正
- 保存第一页的请求体。
- 对于之后的每一页,再次发送这个请求体,只添加或修改
cursor、limit或include。 - 如果您确实需要不同的过滤条件或排序,请不带
cursor开始一次新的搜索。
客户端库会替您完成这些:searchAll(Python:search_all)会在调用时冻结请求,并在后续页中只添加游标。请参阅分页。
重试
不要重试。相同的请求会再次失败。客户端库永远不会重试它,searchAll 会以这个错误停止。
在 SDK 中
| 语言 | 错误 |
|---|---|
| TypeScript、Python、Ruby | CursorMismatchError |
| .NET、Java、PHP | CursorMismatchException |
| Go | *finlight.APIError,其中 APIKind == finlight.APIKindCursorMismatch |
| Rust | Error::Api,其中 api_kind() == ApiErrorKind::CursorMismatch |
让库替您翻页
for await (const article of client.articles.searchAll({ query: 'ticker:NVDA', limit: 100 }, { maxPages: 5 })) {
console.log(article.id, article.title)
}