Skip to content

Commit 06227fe

Browse files
authored
feat: render error messages properly (#2)
* feat: render error messages properly * also skip pre-existing issues if the repo id hasn't been specified * use real versions
1 parent 505289e commit 06227fe

8 files changed

Lines changed: 66 additions & 62 deletions

File tree

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ go 1.25.5
44

55
require (
66
github.com/google/go-cmp v0.7.0
7-
github.com/infracost/proto v1.27.0
7+
github.com/infracost/go-proto v0.11.0
8+
github.com/infracost/proto v1.29.0
89
github.com/shurcooL/githubv4 v0.0.0-20260209031235-2402fdf4a9ed
910
golang.org/x/oauth2 v0.36.0
1011
)
@@ -23,7 +24,6 @@ require (
2324
)
2425

2526
require (
26-
github.com/infracost/go-proto v0.8.0
2727
golang.org/x/net v0.48.0 // indirect
2828
golang.org/x/sys v0.39.0 // indirect
2929
golang.org/x/text v0.32.0 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2424
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2525
github.com/hashicorp/hcl/v2 v2.24.0 h1:2QJdZ454DSsYGoaE6QheQZjtKZSUs9Nh2izTWiwQxvE=
2626
github.com/hashicorp/hcl/v2 v2.24.0/go.mod h1:oGoO1FIQYfn/AgyOhlg9qLC6/nOJPX3qGbkZpYAcqfM=
27-
github.com/infracost/go-proto v0.8.0 h1:YJoTnNJG+QnI9ZhQtiIzeoAvTKuO7HDWp61x8gEtHbA=
28-
github.com/infracost/go-proto v0.8.0/go.mod h1:2D8uulpDxry73uGiE6PW2Wx/PpqNGSgNndmgsWYyab0=
29-
github.com/infracost/proto v1.27.0 h1:Zjl3Hfqu7eBb+eGZJfXpTppODr1aAMBVT51cPHWyZB4=
30-
github.com/infracost/proto v1.27.0/go.mod h1:Z8vPWBWblwJlw+/ksO+BtsXwf9NiOcSTWx0WRWNbfUA=
27+
github.com/infracost/go-proto v0.11.0 h1:Q6nRZDxLJUsdVI0JDgnAb8NL/2JvhmgklZ2aYIeQmtI=
28+
github.com/infracost/go-proto v0.11.0/go.mod h1:VtzyuTe3h6GzrgHOQkVxjiP6Wpn4iUbr1HDaszcaV3k=
29+
github.com/infracost/proto v1.29.0 h1:+SaqDjOwGMpKlitplVS9GdK3TnL4kat0Iyfbxkvzudk=
30+
github.com/infracost/proto v1.29.0/go.mod h1:Z8vPWBWblwJlw+/ksO+BtsXwf9NiOcSTWx0WRWNbfUA=
3131
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
3232
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
3333
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

pkg/vcs/comment/cost_details.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func formatErroredProject(pr ProjectResult) string {
419419
if !diag.Critical {
420420
continue
421421
}
422-
pieces := strings.Split(diag.Error, ": ")
422+
pieces := strings.Split(diag.FormatMessage(), ": ")
423423
for x, piece := range pieces {
424424
s += strings.Repeat(" ", x+1) + piece
425425
if x == len(pieces)-1 {

pkg/vcs/comment/data.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package comment
22

33
import (
4+
"github.com/infracost/go-proto/pkg/diagnostic"
45
"github.com/infracost/go-proto/pkg/event"
56
"github.com/infracost/go-proto/pkg/rat"
6-
"github.com/infracost/proto/gen/go/infracost/parser"
77
"github.com/infracost/proto/gen/go/infracost/provider"
88
)
99

@@ -173,8 +173,8 @@ type ProjectResult struct {
173173
Resources []*provider.Resource
174174

175175
// Diagnostics contains any parsing errors or warnings for this project.
176-
Diagnostics []*parser.Diagnostic
176+
Diagnostics []*diagnostic.Diagnostic
177177

178178
// PastDiagnostics contains any parsing errors or warnings for this project before the current changes.
179-
PastDiagnostics []*parser.Diagnostic
179+
PastDiagnostics []*diagnostic.Diagnostic
180180
}

pkg/vcs/comment/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (data *Data) processProjectErrors(inputs *Inputs) {
2424
var errors []string
2525
for _, diag := range project.Diagnostics {
2626
if diag.Critical {
27-
errors = append(errors, diag.Error)
27+
errors = append(errors, diag.FormatMessage())
2828
}
2929
}
3030
if len(errors) == 0 {

pkg/vcs/comment/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func formatCostDetailsMsg(hasUnsupported, hasError bool) string {
207207
// counting total failed issues on the base branch and subtracting fixed issues.
208208
// See: dashboard/api/src/services/templates/partials/preexistingIssuesSentenceText.ts
209209
func (data *Data) processPreexistingIssues(inputs *Inputs) {
210-
if data.BaseBranchName == "" || data.OrgSlug == "" {
210+
if data.BaseBranchName == "" || data.OrgSlug == "" || data.RepoID == "" {
211211
return
212212
}
213213

pkg/vcs/comment/template_test.go

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ import (
77
"testing"
88

99
"github.com/google/go-cmp/cmp"
10+
"github.com/infracost/go-proto/pkg/diagnostic"
1011
"github.com/infracost/go-proto/pkg/event"
1112
"github.com/infracost/go-proto/pkg/rat"
12-
"github.com/infracost/proto/gen/go/infracost/parser"
13+
parserpb "github.com/infracost/proto/gen/go/infracost/parser"
1314
"github.com/infracost/proto/gen/go/infracost/provider"
1415
)
1516

1617
var update = flag.Bool("update", false, "update golden files")
1718

1819
func TestRender(t *testing.T) {
1920
tests := []struct {
20-
name string
21-
isGithub bool
22-
maxCommentSize int
23-
data Data
24-
goldenFile string
21+
name string
22+
isGithub bool
23+
maxCommentSize int
24+
data Data
25+
goldenFile string
2526
}{
2627
{
2728
name: "minimal_empty",
@@ -47,15 +48,15 @@ func TestRender(t *testing.T) {
4748
},
4849
Projects: []ProjectResult{
4950
{
50-
Name: "my-project",
51-
TotalMonthlyCost: rat.New(250),
52-
PastTotalMonthlyCost: rat.New(200),
53-
TotalMonthlyUsageCost: rat.New(10),
51+
Name: "my-project",
52+
TotalMonthlyCost: rat.New(250),
53+
PastTotalMonthlyCost: rat.New(200),
54+
TotalMonthlyUsageCost: rat.New(10),
5455
PastTotalMonthlyUsageCost: rat.New(5),
5556
Resources: []*provider.Resource{
5657
{
57-
Name: "aws_instance.web",
58-
Action: provider.ResourceAction_CREATE,
58+
Name: "aws_instance.web",
59+
Action: provider.ResourceAction_CREATE,
5960
IsSupported: true,
6061
},
6162
},
@@ -127,15 +128,15 @@ func TestRender(t *testing.T) {
127128
Projects: []ProjectResult{
128129
{
129130
Name: "broken-project",
130-
Diagnostics: []*parser.Diagnostic{
131-
{Critical: true, Error: "Failed to parse: invalid HCL"},
131+
Diagnostics: []*diagnostic.Diagnostic{
132+
{Critical: true, Type: parserpb.DiagnosticType_DIAGNOSTIC_TYPE_HCL_PARSE_ERROR, Error: "Failed to parse: invalid HCL"},
132133
},
133134
},
134135
{
135136
Name: "another-broken-project",
136-
Diagnostics: []*parser.Diagnostic{
137-
{Critical: true, Error: "Module not found: ./modules/vpc"},
138-
{Critical: true, Error: "Provider error: timeout"},
137+
Diagnostics: []*diagnostic.Diagnostic{
138+
{Critical: true, Type: parserpb.DiagnosticType_DIAGNOSTIC_TYPE_MODULE_FETCH_ERROR, Error: "Module not found: ./modules/vpc"},
139+
{Critical: true, Type: parserpb.DiagnosticType_DIAGNOSTIC_TYPE_INVALID_TERRAFORM_CONFIGURATION, Error: "Provider error: timeout"},
139140
},
140141
},
141142
},
@@ -251,21 +252,21 @@ func TestRender(t *testing.T) {
251252
PastTotalMonthlyCost: rat.New(100),
252253
GuardrailResults: []*event.GuardrailResult{
253254
{
254-
GuardrailID: "guardrail-1",
255-
GuardrailName: "Cost increase > $100",
256-
Triggered: true,
257-
PRComment: true,
258-
BlockPR: true,
259-
Increase: rat.New(400),
260-
PercentIncrease: rat.New(400),
255+
GuardrailID: "guardrail-1",
256+
GuardrailName: "Cost increase > $100",
257+
Triggered: true,
258+
PRComment: true,
259+
BlockPR: true,
260+
Increase: rat.New(400),
261+
PercentIncrease: rat.New(400),
261262
TriggeringProjectNames: []string{"my-project"},
262263
},
263264
},
264265
Projects: []ProjectResult{
265266
{
266-
Name: "my-project",
267-
TotalMonthlyCost: rat.New(500),
268-
PastTotalMonthlyCost: rat.New(100),
267+
Name: "my-project",
268+
TotalMonthlyCost: rat.New(500),
269+
PastTotalMonthlyCost: rat.New(100),
269270
Resources: []*provider.Resource{
270271
{
271272
Name: "aws_instance.big",
@@ -373,17 +374,17 @@ func TestRender(t *testing.T) {
373374
CommitSHA: "abc123",
374375
TaggingPolicyResults: []*event.TaggingPolicyResult{
375376
{
376-
Name: "Require env tag",
377+
Name: "Require env tag",
377378
TagPolicyID: "tp-1",
378-
Message: "All resources must have an env tag.",
379-
BlockPR: true,
380-
PRComment: true,
379+
Message: "All resources must have an env tag.",
380+
BlockPR: true,
381+
PRComment: true,
381382
FailingResources: []event.TagPolicyResultResource{
382383
{
383-
Address: "aws_instance.web",
384-
Path: "main.tf",
385-
Line: 10,
386-
ProjectNames: []string{"my-project"},
384+
Address: "aws_instance.web",
385+
Path: "main.tf",
386+
Line: 10,
387+
ProjectNames: []string{"my-project"},
387388
MissingMandatoryTags: []string{"env"},
388389
},
389390
{
@@ -553,9 +554,9 @@ func TestRender(t *testing.T) {
553554
},
554555
Projects: []ProjectResult{
555556
{
556-
Name: "project-a",
557-
TotalMonthlyCost: rat.New(600),
558-
PastTotalMonthlyCost: rat.New(300),
557+
Name: "project-a",
558+
TotalMonthlyCost: rat.New(600),
559+
PastTotalMonthlyCost: rat.New(300),
559560
Resources: []*provider.Resource{
560561
{Name: "aws_instance.a", Action: provider.ResourceAction_CREATE, IsSupported: true},
561562
},
@@ -585,9 +586,9 @@ func TestRender(t *testing.T) {
585586
},
586587
},
587588
{
588-
Name: "project-b",
589-
TotalMonthlyCost: rat.New(400),
590-
PastTotalMonthlyCost: rat.New(200),
589+
Name: "project-b",
590+
TotalMonthlyCost: rat.New(400),
591+
PastTotalMonthlyCost: rat.New(200),
591592
Resources: []*provider.Resource{
592593
{Name: "aws_instance.b", Action: provider.ResourceAction_MODIFY, IsSupported: true},
593594
},
@@ -648,4 +649,4 @@ func TestRender(t *testing.T) {
648649
}
649650
})
650651
}
651-
}
652+
}

pkg/vcs/comment/testdata/project_errors.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
<ul>
77
<li><b>broken-project</b></li>
8-
<pre>Failed to parse: invalid HCL</pre>
8+
<pre>HCL parse error: Failed to parse: invalid HCL</pre>
99
<li><b>another-broken-project</b></li>
10-
<pre>Module not found: ./modules/vpc, Provider error: timeout</pre>
10+
<pre>Failed to load module: Module not found: ./modules/vpc, Invalid Terraform configuration: Provider error: timeout</pre>
1111
</ul>
1212
<hr/>
1313
This pull request is aligned with your company's FinOps policies and the Well-Architected Framework.
@@ -21,16 +21,19 @@ This pull request is aligned with your company's FinOps policies and the Well-Ar
2121
──────────────────────────────────
2222
Project: another-broken-project
2323
Errors:
24-
Module not found:
25-
./modules/vpc
26-
Provider error:
27-
timeout
24+
Failed to load module:
25+
Module not found:
26+
./modules/vpc
27+
Invalid Terraform configuration:
28+
Provider error:
29+
timeout
2830
2931
──────────────────────────────────
3032
Project: broken-project
3133
Errors:
32-
Failed to parse:
33-
invalid HCL
34+
HCL parse error:
35+
Failed to parse:
36+
invalid HCL
3437
3538
──────────────────────────────────
3639

0 commit comments

Comments
 (0)