|
| 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 is a shared errors library for the go-openapi toolkit. It provides an `Error` interface and concrete error types for API errors and JSON-schema validation errors. The package is used throughout the go-openapi ecosystem (github.com/go-openapi). |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +### Testing |
| 12 | +```bash |
| 13 | +# Run all tests |
| 14 | +go test ./... |
| 15 | + |
| 16 | +# Run tests with coverage |
| 17 | +go test -v -race -coverprofile=coverage.out ./... |
| 18 | + |
| 19 | +# Run a specific test |
| 20 | +go test -v -run TestName ./... |
| 21 | +``` |
| 22 | + |
| 23 | +### Linting |
| 24 | +```bash |
| 25 | +# Run golangci-lint (must be run before committing) |
| 26 | +golangci-lint run |
| 27 | +``` |
| 28 | + |
| 29 | +### Building |
| 30 | +```bash |
| 31 | +# Build the package |
| 32 | +go build ./... |
| 33 | + |
| 34 | +# Verify dependencies |
| 35 | +go mod verify |
| 36 | +go mod tidy |
| 37 | +``` |
| 38 | + |
| 39 | +## Architecture and Code Structure |
| 40 | + |
| 41 | +### Core Error Types |
| 42 | + |
| 43 | +The package provides a hierarchy of error types: |
| 44 | + |
| 45 | +1. **Error interface** (api.go:20-24): Base interface with `Code() int32` method that all errors implement |
| 46 | +2. **apiError** (api.go:26-37): Simple error with code and message |
| 47 | +3. **CompositeError** (schema.go:94-122): Groups multiple errors together, implements `Unwrap() []error` |
| 48 | +4. **Validation** (headers.go:12-55): Represents validation failures with Name, In, Value fields |
| 49 | +5. **ParseError** (parsing.go:12-42): Represents parsing errors with Reason field |
| 50 | +6. **MethodNotAllowedError** (api.go:74-88): Special error for method not allowed with Allowed methods list |
| 51 | +7. **APIVerificationFailed** (middleware.go:12-39): Error for API spec/registration mismatches |
| 52 | + |
| 53 | +### Error Categorization by File |
| 54 | + |
| 55 | +- **api.go**: Core error interface, basic error types, HTTP error serving |
| 56 | +- **schema.go**: Validation errors (type, length, pattern, enum, min/max, uniqueness, properties) |
| 57 | +- **headers.go**: Header validation errors (content-type, accept) |
| 58 | +- **parsing.go**: Parameter parsing errors |
| 59 | +- **auth.go**: Authentication errors |
| 60 | +- **middleware.go**: API verification errors |
| 61 | + |
| 62 | +### Key Design Patterns |
| 63 | + |
| 64 | +1. **Error Codes**: Custom error codes >= 600 (maximumValidHTTPCode) to differentiate validation types without conflicting with HTTP status codes |
| 65 | +2. **Conditional Messages**: Most constructors have "NoIn" variants for errors without an "In" field (e.g., tooLongMessage vs tooLongMessageNoIn) |
| 66 | +3. **ServeError Function** (api.go:147-201): Central HTTP error handler using type assertions to handle different error types |
| 67 | +4. **Flattening**: CompositeError flattens nested composite errors recursively (api.go:108-134) |
| 68 | +5. **Name Validation**: Errors can have their Name field updated for nested properties via ValidateName methods |
| 69 | + |
| 70 | +### JSON Serialization |
| 71 | + |
| 72 | +All error types implement `MarshalJSON()` to provide structured JSON responses with code, message, and type-specific fields. |
| 73 | + |
| 74 | +## Testing Practices |
| 75 | + |
| 76 | +- Uses forked `github.com/go-openapi/testify/v2` for minimal test dependencies |
| 77 | +- Tests follow pattern: `*_test.go` files next to implementation |
| 78 | +- Test files cover: api_test.go, schema_test.go, middleware_test.go, parsing_test.go, auth_test.go |
| 79 | + |
| 80 | +## Code Quality Standards |
| 81 | + |
| 82 | +### Linting Configuration |
| 83 | +- Enable all golangci-lint linters by default, with specific exclusions in .golangci.yml |
| 84 | +- Complexity threshold: max 20 (cyclop, gocyclo) |
| 85 | +- Line length: max 180 characters |
| 86 | +- Run `golangci-lint run` before committing |
| 87 | + |
| 88 | +### Disabled Linters (and why) |
| 89 | +Key exclusions from STYLE.md rationale: |
| 90 | +- depguard: No import constraints enforced |
| 91 | +- funlen: Function length not enforced (cognitive complexity preferred) |
| 92 | +- godox: TODOs are acceptable |
| 93 | +- nonamedreturns: Named returns are acceptable |
| 94 | +- varnamelen: Short variable names allowed when appropriate |
| 95 | + |
| 96 | +## Release Process |
| 97 | + |
| 98 | +- Push semver tag (v{major}.{minor}.{patch}) to master branch |
| 99 | +- CI automatically generates release with git-cliff |
| 100 | +- Tags should be PGP-signed |
| 101 | +- Tag message prepends release notes |
| 102 | + |
| 103 | +## Important Constants |
| 104 | + |
| 105 | +- `DefaultHTTPCode = 422` (http.StatusUnprocessableEntity) |
| 106 | +- `maximumValidHTTPCode = 600` |
| 107 | +- Custom error codes start at 600+ (InvalidTypeCode, RequiredFailCode, etc.) |
0 commit comments