Skip to content

Commit 2dfcb9e

Browse files
fixup! fix: recover from panic in compose library parse function
Signed-off-by: Harikrishnan Balagopal <[email protected]>
1 parent 8b2d2d1 commit 2dfcb9e

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

transformer/compose/v1v2.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,20 @@ func removeNonExistentEnvFilesV2(path string) preprocessFunc {
8787
}
8888
}
8989

90-
// panicky_parseV2 parses version 2 compose files (can panic on proj.Parse())
91-
func panicky_parseV2(path string, interpolate bool) (*project.Project, error) {
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+
102+
// parseV2 parses version 2 compose files
103+
func parseV2(path string, interpolate bool) (*project.Project, error) {
92104
context := project.Context{}
93105
context.ComposeFiles = []string{path}
94106
context.ResourceLookup = new(lookup.FileResourceLookup)
@@ -114,7 +126,7 @@ func panicky_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)
@@ -124,18 +136,6 @@ func panicky_parseV2(path string, interpolate bool) (*project.Project, error) {
124136
return proj, nil
125137
}
126138

127-
// parseV2 parses version 2 compose files while capturing panics
128-
func parseV2(path string, interpolate bool) (result *project.Project, err error) {
129-
defer func() {
130-
if r := recover(); r != nil {
131-
logrus.Errorf("recovered from panic in panicky_parseV2: %q", r)
132-
err = fmt.Errorf("panicky_parseV2 failed: %q", r)
133-
}
134-
}()
135-
result, err = panicky_parseV2(path, interpolate)
136-
return result, err
137-
}
138-
139139
// ConvertToIR loads a compose file to IR
140140
func (c *v1v2Loader) ConvertToIR(composefilepath string, serviceName string, parseNetwork bool) (ir irtypes.IR, err error) {
141141
proj, err := parseV2(composefilepath, true)

0 commit comments

Comments
 (0)