commit 643d13b91062e6bbd86bf9b93adffdfb47350b1b
parent 9b2c1d174c7c7137443de1f202cd4fd92fd9e207
Author: Chris Bracken <chris@bracken.jp>
Date: Thu, 11 Nov 2021 15:57:03 -0800
Return error from StoreReading
Also move the code that logs the error to stdout into the HTTP handler
for posting the data.
Diffstat:
2 files changed, 8 insertions(+), 11 deletions(-)
diff --git a/http/http.go b/http/http.go
@@ -45,7 +45,9 @@ func (s HttpServer) PostAirrohr(w http.ResponseWriter, r *http.Request) {
fmt.Println(t.String() + " Report received from sensor " + rpt.SensorId)
for _, v := range rpt.Values {
rdg := storage.Reading{rpt.SensorId, rpt.SoftwareVersion, t, v.Type, v.Value}
- s.ds.StoreReading(&rdg)
+ if err = s.ds.StoreReading(&rdg); err != nil {
+ fmt.Println("Error: failed to write sensor data: " + err.Error())
+ }
}
errorResponse(w, "Success", http.StatusOK)
}
diff --git a/storage/storage.go b/storage/storage.go
@@ -23,7 +23,7 @@ type Reading struct {
type DataStore interface {
Close()
- StoreReading(r *Reading)
+ StoreReading(r *Reading) error
}
type PostgresDataStore struct {
@@ -54,15 +54,9 @@ func (s *PostgresDataStore) Close() {
s.db.Close()
}
-func (s *PostgresDataStore) StoreReading(r *Reading) {
- if s.db == nil {
- panic("Not connected to database")
- }
+func (s *PostgresDataStore) StoreReading(r *Reading) error {
_, err := s.db.Exec(INSERT_READING, r.SensorId, r.SoftwareVersion, r.Time, r.Type, r.Value)
- if err != nil {
- fmt.Println("Error: failed to write sensor data")
- fmt.Println(err)
- }
+ return err
}
type NullDataStore struct {
@@ -75,5 +69,6 @@ func ConnectNull() *NullDataStore {
func (s *NullDataStore) Close() {
}
-func (s *NullDataStore) StoreReading(r *Reading) {
+func (s *NullDataStore) StoreReading(r *Reading) error {
+ return nil
}