Skip to content

Commit 6c39ef5

Browse files
committed
tmp-fix(rest): directly pass pg-error to HTTP response
Signed-off-by: hmoazzem <[email protected]>
1 parent 85191ec commit 6c39ef5

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ func Load(cfgFile string) (*Config, error) {
9494
}
9595

9696
// Version is the current version of the application
97-
var Version = `v0.0.1-experimental-2`
97+
var Version = `v0.0.1-experimental` // should be overridden by build process, ldflags

pkg/pgx/schema/schema.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Package schema provides functionality for caching PostgreSQL objects' metadata
2+
// eg schema of tables / views, function def etc.
3+
// It monitors schema changes via notifications and maintains an in-memory representation
4+
// of tables, columns, and relationships that can be efficiently queried.
15
package schema
26

37
import (

pkg/rest/server.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,32 @@ func (s *Server) handleDelete(w http.ResponseWriter, r *http.Request, table sche
168168
}
169169

170170
func (s *Server) executeQuery(w http.ResponseWriter, r *http.Request, query string, args []any) {
171-
_, conn, err := httputil.ConnWithRole(r)
172-
if err != nil {
173-
httputil.Error(w, http.StatusInternalServerError, err.Error())
171+
_, conn, pgErr := httputil.ConnWithRole(r)
172+
if pgErr != nil {
173+
httputil.Error(w, http.StatusInternalServerError, pgErr.Error())
174174
return
175175
}
176176
defer conn.Release()
177177

178-
rows, pgErr := conn.Query(r.Context(), query, args...)
179-
if pgErr != nil {
180-
log.Printf("Query error: %v", pgErr)
181-
httputil.Error(w, http.StatusInternalServerError, "Database query error")
178+
pgRole, ok := r.Context().Value(httputil.OIDCRoleClaimCtxKey).(string)
179+
if !ok || pgRole == "" {
180+
log.Println("pgrole not found in OIDC claims")
181+
httputil.Error(w, http.StatusUnauthorized, "pgrole not found in OIDC claims")
182+
return
183+
}
184+
185+
rows, err := conn.Query(r.Context(), query, args...)
186+
if err != nil {
187+
log.Printf("TODO - map pg-err to http status: query error: %+v", err)
188+
httputil.Error(w, http.StatusInternalServerError, fmt.Sprintf("%s pgrole: %s", err.Error(), pgRole)) // debug
182189
return
183190
}
184191
defer rows.Close()
185192

186-
results, pgErr := pgx.CollectRows(rows, pgx.RowToMap)
187-
if pgErr != nil {
188-
httputil.Error(w, http.StatusInternalServerError, "Error collecting results")
193+
results, err := pgx.CollectRows(rows, pgx.RowToMap)
194+
if err != nil {
195+
log.Printf("TODO - map pg-err to http status: parse error: %v", err)
196+
httputil.Error(w, http.StatusInternalServerError, fmt.Sprintf("%s pgrole: %s", err.Error(), pgRole)) // debug
189197
return
190198
}
191199

0 commit comments

Comments
 (0)