Skip to content

Commit

Permalink
Catching unhandled errors in tflint (#39)
Browse files Browse the repository at this point in the history
This hotfix catches unhandled errors while running tflint.
In case that HCL is invalid, we return "error" instead of exiting the server.
  • Loading branch information
sefi-infralight authored Apr 9, 2022
1 parent 2b97755 commit 3b7e548
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions backend/api/tflint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/hashicorp/hcl2/hclparse"
"github.com/thoas/go-funk"
"log"
"os"
"os/exec"
"strings"
Expand All @@ -22,7 +21,10 @@ func TFLint(in []byte) ([]byte, error) {

defer os.RemoveAll(path)

var hasAWS, hasAzure, hasGoogle = extractActiveProviders(in)
err, hasAWS, hasAzure, hasGoogle := extractActiveProviders(in)
if err != nil{
return nil, err
}

var args = []string{}
//Adding config args if one of the providers is enabled
Expand Down Expand Up @@ -53,11 +55,11 @@ func tflintInit() ([]byte, error) {
return cmd.CombinedOutput()
}

func extractActiveProviders(hcl []byte) (bool, bool, bool) {
func extractActiveProviders(hcl []byte) (error, bool, bool, bool) {
parser := hclparse.NewParser()
f, parseDiags := parser.ParseHCL(hcl, "")
if parseDiags.HasErrors() {
log.Fatal(parseDiags.Error())
return fmt.Errorf("failed creating temp file: %s", parseDiags.Error()), false, false, false
}

var hasAWS = false
Expand All @@ -81,5 +83,5 @@ func extractActiveProviders(hcl []byte) (bool, bool, bool) {
}
})

return hasAWS, hasAzure, hasGoogle
return nil ,hasAWS, hasAzure, hasGoogle
}

0 comments on commit 3b7e548

Please sign in to comment.