refactor: Improve error logging

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-07 00:25:29 +01:00
parent 8c2e478ae9
commit 25626f1f75
No known key found for this signature in database
GPG key ID: 332171FADF1DB90B
4 changed files with 16 additions and 14 deletions

View file

@ -25,10 +25,10 @@ var (
PreRun: InitializeConfig, PreRun: InitializeConfig,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(config.Gitea.Token)) client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(config.Gitea.Token))
core.ExitOnError(err) core.ExitOnError("Couldn't create gitea client", err)
err = GetPrID(client, &prID) err = GetPrID(client, &prID)
core.ExitOnError(err) core.ExitOnError("Couldn't get PR ID", err)
comments := core.GetComments(&config, client, prID) comments := core.GetComments(&config, client, prID)
core.ProcessComments(&config, &comments) core.ProcessComments(&config, &comments)
@ -54,7 +54,7 @@ func GetPrID(client *gitea.Client, prID *int64) error {
gitCmd.Stdout = &capturedOutput gitCmd.Stdout = &capturedOutput
err := gitCmd.Run() err := gitCmd.Run()
core.ExitOnError(err) core.ExitOnError("Couldn't list git branches", err)
branch = strings.TrimSpace(capturedOutput.String()) branch = strings.TrimSpace(capturedOutput.String())
} }
@ -62,7 +62,7 @@ func GetPrID(client *gitea.Client, prID *int64) error {
prs, _, err := client.ListRepoPullRequests( prs, _, err := client.ListRepoPullRequests(
config.Gitea.Owner, config.Gitea.Repository, gitea.ListPullRequestsOptions{}, config.Gitea.Owner, config.Gitea.Repository, gitea.ListPullRequestsOptions{},
) )
core.ExitOnError(err) core.ExitOnError("Couldn't list pull requests", err)
for _, pr := range prs { for _, pr := range prs {
if pr.Head.Name == branch { if pr.Head.Name == branch {
@ -94,11 +94,11 @@ func ProcessFile(filepath string, comments []*gitea.PullReviewComment) {
core.BackUpSource(filepath) core.BackUpSource(filepath)
inputFile, err := os.Open(filepath + ".bck") inputFile, err := os.Open(filepath + ".bck")
core.ExitOnError(err) core.ExitOnError("Couldn't open backup file", err)
defer inputFile.Close() defer inputFile.Close()
outputFile, err := os.Create(filepath) outputFile, err := os.Create(filepath)
core.ExitOnError(err) core.ExitOnError("Couldn't overwrite original file", err)
defer outputFile.Close() defer outputFile.Close()
var i uint64 = 1 var i uint64 = 1

View file

@ -23,10 +23,10 @@ type Config struct {
func LoadConfig(config *Config, filename string) { func LoadConfig(config *Config, filename string) {
configFile, err := os.Open(filename) configFile, err := os.Open(filename)
ExitOnError(err) ExitOnError("Couldn't open config file", err)
defer configFile.Close() defer configFile.Close()
decoder := yaml.NewDecoder(configFile) decoder := yaml.NewDecoder(configFile)
err = decoder.Decode(&config) err = decoder.Decode(&config)
ExitOnError(err) ExitOnError("Couldn't decode config file", err)
} }

View file

@ -2,6 +2,7 @@ package core
import ( import (
"sort" "sort"
"fmt"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
) )
@ -10,7 +11,7 @@ func GetReviewsPerPR(config *Config, client *gitea.Client, prID int64) []int64 {
reviews, _, err := client.ListPullReviews( reviews, _, err := client.ListPullReviews(
config.Gitea.Owner, config.Gitea.Repository, prID, gitea.ListPullReviewsOptions{}, config.Gitea.Owner, config.Gitea.Repository, prID, gitea.ListPullReviewsOptions{},
) )
ExitOnError(err) ExitOnError("Couldn't list reviews per PR ID", err)
result := make([]int64, 0) result := make([]int64, 0)
for _, review := range reviews { for _, review := range reviews {
@ -24,7 +25,7 @@ func RetrieveComments(config *Config, client *gitea.Client, prID, reviewID int64
comments, _, err := client.ListPullReviewComments( comments, _, err := client.ListPullReviewComments(
config.Gitea.Owner, config.Gitea.Repository, prID, reviewID, config.Gitea.Owner, config.Gitea.Repository, prID, reviewID,
) )
ExitOnError(err) ExitOnError("Couldn't list review comments for review", err)
return comments return comments
} }

View file

@ -14,8 +14,9 @@ func (a ByLineNum) Len() int { return len(a) }
func (a ByLineNum) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByLineNum) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByLineNum) Less(i, j int) bool { return a[i].LineNum < a[j].LineNum } func (a ByLineNum) Less(i, j int) bool { return a[i].LineNum < a[j].LineNum }
func ExitOnError(err error) { func ExitOnError(msg string, err error) {
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s: ", msg)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
@ -23,13 +24,13 @@ func ExitOnError(err error) {
func BackUpSource(filepath string) { func BackUpSource(filepath string) {
inputFile, err := os.Open(filepath) inputFile, err := os.Open(filepath)
ExitOnError(err) ExitOnError("Couldn't open input file for backup", err)
defer inputFile.Close() defer inputFile.Close()
BackUpSourceFile, err := os.Create(filepath + ".bck") BackUpSourceFile, err := os.Create(filepath + ".bck")
ExitOnError(err) ExitOnError("Couldn't create backup file", err)
defer BackUpSourceFile.Close() defer BackUpSourceFile.Close()
_, err = io.Copy(BackUpSourceFile, inputFile) _, err = io.Copy(BackUpSourceFile, inputFile)
ExitOnError(err) ExitOnError("Couldn't copy into backup file", err)
} }