@@ -9,44 +9,49 @@ import (
99 "github.com/PeerDB-io/peerdb/flow/generated/protos"
1010 "github.com/PeerDB-io/peerdb/flow/internal"
1111 "github.com/PeerDB-io/peerdb/flow/shared"
12- "github.com/PeerDB-io/peerdb/flow/shared/exceptions"
1312)
1413
15- func (h * FlowRequestHandler ) GetAlertConfigs (ctx context.Context , req * protos.GetAlertConfigsRequest ) (* protos.GetAlertConfigsResponse , error ) {
14+ func (h * FlowRequestHandler ) GetAlertConfigs (
15+ ctx context.Context ,
16+ req * protos.GetAlertConfigsRequest ,
17+ ) (* protos.GetAlertConfigsResponse , APIError ) {
1618 rows , err := h .pool .Query (ctx , "SELECT id,service_type,service_config,enc_key_id,alert_for_mirrors from peerdb_stats.alerting_config" )
1719 if err != nil {
18- return nil , exceptions . NewInternalApiError (fmt .Errorf ("failed to get alert configs: %w" , err ))
20+ return nil , NewInternalApiError (fmt .Errorf ("failed to get alert configs: %w" , err ))
1921 }
2022
2123 configs , err := pgx .CollectRows (rows , func (row pgx.CollectableRow ) (* protos.AlertConfig , error ) {
2224 var serviceConfigPayload []byte
2325 var encKeyID string
2426 config := & protos.AlertConfig {}
2527 if err := row .Scan (& config .Id , & config .ServiceType , & serviceConfigPayload , & encKeyID , & config .AlertForMirrors ); err != nil {
26- return nil , exceptions . NewInternalApiError (fmt .Errorf ("failed to scan alert config: %w" , err ))
28+ return nil , NewInternalApiError (fmt .Errorf ("failed to scan alert config: %w" , err ))
2729 }
2830 serviceConfig , err := internal .Decrypt (ctx , encKeyID , serviceConfigPayload )
2931 if err != nil {
30- return nil , exceptions . NewInternalApiError (fmt .Errorf ("failed to decrypt alert config: %w" , err ))
32+ return nil , NewInternalApiError (fmt .Errorf ("failed to decrypt alert config: %w" , err ))
3133 }
3234 config .ServiceConfig = string (serviceConfig )
3335 return config , nil
3436 })
3537 if err != nil {
36- return nil , exceptions . NewInternalApiError (fmt .Errorf ("failed to collect alert configs: %w" , err ))
38+ return nil , NewInternalApiError (fmt .Errorf ("failed to collect alert configs: %w" , err ))
3739 }
3840
3941 return & protos.GetAlertConfigsResponse {Configs : configs }, nil
4042}
4143
42- func (h * FlowRequestHandler ) PostAlertConfig (ctx context.Context , req * protos.PostAlertConfigRequest ) (* protos.PostAlertConfigResponse , error ) {
44+ func (h * FlowRequestHandler ) PostAlertConfig (
45+ ctx context.Context ,
46+ req * protos.PostAlertConfigRequest ,
47+ ) (* protos.PostAlertConfigResponse , APIError ) {
4348 key , err := internal .PeerDBCurrentEncKey (ctx )
4449 if err != nil {
45- return nil , exceptions . NewInternalApiError (fmt .Errorf ("failed to get current enc key: %w" , err ))
50+ return nil , NewInternalApiError (fmt .Errorf ("failed to get current enc key: %w" , err ))
4651 }
4752 serviceConfig , err := key .Encrypt (shared .UnsafeFastStringToReadOnlyBytes (req .Config .ServiceConfig ))
4853 if err != nil {
49- return nil , exceptions . NewInternalApiError (fmt .Errorf ("failed to encrypt alert config: %w" , err ))
54+ return nil , NewInternalApiError (fmt .Errorf ("failed to encrypt alert config: %w" , err ))
5055 }
5156
5257 if req .Config .Id == - 1 {
@@ -69,7 +74,7 @@ func (h *FlowRequestHandler) PostAlertConfig(ctx context.Context, req *protos.Po
6974 key .ID ,
7075 req .Config .AlertForMirrors ,
7176 ).Scan (& id ); err != nil {
72- return nil , exceptions . NewInternalApiError (fmt .Errorf ("failed to insert alert config: %w" , err ))
77+ return nil , NewInternalApiError (fmt .Errorf ("failed to insert alert config: %w" , err ))
7378 }
7479 return & protos.PostAlertConfigResponse {Id : id }, nil
7580 } else if _ , err := h .pool .Exec (
@@ -81,17 +86,17 @@ func (h *FlowRequestHandler) PostAlertConfig(ctx context.Context, req *protos.Po
8186 req .Config .AlertForMirrors ,
8287 req .Config .Id ,
8388 ); err != nil {
84- return nil , exceptions . NewInternalApiError (fmt .Errorf ("failed to update alert config: %w" , err ))
89+ return nil , NewInternalApiError (fmt .Errorf ("failed to update alert config: %w" , err ))
8590 }
8691 return & protos.PostAlertConfigResponse {Id : req .Config .Id }, nil
8792}
8893
8994func (h * FlowRequestHandler ) DeleteAlertConfig (
9095 ctx context.Context ,
9196 req * protos.DeleteAlertConfigRequest ,
92- ) (* protos.DeleteAlertConfigResponse , error ) {
97+ ) (* protos.DeleteAlertConfigResponse , APIError ) {
9398 if _ , err := h .pool .Exec (ctx , "delete from peerdb_stats.alerting_config where id = $1" , req .Id ); err != nil {
94- return nil , exceptions . NewInternalApiError (fmt .Errorf ("failed to delete alert config: %w" , err ))
99+ return nil , NewInternalApiError (fmt .Errorf ("failed to delete alert config: %w" , err ))
95100 }
96101 return & protos.DeleteAlertConfigResponse {}, nil
97102}
0 commit comments