Skip to content

Commit 09b0df7

Browse files
committed
improve error handling and logging for webhook events
- Replace generic error messages with structured error types in parser - Add proper error classification for unsupported events and validation errors - Reduce log noise by changing info logs to debug for routine events - Skip error logging for expected unsupported event types in webhook handler
1 parent 068a445 commit 09b0df7

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

internal/agent/agent.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,11 @@ func (a *EnhancedAgent) ProcessGitHubWebhookEvent(ctx context.Context, eventType
116116
xl := xlog.NewWith(ctx)
117117

118118
startTime := time.Now()
119-
xl.Infof("Processing GitHub webhook event: %s, delivery_id: %s", eventType, deliveryID)
119+
xl.Debugf("Processing GitHub webhook event: %s, delivery_id: %s", eventType, deliveryID)
120120

121121
// 1. 解析GitHub事件为类型安全的上下文
122122
githubCtx, err := a.eventParser.ParseWebhookEvent(ctx, eventType, deliveryID, payload)
123123
if err != nil {
124-
xl.Warnf("Failed to parse GitHub webhook event: %v", err)
125124
return fmt.Errorf("failed to parse webhook event: %w", err)
126125
}
127126

internal/events/errors.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package events
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
// Predefined error types for event operations
9+
var (
10+
// Event parsing errors
11+
ErrUnsupportedEventType = errors.New("unsupported event type")
12+
ErrEventNotFound = errors.New("event not found")
13+
ErrInvalidEventFormat = errors.New("invalid event format")
14+
ErrEventParsingFailed = errors.New("event parsing failed")
15+
16+
// Event validation errors
17+
ErrMissingRepository = errors.New("missing repository in event")
18+
ErrMissingSender = errors.New("missing sender in event")
19+
ErrMissingIssue = errors.New("missing issue in event")
20+
ErrMissingComment = errors.New("missing comment in event")
21+
ErrMissingPullRequest = errors.New("missing pull request in event")
22+
ErrMissingReview = errors.New("missing review in event")
23+
)
24+
25+
// EventError represents an event-related error with context
26+
type EventError struct {
27+
Op string // Operation that failed
28+
EventType string // Event type (if applicable)
29+
Err error // Underlying error
30+
Context string // Additional context
31+
}
32+
33+
func (e *EventError) Error() string {
34+
if e.EventType != "" && e.Context != "" {
35+
return fmt.Sprintf("event %s failed for type %s: %v (context: %s)", e.Op, e.EventType, e.Err, e.Context)
36+
}
37+
if e.EventType != "" {
38+
return fmt.Sprintf("event %s failed for type %s: %v", e.Op, e.EventType, e.Err)
39+
}
40+
if e.Context != "" {
41+
return fmt.Sprintf("event %s failed: %v (context: %s)", e.Op, e.Err, e.Context)
42+
}
43+
return fmt.Sprintf("event %s failed: %v", e.Op, e.Err)
44+
}
45+
46+
func (e *EventError) Unwrap() error {
47+
return e.Err
48+
}
49+
50+
// NewEventError creates a new EventError
51+
func NewEventError(op, eventType string, err error, context string) *EventError {
52+
return &EventError{
53+
Op: op,
54+
EventType: eventType,
55+
Err: err,
56+
Context: context,
57+
}
58+
}
59+
60+
// Helper functions for creating specific errors
61+
62+
// UnsupportedEventTypeError creates an unsupported event type error
63+
func UnsupportedEventTypeError(eventType string) error {
64+
return NewEventError("parse", eventType, ErrUnsupportedEventType, "")
65+
}
66+
67+
// ParsingError creates a parsing-related error
68+
func ParsingError(eventType string, err error) error {
69+
return NewEventError("parse", eventType, fmt.Errorf("parsing: %w", err), "")
70+
}
71+
72+
// ValidationError creates a validation-related error
73+
func ValidationError(eventType string, err error, context string) error {
74+
return NewEventError("validate", eventType, fmt.Errorf("validation: %w", err), context)
75+
}

internal/events/parser.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (p *EventParser) ParseEvent(ctx context.Context, eventType string, rawEvent
3535
return p.ParseIssueCommentEvent(ctx, event)
3636
}
3737
}
38-
return nil, fmt.Errorf("unsupported event type or format: %s", eventType)
38+
return nil, UnsupportedEventTypeError(eventType)
3939
}
4040

