-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Revert index when error occur on issue/PR creation #21362
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
Changes from all commits
a9da2ec
8e8f781
13cf733
0bb3bf5
cce1089
9951025
7ea3b9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1077,19 +1077,38 @@ func NewIssue(repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids | |||||
} | ||||||
defer committer.Close() | ||||||
|
||||||
// onFail will try to gracefully revert the update in the resource index. | ||||||
onFail := func() error { | ||||||
// Close commiter. | ||||||
committer.Close() | ||||||
// Try to revert the increase in resource index. | ||||||
if err := db.UpsertDecrResourceIndex(db.DefaultContext, "issue_index", repo.ID, idx); err != nil { | ||||||
return fmt.Errorf("UpsertDecrResourceIndex: %v", err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{ | ||||||
Repo: repo, | ||||||
Issue: issue, | ||||||
LabelIDs: labelIDs, | ||||||
Attachments: uuids, | ||||||
}); err != nil { | ||||||
if err := onFail(); err != nil { | ||||||
return fmt.Errorf("couldn't gracefully handle error: %v", err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a new code-style I haven't been aware of? Since when are we going to use the wrapping errors verb? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, as I've learned: go seems to do some magic if you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we can wrap stuff my pr which tries to create a common hierarchy of errors will be a lot easier |
||||||
} | ||||||
|
||||||
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) { | ||||||
return err | ||||||
} | ||||||
return fmt.Errorf("newIssue: %v", err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrap errors!
Suggested change
|
||||||
} | ||||||
|
||||||
if err = committer.Commit(); err != nil { | ||||||
if err := onFail(); err != nil { | ||||||
return fmt.Errorf("couldn't gracefully handle error: %v", err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And wrap the rest of them too if possible please |
||||||
} | ||||||
|
||||||
return fmt.Errorf("Commit: %v", err) | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -504,13 +504,27 @@ func NewPullRequest(outerCtx context.Context, repo *repo_model.Repository, issue | |
defer committer.Close() | ||
ctx.WithContext(outerCtx) | ||
|
||
// onFail will try to gracefully revert the update in the resource index. | ||
onFail := func() error { | ||
// Close commiter. | ||
committer.Close() | ||
// Try to revert the increase in resource index. | ||
if err := db.UpsertDecrResourceIndex(outerCtx, "issue_index", repo.ID, idx); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect that outerCtx isn't the correct thing here. In fact I'm certain it isn't.
|
||
return fmt.Errorf("UpsertDecrResourceIndex: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{ | ||
Repo: repo, | ||
Issue: issue, | ||
LabelIDs: labelIDs, | ||
Attachments: uuids, | ||
IsPull: true, | ||
}); err != nil { | ||
if err := onFail(); err != nil { | ||
return fmt.Errorf("couldn't gracefully handle error: %v", err) | ||
} | ||
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) { | ||
return err | ||
} | ||
|
@@ -521,10 +535,18 @@ func NewPullRequest(outerCtx context.Context, repo *repo_model.Repository, issue | |
pr.BaseRepo = repo | ||
pr.IssueID = issue.ID | ||
if err = db.Insert(ctx, pr); err != nil { | ||
if err := onFail(); err != nil { | ||
return fmt.Errorf("couldn't gracefully handle error: %v", err) | ||
} | ||
|
||
return fmt.Errorf("insert pull repo: %v", err) | ||
} | ||
|
||
if err = committer.Commit(); err != nil { | ||
if err := onFail(); err != nil { | ||
return fmt.Errorf("couldn't gracefully handle error: %v", err) | ||
} | ||
|
||
return fmt.Errorf("Commit: %v", err) | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.