Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling error loading config > 1MB #248

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
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
42 changes: 23 additions & 19 deletions appconfig/appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,31 +267,35 @@ func (ld *Loader) loadDefaultConfig(ctx context.Context, client *github.Client,
// getFileContents returns the content of the file at path on ref in owner/repo
// if it exists. Returns an empty slice and false if the file does not exist.
func getFileContents(ctx context.Context, client *github.Client, owner, repo, ref, path string) ([]byte, bool, error) {
file, _, _, err := client.Repositories.GetContents(ctx, owner, repo, path, &github.RepositoryContentGetOptions{
Ref: ref,
})
rawURL := client.BaseURL.String() + "repos/" + owner + "/" + repo + "/contents/" + path + "?ref=" + ref
req, err := http.NewRequestWithContext(ctx, "GET", rawURL, nil)
if err != nil {
switch {
case isNotFound(err):
return nil, false, nil
case isTooLargeError(err):
b, err := getLargeFileContents(ctx, client, owner, repo, ref, path)
return b, true, err
}
return nil, false, errors.Wrap(err, "failed to read file")
}

// file will be nil if the path exists but is a directory
if file == nil {
return nil, false, nil
return nil, false, errors.Wrap(err, "failed to create request")
}
req.Header.Set("Accept", "application/vnd.github.raw")

content, err := file.GetContent()
httpClient := http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return nil, true, errors.Wrap(err, "failed to decode file content")
return nil, false, errors.Wrap(err, "failed to fetch file content")
}
defer resp.Body.Close()

return []byte(content), true, nil
switch resp.StatusCode {
case http.StatusOK:
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, true, errors.Wrap(err, "failed to read file content")
}
return content, true, nil
case http.StatusNotFound:
return nil, false, nil
case http.StatusForbidden:
b, err := getLargeFileContents(ctx, client, owner, repo, ref, path)
return b, true, err
default:
return nil, false, errors.New("unexpected error while fetching file content")
}
}

// getLargeFileContents is similar to getFileContents, but works for files up
Expand Down