Small Go demo project showing secure error handling in a layered application:
- HTTP handlers translate service errors into safe JSON responses.
- Services validate input and map repository failures to domain-safe error codes.
- Repository code keeps low-level SQLite details away from API clients.
This project is based on the JetBrains GoLand post on secure error handling in Go:
go mod tidy
go run ./cmd/apicurl -i http://localhost:8080/health
curl -i -H 'X-User-ID: alice' \
-H 'Content-Type: application/json' \
-d '{"title":"Plans","content":"internal notes"}' \
http://localhost:8080/documents
curl -i -H 'X-User-ID: bob' http://localhost:8080/documents/1
curl -i -H 'X-User-ID: alice' http://localhost:8080/demo/internal-errorThe /demo/internal-error route is intentionally unsafe internally and demonstrates that:
- clients only receive a generic
INTERNALresponse with a request ID - logs keep the internal cause for debugging