Skip to content

Commit

Permalink
fix: recover from panic in compose library parse function (#1197)
Browse files Browse the repository at this point in the history
* fix: recover from panic in compose library parse function

Signed-off-by: Harikrishnan Balagopal <harikrishmenon@gmail.com>
  • Loading branch information
HarikrishnanBalagopal authored Oct 18, 2024
1 parent e02fdfb commit 18e6d62
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion transformer/compose/v1v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ func removeNonExistentEnvFilesV2(path string) preprocessFunc {
}
}

// parseCapturingPanics parses version 2 compose files while capturing panics
func parseCapturingPanics(proj *project.Project) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recovered from panic in compose Project.Parse: %q", r)
logrus.Error(err)
}
}()
err = proj.Parse()
return err
}

// parseV2 parses version 2 compose files
func parseV2(path string, interpolate bool) (*project.Project, error) {
context := project.Context{}
Expand Down Expand Up @@ -114,7 +126,7 @@ func parseV2(path string, interpolate bool) (*project.Project, error) {
proj := project.NewProject(&context, nil, &parseOptions)
originalLevel := logrus.GetLevel()
logrus.SetLevel(logrus.FatalLevel) // TODO: this is a hack to prevent libcompose from printing errors to the console.
err = proj.Parse()
err = parseCapturingPanics(proj)
logrus.SetLevel(originalLevel) // TODO: this is a hack to prevent libcompose from printing errors to the console.
if err != nil {
err := fmt.Errorf("failed to load docker compose file at path %s Error: %q", path, err)
Expand Down

0 comments on commit 18e6d62

Please sign in to comment.