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"
@@ -1757,18 +1758,21 @@ func (h *Handler) sendResponse(w common.ResponseWriter, data interface{}, metada
}
func (h *Handler) sendError(w common.ResponseWriter, status int, code, message string, details interface{}) {
apiErr := &common.APIError{
Code: code,
Message: message,
Details: details,
Detail: fmt.Sprintf("%v", details),
}
if asErr, ok := details.(error); ok {
var sqlErr *common.SQLError
if errors.As(asErr, &sqlErr) {
apiErr.SQL = sqlErr.SQL
}
}
w.SetHeader("Content-Type", "application/json")
w.WriteHeader(status)
err := w.WriteJSON(common.Response{
Success: false,
Error: &common.APIError{
Code: code,
Message: message,
Details: details,
Detail: fmt.Sprintf("%v", details),
},
})
if err != nil {
if err := w.WriteJSON(common.Response{Success: false, Error: apiErr}); err != nil {
logger.Error("Error sending response: %v", err)
}
}