Skip to content

Return HTTP errors as a structured type - #16

Merged
utahta merged 1 commit into
mainfrom
api-error
Aug 16, 2026
Merged

utahta merged 1 commit into
mainfrom
api-error

Conversation

@utahta

@utahta utahta commented Aug 16, 2026

Copy link
Copy Markdown
Owner

APIがエラーステータスを返した際に *client.APIError を返すようにする。従来はフォーマット済み文字列だったため、利用側がステータスに応じて処理を分けるにはメッセージの文字列解析が必要だった(エラーボディに含まれる銘柄コード等の数字列が 401 や 429 と誤って一致する)。

  • client.APIError がステータスコードと生のボディを保持する。各サービスは %w でラップしているため、サービスメソッド経由でも errors.As で取り出せる
  • Error() の書式は従来と完全に同一(API error: status=%d, body=%s)で、テストで固定
  • 判定用ヘルパー IsRateLimitExceeded(429)、IsAuthError(401/403)、IsServerError(5xx)、StatusCode を追加。403はAPIキー不正と契約プラン外アクセスの双方で返るため認証エラー側に含めている

Non-200 responses were reported as a formatted string, so callers had to
parse the message to branch on the status: wait and retry on 429, abort
on an auth error. Matching on the message is fragile, since an error body
can contain a stock code or wording that happens to include 401 or 429.

*client.APIError carries the status code and the raw body. Services
already wrap errors with %w, so errors.As reaches it from any service
method. Error() keeps the exact previous format for callers that still
match on the message.
@utahta
utahta merged commit dd18d02 into main Aug 16, 2026
4 checks passed
@utahta
utahta deleted the api-error branch August 16, 2026 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant