From b72a6f93c2c71ee6f952f48c02c1cae1587e3ea3 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Mon, 22 Feb 2021 10:52:03 +0100 Subject: fixed too dense error returns --- trillian.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/trillian.go b/trillian.go index 233d5e8..162ec77 100644 --- a/trillian.go +++ b/trillian.go @@ -12,19 +12,36 @@ import ( ) func checkQueueLeaf(rsp *trillian.QueueLeafResponse, err error) error { - if err != nil || rsp == nil || rsp.QueuedLeaf == nil { - return fmt.Errorf("%v", err) + if err != nil { + return fmt.Errorf("Trillian error: %v", err) + } + if rsp == nil { + return fmt.Errorf("Trillian error: empty response") + } + if rsp.QueuedLeaf == nil { + return fmt.Errorf("Trillian error: empty QueuedLeaf") } if codes.Code(rsp.QueuedLeaf.GetStatus().GetCode()) == codes.AlreadyExists { - // no need to report this as an invalid request, just (re)issue sdi glog.V(3).Infof("queued leaf is a duplicate => %X", rsp.QueuedLeaf.Leaf.LeafValue) } return nil } func checkGetLeavesByRange(req *GetEntriesRequest, rsp *trillian.GetLeavesByRangeResponse, err error) (int, error) { - if err != nil || rsp == nil || len(rsp.Leaves) == 0 || rsp.SignedLogRoot == nil || rsp.SignedLogRoot.LogRoot == nil { - return http.StatusInternalServerError, fmt.Errorf("%v", err) // TODO: break up into multiple returns? + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("Trillian Error: %v", err) + } + if rsp == nil { + return http.StatusInternalServerError, fmt.Errorf("Trillian error: empty response") + } + if rsp.SignedLogRoot == nil { + return http.StatusInternalServerError, fmt.Errorf("Trillian error: no signed log root") + } + if rsp.SignedLogRoot.LogRoot == nil { + return http.StatusInternalServerError, fmt.Errorf("Trillian error: no log root") + } + if len(rsp.Leaves) == 0 { + return http.StatusInternalServerError, fmt.Errorf("Trillian error: no leaves") } if len(rsp.Leaves) > int(req.End-req.Start+1) { return http.StatusInternalServerError, fmt.Errorf("too many leaves: %d for [%d,%d]", len(rsp.Leaves), req.Start, req.End) @@ -45,26 +62,50 @@ func checkGetLeavesByRange(req *GetEntriesRequest, rsp *trillian.GetLeavesByRang return http.StatusInternalServerError, fmt.Errorf("invalid leaf index: wanted %d, got %d", req.Start+int64(i), leaf.LeafIndex) } } - return 0, nil + return http.StatusOK, nil } func checkGetInclusionProofByHash(lp *LogParameters, rsp *trillian.GetInclusionProofByHashResponse, err error) error { - if err != nil || rsp == nil || len(rsp.Proof) == 0 || rsp.Proof[0] == nil { - return fmt.Errorf("%v", err) + if err != nil { + return fmt.Errorf("Trillian Error: %v", err) + } + if rsp == nil { + return fmt.Errorf("Trillian error: empty response") + } + if len(rsp.Proof) == 0 { + return fmt.Errorf("Trillian error: no proofs") + } + if rsp.Proof[0] == nil { + return fmt.Errorf("Trillian error: no proof") } return checkHashPath(lp.HashType.Size(), rsp.Proof[0].Hashes) } func checkGetConsistencyProof(lp *LogParameters, rsp *trillian.GetConsistencyProofResponse, err error) error { - if err != nil || rsp == nil || rsp.Proof == nil { - return fmt.Errorf("%v", err) + if err != nil { + return fmt.Errorf("Trillian Error: %v", err) + } + if rsp == nil { + return fmt.Errorf("Trillian error: empty response") + } + if rsp.Proof == nil { + return fmt.Errorf("Trillian error: no proof") } return checkHashPath(lp.HashType.Size(), rsp.Proof.Hashes) } func checkGetLatestSignedLogRoot(lp *LogParameters, rsp *trillian.GetLatestSignedLogRootResponse, err error, out *types.LogRootV1) error { - if err != nil || rsp == nil || rsp.SignedLogRoot == nil || rsp.SignedLogRoot.LogRoot == nil { - return fmt.Errorf("%v", err) + if err != nil { + return fmt.Errorf("Trillian Error: %v", err) + } + if rsp == nil { + return fmt.Errorf("Trillian error: empty response") + } + if rsp.SignedLogRoot == nil { + return fmt.Errorf("Trillian error: no signed log root") + } + if rsp.SignedLogRoot.LogRoot == nil { + return fmt.Errorf("Trillian error: no log root") } if err := out.UnmarshalBinary(rsp.SignedLogRoot.LogRoot); err != nil { return fmt.Errorf("cannot unmarshal log root: %v", err) -- cgit v1.2.3