fix: error handling in rule store
This commit is contained in:
+23
-6
@@ -17,7 +17,10 @@ func (s *Store) CreateRule(ctx context.Context, r *model.Rule, channelIDs []int)
|
||||
defer tx.Rollback()
|
||||
|
||||
query := `INSERT INTO rule (source_id, event, template_id, conditions, enabled) VALUES (?, ?, ?, ?, ?)`
|
||||
condsJSON, _ := marshalJSON(r.Conditions)
|
||||
condsJSON, err := marshalJSON(r.Conditions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal conditions: %w", err)
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, query, r.SourceID, r.Event, r.TemplateID, condsJSON, r.Enabled)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create rule: %w", err)
|
||||
@@ -79,7 +82,10 @@ func (s *Store) UpdateRule(ctx context.Context, id int, r *model.Rule, channelID
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
condsJSON, _ := marshalJSON(r.Conditions)
|
||||
condsJSON, err := marshalJSON(r.Conditions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal conditions: %w", err)
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `UPDATE rule SET source_id=?, event=?, template_id=?, conditions=?, enabled=? WHERE id=?`,
|
||||
r.SourceID, r.Event, r.TemplateID, condsJSON, r.Enabled, id)
|
||||
if err != nil {
|
||||
@@ -87,7 +93,9 @@ func (s *Store) UpdateRule(ctx context.Context, id int, r *model.Rule, channelID
|
||||
}
|
||||
|
||||
if channelIDs != nil {
|
||||
_, _ = tx.ExecContext(ctx, `DELETE FROM rule_channel WHERE rule_id = ?`, id)
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM rule_channel WHERE rule_id = ?`, id); err != nil {
|
||||
return fmt.Errorf("delete rule channels: %w", err)
|
||||
}
|
||||
for _, chID := range channelIDs {
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO rule_channel (rule_id, channel_id, enabled) VALUES (?, ?, 1)`, id, chID)
|
||||
if err != nil {
|
||||
@@ -100,7 +108,10 @@ func (s *Store) UpdateRule(ctx context.Context, id int, r *model.Rule, channelID
|
||||
|
||||
func (s *Store) DeleteRule(ctx context.Context, id int) error {
|
||||
_, err := s.DB.ExecContext(ctx, `DELETE FROM rule WHERE id = ?`, id)
|
||||
return err
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete rule %d: %w", id, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) SetRuleEnabled(ctx context.Context, id int, enabled bool) error {
|
||||
@@ -109,7 +120,10 @@ func (s *Store) SetRuleEnabled(ctx context.Context, id int, enabled bool) error
|
||||
v = 1
|
||||
}
|
||||
_, err := s.DB.ExecContext(ctx, `UPDATE rule SET enabled = ? WHERE id = ?`, v, id)
|
||||
return err
|
||||
if err != nil {
|
||||
return fmt.Errorf("set rule enabled %d: %w", id, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) GetRuleChannels(ctx context.Context, ruleID int) ([]model.RuleChannel, error) {
|
||||
@@ -127,7 +141,10 @@ func (s *Store) SetRuleChannelEnabled(ctx context.Context, ruleID, channelID int
|
||||
v = 1
|
||||
}
|
||||
_, err := s.DB.ExecContext(ctx, `UPDATE rule_channel SET enabled = ? WHERE rule_id = ? AND channel_id = ?`, v, ruleID, channelID)
|
||||
return err
|
||||
if err != nil {
|
||||
return fmt.Errorf("set rule channel enabled %d/%d: %w", ruleID, channelID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
Reference in New Issue
Block a user