Skip to content

Commit

Permalink
fix: get returns sql.ErrNoRows when hardware not found to avoid 'Unkn…
Browse files Browse the repository at this point in the history
…own desc = unexpected end of JSON input' error

Signed-off-by: Kelly Deng <kelly@packet.com>
  • Loading branch information
kqdeng committed Nov 18, 2020
1 parent ce52ece commit 7473ee9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
13 changes: 3 additions & 10 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,11 @@ func get(ctx context.Context, db *sql.DB, query string, args ...interface{}) (st

buf := []byte{}
err := row.Scan(&buf)
if err == nil {
return string(buf), nil
}

if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT")
if err != nil {
logger.Error(err)
} else {
err = nil
return "", errors.Wrap(err, "SELECT")
}

return "", err
return string(buf), nil
}

// buildGetCondition builds a where condition string in the format "column_name = 'field_value' AND"
Expand Down
7 changes: 4 additions & 3 deletions db/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (d TinkDB) CreateWorkflow(ctx context.Context, wf Workflow, data string, id

err = insertActionList(ctx, d.instance, data, id, tx)
if err != nil {
return errors.Wrap(err, "Failed to insert in workflow_state")
return errors.Wrap(err, "failed to insert in workflow_state")

}
err = insertInWorkflow(ctx, d.instance, wf, tx)
Expand Down Expand Up @@ -109,9 +109,10 @@ func insertActionList(ctx context.Context, db *sql.DB, yamlData string, id uuid.

workerID, err := getWorkerID(ctx, db, task.WorkerAddr)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return errors.Wrapf(err, "hardware %s not found", task.WorkerAddr)
}
return err
} else if workerID == "" {
return fmt.Errorf("hardware mentioned with reference %s not found", task.WorkerAddr)
}
workerUID, err := uuid.Parse(workerID)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions grpc-server/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func (s *server) CreateWorkflow(ctx context.Context, in *workflow.CreateRequest)
if pqErr := db.Error(err); pqErr != nil {
l = l.With("detail", pqErr.Detail, "where", pqErr.Where)
}
l.Error(err)
return &workflow.CreateResponse{}, err
e := errors.Wrap(err, "failed to create workflow")
l.Error(e)
return &workflow.CreateResponse{}, e
}

l := logger.With("workflowID", id.String())
Expand Down

0 comments on commit 7473ee9

Please sign in to comment.