Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Add error handling for unmarshalling files
Browse files Browse the repository at this point in the history
Signed-off-by: thepetk <[email protected]>
  • Loading branch information
thepetk committed Jun 1, 2023
1 parent 3b29703 commit 07104f6
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
6 changes: 4 additions & 2 deletions go/pkg/apis/enricher/framework/dotnet/dotnet_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ func getFrameworks(configFilePath string) string {
byteValue, _ := ioutil.ReadAll(xmlFile)

var proj schema.DotNetProject
xml.Unmarshal(byteValue, &proj)

err = xml.Unmarshal(byteValue, &proj)
if err != nil {
return ""
}
defer func() error {
if err := xmlFile.Close(); err != nil {
return fmt.Errorf("error closing file: %s", err)
Expand Down
5 changes: 4 additions & 1 deletion go/pkg/apis/enricher/framework/java/micronaut_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func (m MicronautDetector) DoPortsDetection(component *model.Component, ctx *con
func getMicronautPortsFromBytes(bytes []byte) []int {
var ports []int
var data MicronautApplicationProps
yaml.Unmarshal(bytes, &data)
err := yaml.Unmarshal(bytes, &data)
if err != nil {
return []int{}
}
if data.Micronaut.Server.SSL.Enabled && utils.IsValidPort(data.Micronaut.Server.SSL.Port) {
ports = append(ports, data.Micronaut.Server.SSL.Port)
}
Expand Down
5 changes: 4 additions & 1 deletion go/pkg/apis/enricher/framework/java/openliberty_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ func (o OpenLibertyDetector) DoPortsDetection(component *model.Component, ctx *c
return
}
var data ServerXml
xml.Unmarshal(bytes, &data)
err = xml.Unmarshal(bytes, &data)
if err != nil {
return
}
ports := utils.GetValidPorts([]string{data.HttpEndpoint.HttpPort, data.HttpEndpoint.HttpsPort})
if len(ports) > 0 {
component.Ports = ports
Expand Down
5 changes: 4 additions & 1 deletion go/pkg/apis/enricher/framework/java/quarkus_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ func getServerPortsFromQuarkusApplicationYamlFile(file string) ([]int, error) {
return []int{}, err
}
var data QuarkusApplicationYaml
yaml.Unmarshal(yamlFile, &data)
err = yaml.Unmarshal(yamlFile, &data)
if err != nil {
return []int{}, err
}
var ports []int
if data.Quarkus.Http.SSLPort > 0 {
ports = append(ports, data.Quarkus.Http.SSLPort)
Expand Down
5 changes: 4 additions & 1 deletion go/pkg/apis/enricher/framework/java/spring_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ func getServerPortsFromYamlFile(file string) ([]int, error) {
return []int{}, err
}
var data ApplicationProsServer
yaml.Unmarshal(yamlFile, &data)
err = yaml.Unmarshal(yamlFile, &data)
if err != nil {
return []int{}, err
}
var ports []int
if data.Server.Port > 0 {
ports = append(ports, data.Server.Port)
Expand Down
16 changes: 12 additions & 4 deletions go/pkg/utils/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ func GetPomFileContent(pomFilePath string) (schema.Pom, error) {
byteValue, _ := ioutil.ReadAll(xmlFile)

var pom schema.Pom
xml.Unmarshal(byteValue, &pom)

err = xml.Unmarshal(byteValue, &pom)
if err != nil {
return schema.Pom{}, err
}
defer func() error {
if err := xmlFile.Close(); err != nil {
return fmt.Errorf("error closing file: %s", err)
Expand Down Expand Up @@ -191,7 +193,10 @@ func GetPackageJsonSchemaFromFile(path string) (schema.PackageJson, error) {
}

var packageJson schema.PackageJson
json.Unmarshal(bytes, &packageJson)
err = json.Unmarshal(bytes, &packageJson)
if err != nil {
return schema.PackageJson{}, err
}
return packageJson, nil
}

Expand All @@ -217,7 +222,10 @@ func GetComposerJsonSchemaFromFile(path string) (schema.ComposerJson, error) {
}

var composerJson schema.ComposerJson
json.Unmarshal(bytes, &composerJson)
err = json.Unmarshal(bytes, &composerJson)
if err != nil {
return schema.ComposerJson{}, err
}
return composerJson, nil
}

Expand Down
10 changes: 8 additions & 2 deletions go/pkg/utils/langfiles/languages_file_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func getLanguagesProperties() schema.LanguagesProperties {
return schema.LanguagesProperties{}
}
var data schema.LanguagesProperties
yaml.Unmarshal(yamlFile, &data)
err = yaml.Unmarshal(yamlFile, &data)
if err != nil {
return schema.LanguagesProperties{}
}
return data
}

Expand All @@ -125,7 +128,10 @@ func getLanguageCustomizations() schema.LanguagesCustomizations {
}

var data schema.LanguagesCustomizations
yaml.Unmarshal(yamlFile, &data)
err = yaml.Unmarshal(yamlFile, &data)
if err != nil {
return schema.LanguagesCustomizations{}
}
return data
}

Expand Down

0 comments on commit 07104f6

Please sign in to comment.