Skip to content

Commit f55ed2a

Browse files
committed
feat: enhance error handling with detailed logging and structured error model
1 parent aeca6a0 commit f55ed2a

File tree

4 files changed

+93
-32
lines changed

4 files changed

+93
-32
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: Added better error logging to include details and descriptive message
3+
time: 2025-06-13T11:17:54.899109137+02:00

internal/sdk/main.gen.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/utils/client.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package utils
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"net/http"
78
"reflect"
89

910
"github.com/deepmap/oapi-codegen/pkg/securityprovider"
1011
"github.com/hashicorp/go-retryablehttp"
12+
"github.com/hashicorp/terraform-plugin-log/tflog"
1113

1214
"github.com/labd/terraform-provider-contentful/internal/sdk"
1315
)
@@ -86,7 +88,31 @@ func ExtractErrorResponse(resp Response) error {
8688
if body.IsValid() && !body.IsZero() {
8789
value := body.Interface()
8890
if v, ok := value.([]byte); ok {
89-
return fmt.Errorf("response from Contentful API (%d):\n\n %s", resp.StatusCode(), string(v))
91+
var apiError sdk.Error
92+
err := json.Unmarshal(v, &apiError)
93+
if err != nil {
94+
return fmt.Errorf("error unmarshalling Contentful API error response: %w", err)
95+
}
96+
97+
var details []byte
98+
if apiError.Details != nil {
99+
details, err = json.MarshalIndent(apiError.Details, "", " ")
100+
if err != nil {
101+
tflog.Warn(context.TODO(), fmt.Sprintf("error marshalling details: %s", err.Error()))
102+
}
103+
} else {
104+
details = []byte("N/A")
105+
}
106+
107+
var message string
108+
if apiError.Message != nil {
109+
message = *apiError.Message
110+
} else {
111+
message = apiError.Sys.Id
112+
}
113+
114+
//TODO: we should probably use a more structured error type here, so we can handle the outputs better downstream
115+
return fmt.Errorf("response from Contentful API (%d): %s\n\nDetails: %s", resp.StatusCode(), message, string(details))
90116
}
91117
}
92118

openapi.yaml

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ servers:
1414
description: Contentful Content Management API
1515

1616
security:
17-
- bearerAuth: []
17+
- bearerAuth: [ ]
1818

1919
paths:
2020
/spaces:
@@ -1404,7 +1404,7 @@ components:
14041404
sys:
14051405
properties:
14061406
type:
1407-
enum: [Array]
1407+
enum: [ Array ]
14081408
type: string
14091409
type: object
14101410
total:
@@ -1493,7 +1493,7 @@ components:
14931493
sys:
14941494
properties:
14951495
type:
1496-
enum: [Array]
1496+
enum: [ Array ]
14971497
type: string
14981498
type: object
14991499
total:
@@ -1558,7 +1558,7 @@ components:
15581558
type: object
15591559
x-go-type: orderedmap.OrderedMap
15601560
x-go-type-import:
1561-
path: "github.com/iancoleman/orderedmap"
1561+
path: "github.com/iancoleman/orderedmap"
15621562
sys:
15631563
$ref: '#/components/schemas/SystemPropertiesEntry'
15641564
required:
@@ -1581,7 +1581,7 @@ components:
15811581
sys:
15821582
properties:
15831583
type:
1584-
enum: [Array]
1584+
enum: [ Array ]
15851585
type: string
15861586
type: object
15871587
total:
@@ -1597,7 +1597,7 @@ components:
15971597
type: object
15981598
x-go-type: orderedmap.OrderedMap
15991599
x-go-type-import:
1600-
path: "github.com/iancoleman/orderedmap"
1600+
path: "github.com/iancoleman/orderedmap"
16011601

16021602
Environment:
16031603
type: object
@@ -1627,7 +1627,7 @@ components:
16271627
sys:
16281628
properties:
16291629
type:
1630-
enum: [Array]
1630+
enum: [ Array ]
16311631
type: string
16321632
type: object
16331633
total:
@@ -1656,10 +1656,10 @@ components:
16561656
description: Environment ID
16571657
type: string
16581658
linkType:
1659-
enum: [Environment]
1659+
enum: [ Environment ]
16601660
type: string
16611661
type:
1662-
enum: [Link]
1662+
enum: [ Link ]
16631663
type: string
16641664
required:
16651665
- id
@@ -1733,7 +1733,7 @@ components:
17331733
$ref: '#/components/schemas/FieldItem'
17341734
linkType:
17351735
description: For Link fields, defines what type of resource it links to
1736-
enum: [Entry, Asset]
1736+
enum: [ Entry, Asset ]
17371737
type: string
17381738
localized:
17391739
description: Whether the field is localized
@@ -1959,7 +1959,7 @@ components:
19591959
sys:
19601960
properties:
19611961
type:
1962-
enum: [Array]
1962+
enum: [ Array ]
19631963
type: string
19641964
type: object
19651965
total:
@@ -2056,7 +2056,7 @@ components:
20562056
sys:
20572057
properties:
20582058
type:
2059-
enum: [Array]
2059+
enum: [ Array ]
20602060
type: string
20612061
type: object
20622062
total:
@@ -2241,10 +2241,10 @@ components:
22412241
description: Content type ID
22422242
type: string
22432243
linkType:
2244-
enum: [ContentType]
2244+
enum: [ ContentType ]
22452245
type: string
22462246
type:
2247-
enum: [Link]
2247+
enum: [ Link ]
22482248
type: string
22492249
type: object
22502250
type: object
@@ -2356,7 +2356,7 @@ components:
23562356
sys:
23572357
properties:
23582358
type:
2359-
enum: [Array]
2359+
enum: [ Array ]
23602360
type: string
23612361
type: object
23622362
total:
@@ -2395,8 +2395,8 @@ components:
23952395
description: Filters to apply to the webhook
23962396
type: array
23972397
items:
2398-
type: object
2399-
additionalProperties: true
2398+
type: object
2399+
additionalProperties: true
24002400
required:
24012401
- name
24022402
- url
@@ -2480,8 +2480,8 @@ components:
24802480
type: object
24812481
additionalProperties: true
24822482
required:
2483-
- sys
2484-
- controls
2483+
- sys
2484+
- controls
24852485

24862486
EditorInterfaceUpdate:
24872487
type: object
@@ -2508,7 +2508,7 @@ components:
25082508
type: object
25092509
additionalProperties: true
25102510
required:
2511-
- controls
2511+
- controls
25122512

25132513
EditorInterfaceControl:
25142514
type: object
@@ -2522,7 +2522,7 @@ components:
25222522
widgetNamespace:
25232523
type: string
25242524
description: Namespace of the widget (optional)
2525-
enum: [builtin, app, extension]
2525+
enum: [ builtin, app, extension ]
25262526
disabled:
25272527
type: boolean
25282528
description: Whether the field is disabled
@@ -2540,7 +2540,7 @@ components:
25402540
widgetNamespace:
25412541
type: string
25422542
description: Namespace of the widget (optional)
2543-
enum: [builtin, app, extension]
2543+
enum: [ builtin, app, extension ]
25442544
disabled:
25452545
type: boolean
25462546
description: Whether the field is disabled
@@ -2603,7 +2603,7 @@ components:
26032603
description: ID of the preview API key
26042604
type:
26052605
type: string
2606-
enum: [PreviewApiKey]
2606+
enum: [ PreviewApiKey ]
26072607
environments:
26082608
type: array
26092609
items:
@@ -2635,7 +2635,7 @@ components:
26352635
properties:
26362636
type:
26372637
type: string
2638-
enum: [Array]
2638+
enum: [ Array ]
26392639
items:
26402640
type: array
26412641
items:
@@ -2702,7 +2702,7 @@ components:
27022702
properties:
27032703
type:
27042704
type: string
2705-
enum: [Array]
2705+
enum: [ Array ]
27062706
items:
27072707
type: array
27082708
items:
@@ -2744,10 +2744,10 @@ components:
27442744
description: ID of the app definition
27452745
type:
27462746
type: string
2747-
enum: [Link]
2747+
enum: [ Link ]
27482748
linkType:
27492749
type: string
2750-
enum: [AppDefinition]
2750+
enum: [ AppDefinition ]
27512751
required:
27522752
- sys
27532753

@@ -2772,7 +2772,7 @@ components:
27722772
properties:
27732773
type:
27742774
type: string
2775-
enum: [Array]
2775+
enum: [ Array ]
27762776
total:
27772777
type: integer
27782778
description: Total number of app installations
@@ -2812,7 +2812,7 @@ components:
28122812
properties:
28132813
type:
28142814
type: string
2815-
enum: [Symbol, Boolean, Number, Object]
2815+
enum: [ Symbol, Boolean, Number, Object ]
28162816
name:
28172817
type: string
28182818
description: Name of the parameter
@@ -2834,7 +2834,7 @@ components:
28342834
properties:
28352835
type:
28362836
type: string
2837-
enum: [Symbol, Boolean, Number, Object]
2837+
enum: [ Symbol, Boolean, Number, Object ]
28382838
name:
28392839
type: string
28402840
description: Name of the parameter
@@ -2895,7 +2895,7 @@ components:
28952895
properties:
28962896
type:
28972897
type: string
2898-
enum: [Array]
2898+
enum: [ Array ]
28992899
total:
29002900
type: integer
29012901
description: Total number of app definitions
@@ -3046,3 +3046,22 @@ components:
30463046
- name
30473047
- description
30483048
- configurations
3049+
3050+
Error:
3051+
type: object
3052+
properties:
3053+
sys:
3054+
$ref: "#/components/schemas/SystemPropertiesBase"
3055+
requestId:
3056+
type: string
3057+
description: Unique identifier for the request
3058+
message:
3059+
type: string
3060+
description: Error message
3061+
details:
3062+
type: object
3063+
description: Additional error details
3064+
additionalProperties: true
3065+
required:
3066+
- sys
3067+
- requestId

0 commit comments

Comments
 (0)