Documentation
¶
Overview ¶
PostgreSQL error mapping: one blank import turns the driver's errors into 404, 409 and 400.
rest/errors does not link the PostgreSQL driver any more, so the default map of errors.NewOptions() covers database/sql's ErrNoRows and nothing else. Applications on github.com/kataras/pg add one blank import and get the driver mapping back: pg.ErrNoRows to 404, duplicate-key violations to 409 (raw error logged, never sent) and SQLSTATE-tagged input errors to 400 with the SQLSTATE noise trimmed. The import registers an errors.RegisterErrorResolver in its init; nothing else in the code changes.
No database is opened here: the handlers hand the driver's errors to errors.HandleError directly, exactly as a repository would after a failed query. Resolvers run inside HandleError even when no Options middleware is registered, so the /pg routes below carry none.
Learn: map a database driver's errors to HTTP statuses with one blank import and no code changes in your handlers.
Run:
go run main.go
Try:
curl -i http://localhost:8080/pg/no-rows # 404 NOT_FOUND, "resource not found" curl -i http://localhost:8080/pg/duplicate # 409 ALREADY_EXISTS, generic detail curl -i http://localhost:8080/pg/syntax # 400 INVALID_ARGUMENT, SQLSTATE trimmed curl -i http://localhost:8080/sql/no-rows # 404, the default map, no pgerrors needed
Next: ../pg-crud