Fix bugs in error handling for creation of live queries (#2181)

- Return the correct result type in error cases
- Handle nil values for campaign when logging
This commit is contained in:
Zachary Wasserman 2020-01-13 17:35:42 -08:00 committed by GitHub
parent a83a26b279
commit ef1d69c371
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 6 deletions

View File

@ -39,9 +39,9 @@ func makeCreateDistributedQueryCampaignEndpoint(svc kolide.Service) endpoint.End
req := request.(createDistributedQueryCampaignRequest)
campaign, err := svc.NewDistributedQueryCampaign(ctx, req.Query, req.Selected.Hosts, req.Selected.Labels)
if err != nil {
return createQueryResponse{Err: err}, nil
return createDistributedQueryCampaignResponse{Err: err}, nil
}
return createDistributedQueryCampaignResponse{campaign, nil}, nil
return createDistributedQueryCampaignResponse{Campaign: campaign}, nil
}
}
@ -64,9 +64,9 @@ func makeCreateDistributedQueryCampaignByNamesEndpoint(svc kolide.Service) endpo
req := request.(createDistributedQueryCampaignByNamesRequest)
campaign, err := svc.NewDistributedQueryCampaignByNames(ctx, req.Query, req.Selected.Hosts, req.Selected.Labels)
if err != nil {
return createQueryResponse{Err: err}, nil
return createDistributedQueryCampaignResponse{Err: err}, nil
}
return createDistributedQueryCampaignResponse{campaign, nil}, nil
return createDistributedQueryCampaignResponse{Campaign: campaign}, nil
}
}

View File

@ -20,12 +20,16 @@ func (mw loggingMiddleware) NewDistributedQueryCampaign(ctx context.Context, que
loggedInUser = vc.Username()
}
defer func(begin time.Time) {
var numHosts uint = 0
if campaign != nil {
numHosts = campaign.Metrics.TotalHosts
}
_ = mw.logger.Log(
"method", "NewDistributedQueryCampaign",
"err", err,
"user", loggedInUser,
"sql", queryString,
"numHosts", campaign.Metrics.TotalHosts,
"numHosts", numHosts,
"took", time.Since(begin),
)
}(time.Now())
@ -44,11 +48,15 @@ func (mw loggingMiddleware) NewDistributedQueryCampaignByNames(ctx context.Conte
loggedInUser = vc.Username()
}
defer func(begin time.Time) {
var numHosts uint = 0
if campaign != nil {
numHosts = campaign.Metrics.TotalHosts
}
_ = mw.logger.Log(
"method", "NewDistributedQueryCampaignByNames",
"err", err,
"user", loggedInUser,
"numHosts", campaign.Metrics.TotalHosts,
"numHosts", numHosts,
"took", time.Since(begin),
)
}(time.Now())