Make errors from updateUsageData(...) crash when running in non-prod to ensure they're detected/handled before deployment

This commit is contained in:
David Dworken 2023-09-17 12:03:42 -07:00
parent 60406916e9
commit 04a0fc40cd
No known key found for this signature in database
3 changed files with 13 additions and 16 deletions

View File

@ -186,4 +186,3 @@ func main() {
}
// TODO(optimization): Maybe optimize the endpoints a bit to reduce the number of round trips required?
// TODO: Add error checking for the calls to updateUsageData(...) that logs it/triggers an alert in prod, but is an error in test

View File

@ -32,9 +32,7 @@ func (s *Server) apiSubmitHandler(w http.ResponseWriter, r *http.Request) {
version := getHishtoryVersion(r)
remoteIPAddr := getRemoteAddr(r)
if err := s.updateUsageData(r.Context(), version, remoteIPAddr, entries[0].UserId, entries[0].DeviceId, len(entries), false); err != nil {
fmt.Printf("updateUsageData: %v\n", err)
}
s.handleNonCriticalError(s.updateUsageData(r.Context(), version, remoteIPAddr, entries[0].UserId, entries[0].DeviceId, len(entries), false))
devices, err := s.db.DevicesForUser(r.Context(), entries[0].UserId)
checkGormError(err)
@ -64,9 +62,7 @@ func (s *Server) apiBootstrapHandler(w http.ResponseWriter, r *http.Request) {
version := getHishtoryVersion(r)
remoteIPAddr := getRemoteAddr(r)
if err := s.updateUsageData(r.Context(), version, remoteIPAddr, userId, deviceId, 0, false); err != nil {
fmt.Printf("updateUsageData: %v\n", err)
}
s.handleNonCriticalError(s.updateUsageData(r.Context(), version, remoteIPAddr, userId, deviceId, 0, false))
historyEntries, err := s.db.AllHistoryEntriesForUser(r.Context(), userId)
checkGormError(err)
fmt.Printf("apiBootstrapHandler: Found %d entries\n", len(historyEntries))
@ -84,9 +80,7 @@ func (s *Server) apiQueryHandler(w http.ResponseWriter, r *http.Request) {
version := getHishtoryVersion(r)
remoteIPAddr := getRemoteAddr(r)
if err := s.updateUsageData(r.Context(), version, remoteIPAddr, userId, deviceId, 0, true); err != nil {
fmt.Printf("updateUsageData: %v\n", err)
}
s.handleNonCriticalError(s.updateUsageData(r.Context(), version, remoteIPAddr, userId, deviceId, 0, true))
// Delete any entries that match a pending deletion request
deletionRequests, err := s.db.DeletionRequestsForUserAndDevice(r.Context(), userId, deviceId)
@ -156,9 +150,7 @@ func (s *Server) apiSubmitDumpHandler(w http.ResponseWriter, r *http.Request) {
version := getHishtoryVersion(r)
remoteIPAddr := getRemoteAddr(r)
if err := s.updateUsageData(r.Context(), version, remoteIPAddr, userId, srcDeviceId, len(entries), false); err != nil {
fmt.Printf("updateUsageData: %v\n", err)
}
s.handleNonCriticalError(s.updateUsageData(r.Context(), version, remoteIPAddr, userId, srcDeviceId, len(entries), false))
w.Header().Set("Content-Length", "0")
w.WriteHeader(http.StatusOK)
@ -226,9 +218,7 @@ func (s *Server) apiRegisterHandler(w http.ResponseWriter, r *http.Request) {
version := getHishtoryVersion(r)
remoteIPAddr := getRemoteAddr(r)
if err := s.updateUsageData(r.Context(), version, remoteIPAddr, userId, deviceId, 0, false); err != nil {
fmt.Printf("updateUsageData: %v\n", err)
}
s.handleNonCriticalError(s.updateUsageData(r.Context(), version, remoteIPAddr, userId, deviceId, 0, false))
if s.statsd != nil {
s.statsd.Incr("hishtory.register", []string{}, 1.0)

View File

@ -330,6 +330,14 @@ func (s *Server) getNumConnectionsHandler(w http.ResponseWriter, r *http.Request
_, _ = fmt.Fprintf(w, "%#v", stats.OpenConnections)
}
func (s *Server) handleNonCriticalError(err error) {
if s.isProductionEnvironment {
fmt.Printf("Unexpected non-critical error: %v", err)
} else {
panic(fmt.Errorf("unexpected non-critical error: %w", err))
}
}
func (s *Server) updateUsageData(ctx context.Context, version string, remoteAddr string, userId, deviceId string, numEntriesHandled int, isQuery bool) error {
var usageData []shared.UsageData
usageData, err := s.db.UsageDataFindByUserAndDevice(ctx, userId, deviceId)