|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This package provides structured error types for the go-openapi/go-swagger ecosystem. |
| 8 | +It defines an `Error` interface (with an HTTP status code and message) and concrete types |
| 9 | +for validation failures, parsing errors, authentication errors, and HTTP middleware errors. |
| 10 | +These error types are consumed by validators, spec loaders, and API runtimes to report |
| 11 | +problems back to API consumers in a structured, JSON-serializable way. |
| 12 | + |
| 13 | +See [docs/MAINTAINERS.md](../docs/MAINTAINERS.md) for CI/CD, release process, and repo structure details. |
| 14 | + |
| 15 | +### Package layout |
| 16 | + |
| 17 | +| File | Contents | |
| 18 | +|------|----------| |
| 19 | +| `doc.go` | Package-level godoc | |
| 20 | +| `api.go` | Core `Error` interface, `apiError` struct, `New`/`NotFound`/`NotImplemented`/`MethodNotAllowed` constructors, `ServeError` HTTP handler, `CompositeError` flattening | |
| 21 | +| `schema.go` | `Validation` struct, `CompositeError` struct, and ~30 constructor functions for JSON Schema / OpenAPI validation failures (type, required, enum, min/max, pattern, etc.); error code constants (`InvalidTypeCode`, `RequiredFailCode`, ...) | |
| 22 | +| `headers.go` | `Validation` constructors for HTTP header errors: `InvalidContentType`, `InvalidResponseFormat` | |
| 23 | +| `parsing.go` | `ParseError` struct and `NewParseError` constructor for parameter parsing failures | |
| 24 | +| `auth.go` | `Unauthenticated` constructor (401) | |
| 25 | +| `middleware.go` | `APIVerificationFailed` struct for mismatches between spec and registered handlers | |
| 26 | + |
| 27 | +### Key API |
| 28 | + |
| 29 | +- `Error` interface -- `error` + `Code() int32` |
| 30 | +- `New(code, message, args...)` -- general-purpose error constructor |
| 31 | +- `NotFound` / `NotImplemented` / `MethodNotAllowed` / `Unauthenticated` -- HTTP error constructors |
| 32 | +- `Validation` struct -- carries `Name`, `In`, `Value` context for validation failures |
| 33 | +- ~30 validation constructors: `Required`, `InvalidType`, `TooLong`, `TooShort`, `EnumFail`, `ExceedsMaximum`, `FailedPattern`, `DuplicateItems`, `TooManyItems`, `TooFewItems`, `PropertyNotAllowed`, `CompositeValidationError`, etc. |
| 34 | +- `CompositeError` -- groups multiple errors; implements `Unwrap() []error` |
| 35 | +- `ServeError(rw, r, err)` -- HTTP handler that serializes any `Error` to JSON |
| 36 | +- `ParseError` -- parsing failure with `Name`, `In`, `Value`, `Reason` |
| 37 | + |
| 38 | +### Dependencies |
| 39 | + |
| 40 | +- `github.com/go-openapi/testify/v2` -- test-only assertions (zero-dep testify fork) |
| 41 | + |
| 42 | +This package has **zero runtime dependencies** outside the Go standard library. |
| 43 | + |
| 44 | +### Notable design decisions |
| 45 | + |
| 46 | +- **Error codes above 599 are domain codes, not HTTP codes** -- validation error codes (`InvalidTypeCode = 600`, `RequiredFailCode = 601`, ...) use values >= 600 so they can be distinguished from real HTTP status codes. `ServeError` maps any code >= 600 to `DefaultHTTPCode` (422). |
| 47 | +- **`Validation.ValidateName` mutates in place** -- callers chain `.ValidateName(name)` to prepend parent property names for nested validation errors, building dotted paths like `address.street`. |
| 48 | +- **All error types implement `json.Marshaler`** -- errors serialize to structured JSON with `code`, `message`, and type-specific fields (`name`, `in`, `value`), not just a string. |
| 49 | +- **`CompositeError` flattens recursively** -- `ServeError` flattens nested `CompositeError` trees and serves only the first leaf error to avoid overwhelming API consumers. |
0 commit comments