Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit ad501f7

Browse files
Correct errors (#78)
* Specify bad request error * Use Internal when possible * Clean leftover in utils * Fix after reviews * BadRequest for DAG
1 parent ba62a4a commit ad501f7

25 files changed

+69
-127
lines changed

chaincode/algo_aggregate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestAggregateAlgo(t *testing.T) {
5151
resp = mockStub.MockInvoke("42", args)
5252
assert.EqualValuesf(t, 200, resp.Status, "when querying an aggregate algo with status %d and message %s", resp.Status, resp.Message)
5353
algo := outputAggregateAlgo{}
54-
err = bytesToStruct(resp.Payload, &algo)
54+
err = json.Unmarshal(resp.Payload, &algo)
5555
assert.NoError(t, err, "when unmarshalling queried aggregate algo")
5656
expectedAlgo := outputAggregateAlgo{
5757
outputAlgo: outputAlgo{

chaincode/algo_composite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestCompositeAlgo(t *testing.T) {
5151
resp = mockStub.MockInvoke("42", args)
5252
assert.EqualValuesf(t, 200, resp.Status, "when querying a composite algo with status %d and message %s", resp.Status, resp.Message)
5353
algo := outputCompositeAlgo{}
54-
err = bytesToStruct(resp.Payload, &algo)
54+
err = json.Unmarshal(resp.Payload, &algo)
5555
assert.NoError(t, err, "when unmarshalling queried composite algo")
5656
expectedAlgo := outputCompositeAlgo{
5757
outputAlgo: outputAlgo{

chaincode/algo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestAlgo(t *testing.T) {
4949
resp = mockStub.MockInvoke("42", args)
5050
assert.EqualValuesf(t, 200, resp.Status, "when querying an algo with status %d and message %s", resp.Status, resp.Message)
5151
algo := outputAlgo{}
52-
err = bytesToStruct(resp.Payload, &algo)
52+
err = json.Unmarshal(resp.Payload, &algo)
5353
assert.NoError(t, err, "when unmarshalling queried objective")
5454
expectedAlgo := outputAlgo{
5555
Key: algoKey,

chaincode/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package main
1616

1717
import (
18-
"fmt"
18+
"chaincode/errors"
1919
"strings"
2020
)
2121

@@ -39,7 +39,7 @@ func queryFilter(db *LedgerDB, args []string) (elements interface{}, err error)
3939
"aggregatetuple~tag",
4040
}
4141
if !stringInSlice(inp.IndexName, validIndexNames) {
42-
err = fmt.Errorf("invalid indexName filter query: %s", inp.IndexName)
42+
err = errors.BadRequest("invalid indexName filter query: %s", inp.IndexName)
4343
return
4444
}
4545
indexName := inp.IndexName + "~key"

chaincode/compute_plan.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616

1717
import (
1818
"chaincode/errors"
19-
"fmt"
2019
"strconv"
2120
)
2221

@@ -31,7 +30,7 @@ func (inpTraintuple *inputTraintuple) Fill(inpCP inputComputePlanTraintuple, tra
3130
for _, InModelID := range inpCP.InModelsIDs {
3231
inModelKey, ok := traintupleKeysByID[InModelID]
3332
if !ok {
34-
return fmt.Errorf("model ID %s not found", InModelID)
33+
return errors.BadRequest("model ID %s not found", InModelID)
3534
}
3635
inpTraintuple.InModels = append(inpTraintuple.InModels, inModelKey)
3736
}
@@ -49,7 +48,7 @@ func (inpAggregatetuple *inputAggregatetuple) Fill(inpCP inputComputePlanAggrega
4948
for _, InModelID := range inpCP.InModelsIDs {
5049
inModelKey, ok := aggregatetupleKeysByID[InModelID]
5150
if !ok {
52-
return fmt.Errorf("model ID %s not found", InModelID)
51+
return errors.BadRequest("model ID %s not found", InModelID)
5352
}
5453
inpAggregatetuple.InModels = append(inpAggregatetuple.InModels, inModelKey)
5554
}
@@ -70,14 +69,14 @@ func (inpCompositeTraintuple *inputCompositeTraintuple) Fill(inpCP inputComputeP
7069
var ok bool
7170
inpCompositeTraintuple.InHeadModelKey, ok = traintupleKeysByID[inpCP.InHeadModelID]
7271
if !ok {
73-
return fmt.Errorf("head model ID %s not found", inpCP.InHeadModelID)
72+
return errors.BadRequest("head model ID %s not found", inpCP.InHeadModelID)
7473
}
7574
}
7675
if inpCP.InTrunkModelID != "" {
7776
var ok bool
7877
inpCompositeTraintuple.InTrunkModelKey, ok = traintupleKeysByID[inpCP.InTrunkModelID]
7978
if !ok {
80-
return fmt.Errorf("trunk model ID %s not found", inpCP.InTrunkModelID)
79+
return errors.BadRequest("trunk model ID %s not found", inpCP.InTrunkModelID)
8180
}
8281
}
8382
return nil
@@ -86,7 +85,7 @@ func (inpCompositeTraintuple *inputCompositeTraintuple) Fill(inpCP inputComputeP
8685
func (inpTesttuple *inputTesttuple) Fill(inpCP inputComputePlanTesttuple, traintupleKeysByID map[string]string) error {
8786
traintupleKey, ok := traintupleKeysByID[inpCP.TraintupleID]
8887
if !ok {
89-
return fmt.Errorf("traintuple ID %s not found", inpCP.TraintupleID)
88+
return errors.BadRequest("traintuple ID %s not found", inpCP.TraintupleID)
9089
}
9190
inpTesttuple.TraintupleKey = traintupleKey
9291
inpTesttuple.DataManagerKey = inpCP.DataManagerKey

chaincode/compute_plan_dag.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package main
22

3-
import "fmt"
3+
import "chaincode/errors"
44

55
// TrainingTask is a node of a ComputeDAG. It represents a training task
66
// (i.e. a Traintuple, a CompositeTraintuple or an Aggregatetuple)
@@ -75,7 +75,7 @@ func (dag *ComputeDAG) sort() error {
7575
current[i].Depth = depth
7676
final = append(final, current[i])
7777
if _, ok := IDPresents[current[i].ID]; ok {
78-
return fmt.Errorf("compute plan error: Duplicate training task ID: %s", current[i].ID)
78+
return errors.BadRequest("compute plan error: Duplicate training task ID: %s", current[i].ID)
7979
}
8080
IDPresents[current[i].ID] = current[i].Depth
8181
} else {
@@ -90,7 +90,7 @@ func (dag *ComputeDAG) sort() error {
9090
for _, c := range current {
9191
errorIDs = append(errorIDs, c.ID)
9292
}
93-
return fmt.Errorf("compute plan error: Cyclic or missing dependency among inModels IDs: %v", errorIDs)
93+
return errors.BadRequest("compute plan error: Cyclic or missing dependency among inModels IDs: %v", errorIDs)
9494
}
9595
i = 0
9696
current = temp

chaincode/compute_plan_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package main
1616

1717
import (
18-
"fmt"
1918
"testing"
2019

2120
"github.com/stretchr/testify/assert"
@@ -240,7 +239,7 @@ func validateTupleRank(t *testing.T, db *LedgerDB, expectedRank int, key string,
240239
assert.NoError(t, err)
241240
rank = tuple.Rank
242241
default:
243-
assert.NoError(t, fmt.Errorf("not implemented: %v", assetType))
242+
t.Errorf("not implemented: %s", assetType)
244243
}
245244
assert.Equal(t, expectedRank, rank, "Rank for tuple of type %v with key \"%s\" should be %d", assetType, key, expectedRank)
246245
}

chaincode/data.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616

1717
import (
1818
"chaincode/errors"
19-
"fmt"
2019
"strconv"
2120
"strings"
2221
)
@@ -53,10 +52,6 @@ func (dataManager *DataManager) Set(db *LedgerDB, inp inputDataManager) (string,
5352
// and returning corresponding dataSample hashes, associated dataManagers, testOnly and errors
5453
func setDataSample(db *LedgerDB, inp inputDataSample) (dataSampleHashes []string, dataSample DataSample, err error) {
5554
dataSampleHashes = inp.Hashes
56-
if err = checkHashes(dataSampleHashes); err != nil {
57-
err = errors.BadRequest(err)
58-
return
59-
}
6055
// check dataSample is not already in the ledger
6156
if existingKeys := checkDataSamplesExist(db, dataSampleHashes); existingKeys != nil {
6257
err = errors.Conflict("data samples with keys %s already exist", existingKeys).WithKeys(existingKeys)
@@ -92,11 +87,6 @@ func setDataSample(db *LedgerDB, inp inputDataSample) (dataSampleHashes []string
9287
// one or more dataSamplef
9388
func validateUpdateDataSample(db *LedgerDB, inp inputUpdateDataSample) (dataSampleHashes []string, dataManagerKeys []string, err error) {
9489
// TODO return full dataSample
95-
// check validity of dataSampleHashes
96-
if err = checkHashes(inp.Hashes); err != nil {
97-
err = errors.BadRequest(err)
98-
return
99-
}
10090
// check dataManagers exist and are owned by the transaction requester
10191
if err = checkDataManagerOwner(db, inp.DataManagerKeys); err != nil {
10292
return
@@ -323,7 +313,7 @@ func queryDataset(db *LedgerDB, args []string) (outputDataset, error) {
323313
func queryDataSamples(db *LedgerDB, args []string) ([]outputDataSample, error) {
324314
outDataSamples := []outputDataSample{}
325315
if len(args) != 0 {
326-
err := fmt.Errorf("incorrect number of arguments, expecting nothing")
316+
err := errors.BadRequest("incorrect number of arguments, expecting nothing")
327317
return outDataSamples, err
328318
}
329319
elementsKeys, err := db.GetIndexKeys("dataSample~dataManager~key", []string{"dataSample"})
@@ -393,7 +383,7 @@ func checkSameDataManager(db *LedgerDB, dataManagerKey string, dataSampleKeys []
393383
return testOnly, trainOnly, err
394384
}
395385
if !stringInSlice(dataManagerKey, dataSample.DataManagerKeys) {
396-
err = fmt.Errorf("dataSample do not belong to the same dataManager")
386+
err = errors.BadRequest("dataSample do not belong to the same dataManager")
397387
return testOnly, trainOnly, err
398388
}
399389
testOnly = testOnly && dataSample.TestOnly

chaincode/data_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestDataManager(t *testing.T) {
6666
resp = mockStub.MockInvoke("42", args)
6767
assert.EqualValuesf(t, 200, resp.Status, "when querying the dataManager, status %d and message %s", resp.Status, resp.Message)
6868
dataManager := outputDataManager{}
69-
err = bytesToStruct(resp.Payload, &dataManager)
69+
err = json.Unmarshal(resp.Payload, &dataManager)
7070
assert.NoError(t, err, "when unmarshalling queried dataManager")
7171
expectedDataManager := outputDataManager{
7272
ObjectiveKey: inpDataManager.ObjectiveKey,

chaincode/generate_examples_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func TestPipeline(t *testing.T) {
123123
assert.EqualValuesf(t, 200, resp.Status, "when adding traintuple with status %d and message %s", resp.Status, resp.Message)
124124
traintuple := outputTraintuple{}
125125
respTraintuple := resp.Payload
126-
if err := bytesToStruct(respTraintuple, &traintuple); err != nil {
126+
if err := json.Unmarshal(respTraintuple, &traintuple); err != nil {
127127
t.Errorf("when unmarshalling queried traintuple with error %s", err)
128128
}
129129
trainWorker := traintuple.Dataset.Worker
@@ -185,7 +185,7 @@ func TestPipeline(t *testing.T) {
185185
resp = mockStub.MockInvoke("42", args)
186186
respTesttuple := resp.Payload
187187
testtuple := outputTesttuple{}
188-
if err := bytesToStruct(respTesttuple, &testtuple); err != nil {
188+
if err := json.Unmarshal(respTesttuple, &testtuple); err != nil {
189189
t.Errorf("when unmarshalling queried testtuple with error %s", err)
190190
}
191191
testWorker := testtuple.Dataset.Worker

0 commit comments

Comments
 (0)