fix: better error detail for failed sql
Some checks failed
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Failing after -35m9s
Build , Vet Test, and Lint / Build (push) Failing after -35m9s
Tests / Unit Tests (push) Failing after -35m10s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Failing after -35m9s
Build , Vet Test, and Lint / Lint Code (push) Failing after -35m9s
Tests / Integration Tests (push) Failing after -35m10s

This commit is contained in:
Hein
2026-05-20 13:06:26 +02:00
parent 0647a88aba
commit c42d09238f
9 changed files with 101 additions and 32 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
@@ -579,8 +580,8 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
// preload LEFT JOIN (to prevent "table name specified more than once" errors).
if len(options.CustomSQLJoin) > 0 {
preloadAliasSet := make(map[string]bool, len(options.Preload))
for _, p := range options.Preload {
if alias := common.RelationPathToBunAlias(p.Relation); alias != "" {
for i := range options.Preload {
if alias := common.RelationPathToBunAlias(options.Preload[i].Relation); alias != "" {
preloadAliasSet[alias] = true
}
}
@@ -2645,6 +2646,12 @@ func (h *Handler) sendError(w common.ResponseWriter, statusCode int, code, messa
"_error": errorMsg,
"_retval": 1,
}
var sqlErr *common.SQLError
if errors.As(err, &sqlErr) {
response["_sql"] = sqlErr.SQL
}
w.SetHeader("Content-Type", "application/json")
w.WriteHeader(statusCode)
if jsonErr := w.WriteJSON(response); jsonErr != nil {