4141
// ParseIssueCommentEvent 解析Issue评论事件(从原始GitHub事件)
@@ -71,7 +71,7 @@ func (p *EventParser) ParseWebhookEvent(
7171
) (models.GitHubContext, error) {
7272
// 验证事件类型
7373
if !models.IsValidEventType(eventType) {
74-
return nil, fmt.Errorf("unsupported event type: %s", eventType)
74+
return nil, UnsupportedEventTypeError(eventType)
7575
}
7676

7777
// 根据事件类型解析
@@ -89,7 +89,7 @@ func (p *EventParser) ParseWebhookEvent(
8989
case models.EventPush:
9090
return p.parsePushEvent(ctx, payload, deliveryID)
9191
default:
92-
return nil, fmt.Errorf("event type %s not implemented yet", eventType)
92+
return nil, UnsupportedEventTypeError(eventType)
9393
}
9494
}
9595

@@ -101,21 +101,21 @@ func (p *EventParser) parseIssueCommentEvent(
101101
) (*models.IssueCommentContext, error) {
102102
var event github.IssueCommentEvent
103103
if err := json.Unmarshal(payload, &event); err != nil {
104-
return nil, fmt.Errorf("failed to unmarshal issue comment event: %w", err)
104+
return nil, ParsingError("issue_comment", err)
105105
}
106106

107107
// 检查必需字段
108108
if event.Repo == nil {
109-
return nil, fmt.Errorf("missing repository in issue comment event")
109+
return nil, ValidationError("issue_comment", ErrMissingRepository, "")
110110
}
111111
if event.Sender == nil {
112-
return nil, fmt.Errorf("missing sender in issue comment event")
112+
return nil, ValidationError("issue_comment", ErrMissingSender, "")
113113
}
114114
if event.Issue == nil {
115-
return nil, fmt.Errorf("missing issue in issue comment event")
115+
return nil, ValidationError("issue_comment", ErrMissingIssue, "")
116116
}
117117
if event.Comment == nil {
118-
return nil, fmt.Errorf("missing comment in issue comment event")
118+
return nil, ValidationError("issue_comment", ErrMissingComment, "")
119119
}
120120

121121
// 判断是否是PR评论

internal/webhook/handler.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package webhook
22

33
import (
44
"context"
5+
"errors"
56
"io"
67
"net/http"
78

89
"github.com/qiniu/codeagent/internal/agent"
910
"github.com/qiniu/codeagent/internal/config"
11+
"github.com/qiniu/codeagent/internal/events"
1012
"github.com/qiniu/codeagent/pkg/signature"
1113

1214
"github.com/qiniu/x/reqid"
@@ -82,7 +84,11 @@ func (h *Handler) HandleWebhook(w http.ResponseWriter, r *http.Request) {
8284
go func(eventType string, payload []byte, deliveryID string, traceCtx context.Context) {
8385
traceLog := xlog.NewWith(traceCtx)
8486
if err := h.enhancedAgent.ProcessGitHubWebhookEvent(traceCtx, eventType, deliveryID, payload); err != nil {
85-
traceLog.Warnf("enhanced agent event processing error: %v", err)
87+
if errors.Is(err, events.ErrUnsupportedEventType) {
88+
traceLog.Debugf("enhanced agent unsupported event type: %v", err)
89+
} else {
90+
traceLog.Warnf("enhanced agent event processing error: %v", err)
91+
}
8692
} else {
8793
traceLog.Infof("enhanced agent event processing completed successfully")
8894
}

0 commit comments

Comments
 (0)