Skip to content

Commit 18e6d62

Browse files
fix: recover from panic in compose library parse function (#1197)
* fix: recover from panic in compose library parse function Signed-off-by: Harikrishnan Balagopal <[email protected]>
1 parent e02fdfb commit 18e6d62

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

transformer/compose/v1v2.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ func removeNonExistentEnvFilesV2(path string) preprocessFunc {
8787
}
8888
}
8989

90+
// parseCapturingPanics parses version 2 compose files while capturing panics
91+
func parseCapturingPanics(proj *project.Project) (err error) {
92+
defer func() {
93+
if r := recover(); r != nil {
94+
err = fmt.Errorf("recovered from panic in compose Project.Parse: %q", r)
95+
logrus.Error(err)
96+
}
97+
}()
98+
err = proj.Parse()
99+
return err
100+
}
101+
90102
// parseV2 parses version 2 compose files
91103
func parseV2(path string, interpolate bool) (*project.Project, error) {
92104
context := project.Context{}
@@ -114,7 +126,7 @@ func parseV2(path string, interpolate bool) (*project.Project, error) {
114126
proj := project.NewProject(&context, nil, &parseOptions)
115127
originalLevel := logrus.GetLevel()
116128
logrus.SetLevel(logrus.FatalLevel) // TODO: this is a hack to prevent libcompose from printing errors to the console.
117-
err = proj.Parse()
129+
err = parseCapturingPanics(proj)
118130
logrus.SetLevel(originalLevel) // TODO: this is a hack to prevent libcompose from printing errors to the console.
119131
if err != nil {
120132
err := fmt.Errorf("failed to load docker compose file at path %s Error: %q", path, err)

0 commit comments

Comments
 (0)