Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup! fix: recover from panic in compose library parse function
Signed-off-by: Harikrishnan Balagopal <harikrishmenon@gmail.com>
  • Loading branch information
HarikrishnanBalagopal committed Oct 18, 2024
commit 2dfcb9eab68ba4e5010316b46a474c25ae94918d
30 changes: 15 additions & 15 deletions transformer/compose/v1v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,20 @@ func removeNonExistentEnvFilesV2(path string) preprocessFunc {
}
}

// panicky_parseV2 parses version 2 compose files (can panic on proj.Parse())
func panicky_parseV2(path string, interpolate bool) (*project.Project, error) {
// 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{}
context.ComposeFiles = []string{path}
context.ResourceLookup = new(lookup.FileResourceLookup)
Expand All @@ -114,7 +126,7 @@ func panicky_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 All @@ -124,18 +136,6 @@ func panicky_parseV2(path string, interpolate bool) (*project.Project, error) {
return proj, nil
}

// parseV2 parses version 2 compose files while capturing panics
func parseV2(path string, interpolate bool) (result *project.Project, err error) {
defer func() {
if r := recover(); r != nil {
logrus.Errorf("recovered from panic in panicky_parseV2: %q", r)
err = fmt.Errorf("panicky_parseV2 failed: %q", r)
}
}()
result, err = panicky_parseV2(path, interpolate)
return result, err
}

// ConvertToIR loads a compose file to IR
func (c *v1v2Loader) ConvertToIR(composefilepath string, serviceName string, parseNetwork bool) (ir irtypes.IR, err error) {
proj, err := parseV2(composefilepath, true)
Expand Down