Skip to content

Commit

Permalink
Catch or explicitly ignore unhandled errors
Browse files Browse the repository at this point in the history
  • Loading branch information
geofffranks authored and ameowlia committed Sep 23, 2024
1 parent 12e2226 commit 6f8f115
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions db/conn_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (c *ConnWrapper) Query(query string, args ...interface{}) (*sql.Rows, error

func (c *ConnWrapper) QueryRow(query string, args ...interface{}) *sql.Row {
var result *sql.Row
// #nosec G104 - the Monitor function only returns an error if the passed function errors, which this doesn't. we just want to log queries in our counters here
c.Monitor.Monitor(func() error {
result = c.DB.QueryRow(query, args...)
return nil
Expand Down
1 change: 1 addition & 0 deletions db/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func GetConnectionPool(dbConfig Config, ctx context.Context) (*ConnWrapper, erro
dbConn := sqlx.NewDb(nativeDBConn, dbConfig.Type)

if err = dbConn.PingContext(ctx); err != nil {
// #nosec G104 - ignore errors closing a connection that failed to ping, prefer the ping error
dbConn.Close()
if netErr, ok := err.(*net.OpError); ok {
return nil, RetriableError{
Expand Down
2 changes: 2 additions & 0 deletions db/monitor_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (tx *monitoredTx) Rollback() error {

func (tx *monitoredTx) Rebind(query string) string {
var result string
// #nosec G104 - the Monitor function only returns an error if the passed function errors, which this doesn't. we just want to log queries in our counters here
tx.monitor.Monitor(func() error {
result = tx.tx.Rebind(query)
return nil
Expand All @@ -70,6 +71,7 @@ func (tx *monitoredTx) Rebind(query string) string {

func (tx *monitoredTx) DriverName() string {
var result string
// #nosec G104 - the Monitor function only returns an error if the passed function errors, which this doesn't. we just want to log queries in our counters here
tx.monitor.Monitor(func() error {
result = tx.tx.DriverName()
return nil
Expand Down
2 changes: 2 additions & 0 deletions httperror/error_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ func (e *ErrorResponse) respondWithCode(statusCode int, logger lager.Logger, w h
w.WriteHeader(statusCode)
if metadataError, ok := err.(MetadataError); ok {
j, _ := json.Marshal(metadataError.Metadata())
// #nosec G104 - ignore write error generated while writing out the error handling response to an http connection. it's already handling a more important error
w.Write([]byte(fmt.Sprintf(`{"error": "%s", "metadata": %s}`, description, j)))
} else {
// #nosec G104 - ignore write error generated while writing out the error handling response to an http connection. it's already handling a more important error
w.Write([]byte(fmt.Sprintf(`{"error": "%s"}`, description)))
}
e.MetricsSender.IncrementCounter(HTTP_ERROR_METRIC_NAME)
Expand Down
15 changes: 12 additions & 3 deletions lagerlevel/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ func (s *Server) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
break
}
if time.Now().After(timeOut) {
httpServer.Close()
cErr := httpServer.Close()
if cErr != nil {
s.logger.Error("failed to close http server", cErr)
}
return errors.New("failed to successfully connect to http server")
}
time.Sleep(100 * time.Millisecond)
Expand All @@ -74,10 +77,16 @@ func (s *Server) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
for {
select {
case err := <-exited:
httpServer.Close()
cErr := httpServer.Close()
if cErr != nil {
s.logger.Error("failed to close http server", cErr)
}
return err
case <-signals:
httpServer.Close()
cErr := httpServer.Close()
if cErr != nil {
s.logger.Error("failed to close http server", cErr)
}
return nil
}
}
Expand Down
5 changes: 4 additions & 1 deletion metrics/metrics_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func (m *MetricsEmitter) emitMetrics() {
continue
}

dropsondemetrics.SendValue(source.Name, value, source.Unit)
err = dropsondemetrics.SendValue(source.Name, value, source.Unit)
if err != nil {
m.logger.Error("metric-send", err, lager.Data{"source": source.Name})
}
}
}

Expand Down

0 comments on commit 6f8f115

Please sign in to comment.