Skip to content

Commit

Permalink
updated error format again
Browse files Browse the repository at this point in the history
  • Loading branch information
sagostin committed Sep 20, 2024
1 parent d765c12 commit d1aa661
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 57 deletions.
48 changes: 24 additions & 24 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func UpdateProbeTarget(db *mongo.Database, probeID primitive.ObjectID, newTarget

_, err := db.Collection("probes").UpdateOne(context.TODO(), filter, update)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "UpdateProbeTarget", Level: log.ErrorLevel, ObjectID: probeID, Message: "unable to update probe target", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.UpdateProbeTarget", Level: log.ErrorLevel, ObjectID: probeID, Message: "unable to update probe target", Error: err}.ToError()
}

return nil
Expand Down Expand Up @@ -106,7 +106,7 @@ func (a *Agent) UpdateTimestamp(db *mongo.Database) error {
}
atoi, err := strconv.Atoi(v)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "UpdateTimestamp", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to get version for agent", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.UpdateTimestamp", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to get version for agent", Error: err}.ToError()
}

splitVer = append(splitVer, atoi)
Expand All @@ -116,7 +116,7 @@ func (a *Agent) UpdateTimestamp(db *mongo.Database) error {
probe := Probe{Agent: a.ID}
pps, err2 := probe.GetAllProbesForAgent(db)
if err2 != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "UpdateTimestamp", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to get all probes for agent", Error: err2}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.UpdateTimestamp", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to get all probes for agent", Error: err2}.ToError()
}

hasSpeedtestServers := false
Expand All @@ -138,7 +138,7 @@ func (a *Agent) UpdateTimestamp(db *mongo.Database) error {
if err != nil {
return internal.ErrorFormat{
Package: "agent",
Function: "UpdateTimestamp",
Function: "agent.UpdateTimestamp",
Level: log.ErrorLevel,
ObjectID: a.ID,
Message: "unable to create speedtest servers probe for agent",
Expand All @@ -152,7 +152,7 @@ func (a *Agent) UpdateTimestamp(db *mongo.Database) error {
s3 := Probe{Agent: a.ID, Type: ProbeType_SPEEDTEST, Config: ProbeConfig{Target: []ProbeTarget{target}}}
err = s3.Create(db)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "UpdateTimestamp", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to create speedtest probe for agent", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.UpdateTimestamp", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to create speedtest probe for agent", Error: err}.ToError()
}
}
}
Expand All @@ -169,7 +169,7 @@ func (a *Agent) UpdateAgentVersion(version string, db *mongo.Database) error {

_, err := db.Collection("agents").UpdateOne(context.TODO(), filter, update)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "UpdateAgentVersion", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to update agent version", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.UpdateAgentVersion", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to update agent version", Error: err}.ToError()
}

return nil
Expand All @@ -182,7 +182,7 @@ func (a *Agent) Initialize(db *mongo.Database) error {

_, err := db.Collection("agents").UpdateOne(context.TODO(), filter, update)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Initialize", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to initialize agent", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Initialize", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to initialize agent", Error: err}.ToError()
}

return nil
Expand All @@ -195,12 +195,12 @@ func DeleteAgent(db *mongo.Database, agentID primitive.ObjectID) error {

err := DeleteProbesByAgentID(db, agentID)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "DeleteAgent", Level: log.ErrorLevel, ObjectID: agentID, Message: "unable to delete probes by agent id", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.DeleteAgent", Level: log.ErrorLevel, ObjectID: agentID, Message: "unable to delete probes by agent id", Error: err}.ToError()
}

_, err = db.Collection("agents").DeleteMany(context.TODO(), filter)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "DeleteAgent", Level: log.ErrorLevel, ObjectID: agentID, Message: "unable to delete agent by id", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.DeleteAgent", Level: log.ErrorLevel, ObjectID: agentID, Message: "unable to delete agent by id", Error: err}.ToError()
}

return nil
Expand All @@ -217,7 +217,7 @@ func (a *Agent) Deactivate(db *mongo.Database) error {
}
_, err := db.Collection("agents").UpdateOne(context.TODO(), filter, update)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Deactivate", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to deactivate agent", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Deactivate", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to deactivate agent", Error: err}.ToError()
}

return nil
Expand All @@ -229,7 +229,7 @@ func (a *Agent) DeInitialize(db *mongo.Database) error {

_, err := db.Collection("agents").UpdateOne(context.TODO(), filter, update)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "DeInitialize", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to de-initialize agent", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.DeInitialize", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to de-initialize agent", Error: err}.ToError()
}

return nil
Expand All @@ -254,29 +254,29 @@ func (a *Agent) Get(db *mongo.Database) error {

cursor, err := db.Collection("agents").Find(context.TODO(), filter)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to search for agent by id", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to search for agent by id", Error: err}.ToError()
}
var results []bson.D
if err = cursor.All(context.TODO(), &results); err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error cursoring through agents", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error cursoring through agents", Error: err}.ToError()
}

if len(results) > 1 {
return internal.ErrorFormat{Package: "internal.agent", Function: "Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "multiple agents match when getting using id", Error: err}.ToError() // edge case??
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "multiple agents match when getting using id", Error: err}.ToError() // edge case??
}

if len(results) == 0 {
return internal.ErrorFormat{Package: "internal.agent", Function: "Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "no agents match", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "no agents match", Error: err}.ToError()
}

doc, err := bson.Marshal(&results[0])
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to marshal get agents results[0]", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to marshal get agents results[0]", Error: err}.ToError()
}

err = bson.Unmarshal(doc, &a)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to marshal get agents result", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Get", Level: log.ErrorLevel, ObjectID: a.ID, Message: "unable to marshal get agents result", Error: err}.ToError()
}

return nil
Expand All @@ -292,43 +292,43 @@ func (a *Agent) Create(db *mongo.Database) error {

mar, err := bson.Marshal(a)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error marshalling agent data when creating agent", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error marshalling agent data when creating agent", Error: err}.ToError()
}
var b *bson.D
err = bson.Unmarshal(mar, &b)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error unmarshalling agent when creating agent", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error unmarshalling agent when creating agent", Error: err}.ToError()
}
result, err := db.Collection("agents").InsertOne(context.TODO(), b)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error creating agent", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error creating agent", Error: err}.ToError()
}

// also create netinfo probe
probe := Probe{Agent: a.ID, Type: ProbeType_NETWORKINFO}
err = probe.Create(db)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error creating network info probe", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error creating network info probe", Error: err}.ToError()
}

// also create system info probe
ss := Probe{Agent: a.ID, Type: ProbeType_SYSTEMINFO}
err = ss.Create(db)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error creating system info probe", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error creating system info probe", Error: err}.ToError()
}

s2 := Probe{Agent: a.ID, Type: ProbeType_SPEEDTEST_SERVERS}
err = s2.Create(db)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error creating speedtest servers probe", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error creating speedtest servers probe", Error: err}.ToError()
}
target := ProbeTarget{Target: "ok"}

s3 := Probe{Agent: a.ID, Type: ProbeType_SPEEDTEST, Config: ProbeConfig{Target: []ProbeTarget{target}}}
err = s3.Create(db)
if err != nil {
return internal.ErrorFormat{Package: "internal.agent", Function: "Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error creating speedtest probe", Error: err}.ToError()
return internal.ErrorFormat{Package: "internal.agent", Function: "agent.Create", Level: log.ErrorLevel, ObjectID: a.ID, Message: "error creating speedtest probe", Error: err}.ToError()
}

// todo output to loki??
Expand Down
Loading

0 comments on commit d1aa661

Please sign in to comment.