Skip to content

Commit

Permalink
chore: handle file not found error when getResources
Browse files Browse the repository at this point in the history
This commit message documents changes made to teler.go
file that handles the file not found error when reading
the threat resources. Previously, the program returned
an error immediately when it encountered a file not
found error. With this change, the program will check
if the error is a file not found error and attempt to
retrieve the threat before retrying to read the file.
If the threat retrieval fails, an error will be returned.
Otherwise, the program will continue to read the file
as usual.

These changes improve the robustness of the program by
gracefully handling file not found errors when reading
the threat resources.
  • Loading branch information
dwisiswant0 committed Feb 27, 2023
1 parent 5fe776a commit b998626
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions teler.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,32 @@ func (t *Teler) getResources() error {
return err
}

// Read the contents of the data file and store it
// as a string in the data field of the Threat struct
// Read the contents of the data file at the specified path and store it
// as a string in the data field of the Threat struct. If the file is not
// found, the function will attempt to retrieve the threat from an external
// source using the `Get()` method on the `threat` object. If the threat
// retrieval fails, an error will be returned. Otherwise, the function will
// retry reading the file as usual. If any other error occurs while reading
// the file, it will be returned immediately.
b, err := os.ReadFile(path)
if err != nil {
return err
if os.IsNotExist(err) {
// If the error is a file not found error, attempt to retrieve the
// threat from an external source using the `Get()` method on the
// `threat` object.
if err := threat.Get(); err != nil {
return err
}

// Retry reading the file after retrieving the threat.
b, err = os.ReadFile(path)
if err != nil {
return err
}
} else {
// If the error is not a file not found error, return it immediately.
return err
}
}
t.threat.data[k] = string(b)

Expand Down

0 comments on commit b998626

Please sign in to comment.