RFC 9457 (Problem Details for HTTP APIs) のGo実装ライブラリです。
HTTP APIのエラーレスポンスを標準化された application/problem+json 形式で返すためのライブラリです。
go get golang-rfc9457import (
"net/http"
"golang-rfc9457/pkg/rfc9457"
)
// 基本的なエラー
p := rfc9457.New(http.StatusNotFound,
rfc9457.WithDetail("指定されたユーザーが見つかりません"),
rfc9457.WithInstance("/users/12345"),
)
// HTTPレスポンスとして書き込み
rfc9457.WriteProblem(w, p)p := rfc9457.New(http.StatusForbidden,
rfc9457.WithType("https://api.example.com/problems/out-of-credit"),
rfc9457.WithTitle("クレジット不足"),
rfc9457.WithDetail("現在の残高は30ですが、この操作には50が必要です"),
rfc9457.WithExtension("balance", 30),
rfc9457.WithExtension("required", 50),
)errors := []rfc9457.ValidationError{
{Field: "email", Code: "INVALID_FORMAT", Message: "メールアドレスの形式が正しくありません"},
{Field: "password", Code: "TOO_SHORT", Message: "パスワードは8文字以上必要です"},
}
p := rfc9457.NewValidationError("/api/users", errors)
rfc9457.WriteProblem(w, p)p := rfc9457.NewRateLimitError(
"/api/search", // instance
100, // limit
0, // remaining
"2025-01-27T15:00:00Z", // reset_at
3600, // retry_after (秒)
)
rfc9457.WriteProblem(w, p)
// Retry-After ヘッダーも自動的に設定されます{
"type": "https://api.example.com/problems/out-of-credit",
"status": 403,
"title": "クレジット不足",
"detail": "現在の残高は30ですが、この操作には50が必要です",
"instance": "/account/12345",
"balance": 30,
"required": 50
}| フィールド | 説明 |
|---|---|
type |
問題タイプを識別するURI(デフォルト: about:blank) |
status |
HTTPステータスコード |
title |
問題タイプの短い要約 |
detail |
この発生に固有の説明 |
instance |
問題発生を識別するURI |
上記以外のフィールドは拡張メンバーとして自由に追加できます。
# サーバー起動
go run ./cmd/server/
# または環境変数でポート指定
PORT=9090 go run ./cmd/server/| パス | 説明 |
|---|---|
GET / |
サーバー情報 |
GET /health |
ヘルスチェック |
GET /demo/not-found |
404エラーのデモ |
GET /demo/validation-error |
バリデーションエラーのデモ |
GET /demo/rate-limit |
レート制限エラーのデモ |
GET /demo/internal-error |
内部エラーのデモ |
./check.sh以下を実行します
go fmt- フォーマットgo vet- 静的解析staticcheck- 追加静的解析golangci-lint- 総合リンターgo test- テスト(カバレッジ80%以上必須)go build- ビルド確認
./scripts/e2e_test.shDDD + Clean Architectureで設計されています。
├── cmd/server/ # エントリーポイント
├── internal/
│ ├── domain/problem/ # ドメイン層
│ ├── usecase/problem/ # ユースケース層
│ ├── adapter/
│ │ ├── handler/ # HTTPハンドラー
│ │ └── presenter/ # レスポンス整形
│ └── infrastructure/server/ # HTTPサーバー
├── pkg/rfc9457/ # 公開API
└── testutil/ # テストユーティリティMIT License