From b7e27b6588eb16e8e7ea8318ae7037a901e68901 Mon Sep 17 00:00:00 2001 From: anggakrnwn Date: Tue, 18 Nov 2025 00:54:51 +0700 Subject: [PATCH 1/2] fix: add error handling for json.Unmarshal and json.Marshal calls --- builder.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/builder.go b/builder.go index 97c8a3e..78bbda1 100644 --- a/builder.go +++ b/builder.go @@ -261,10 +261,9 @@ func (b *Builder[T]) Execute(ctx context.Context) (*PostgrestResponse[T], error) if _, ok := any(zeroT).(string); ok { response.Data = any(strData).(T) } else { - // If T is not string, try to unmarshal normally - if err := json.Unmarshal(bodyBytes, &response.Data); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) - } + // If T is not string, for plan text we can't unmarshal as JSON + // Just leave Data as zero value - this is expected behavior for non-string types + // The response body is available as plain text but can't be unmarshaled into non-string T } } else if len(bodyBytes) > 0 { acceptHeader := b.headers.Get("Accept") @@ -319,9 +318,24 @@ func (b *Builder[T]) Execute(ctx context.Context) (*PostgrestResponse[T], error) response.Data = *new(T) } } else { - // Not an array, unmarshal directly - if err := json.Unmarshal(bodyBytes, &response.Data); err != nil { - return nil, fmt.Errorf("error unmarshaling response: %w", err) + // Not an array, it's a single object + // Check if T is a slice type - if so, wrap the object in an array + var zeroT T + tType := reflect.TypeOf(zeroT) + if tType != nil && tType.Kind() == reflect.Slice { + // T is a slice type, wrap single object in array + var arrJSON []byte + arrJSON = append(arrJSON, '[') + arrJSON = append(arrJSON, bodyBytes...) + arrJSON = append(arrJSON, ']') + if err := json.Unmarshal(arrJSON, &response.Data); err != nil { + return nil, fmt.Errorf("error unmarshaling single object array: %w", err) + } + } else { + // T is not a slice, unmarshal directly + if err := json.Unmarshal(bodyBytes, &response.Data); err != nil { + return nil, fmt.Errorf("error unmarshaling response: %w", err) + } } } } else { From abab7b618cd27f3fc32a25f1c0803753fc25ef64 Mon Sep 17 00:00:00 2001 From: Angga Kurniawan <135822142+anggakrnwn@users.noreply.github.com> Date: Tue, 18 Nov 2025 01:35:10 +0700 Subject: [PATCH 2/2] Add error handling for JSON operations Improved error handling for JSON marshaling and unmarshaling. --- builder.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/builder.go b/builder.go index f2859f5..8331ead 100644 --- a/builder.go +++ b/builder.go @@ -306,8 +306,15 @@ func (b *Builder[T]) Execute(ctx context.Context) (*PostgrestResponse[T], error) return response, nil } else if len(arr) == 1 { // Unmarshal single item - itemBytes, _ := json.Marshal(arr[0]) - json.Unmarshal(itemBytes, &response.Data) + itemBytes, err := json.Marshal(arr[0]) + if err != nil { + return nil, fmt.Errorf("error marshaling maybeSingle item: %w", err) + } + + if err := json.Unmarshal(itemBytes, &response.Data); err != nil { + return nil, fmt.Errorf("error unmarshaling maybeSingle item: %w", err) + } + } else { // Empty array, return null equivalent response.Data = *new(T) @@ -334,7 +341,9 @@ func (b *Builder[T]) Execute(ctx context.Context) (*PostgrestResponse[T], error) } } } else { - json.Unmarshal(bodyBytes, &response.Data) + if err := json.Unmarshal(bodyBytes, &response.Data); err != nil { + return nil, fmt.Errorf("error unmarshaling response: %w", err) + } } } }