go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipd/common/cipderr/cipderr_test.go (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cipderr
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"go.chromium.org/luci/common/errors"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  )
    26  
    27  func TestError(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	Convey("Setting and getting code", t, func() {
    31  		So(ToCode(nil), ShouldEqual, Unknown)
    32  		So(ToCode(fmt.Errorf("old school")), ShouldEqual, Unknown)
    33  		So(ToCode(errors.New("new")), ShouldEqual, Unknown)
    34  
    35  		So(ToCode(errors.New("tagged", Auth)), ShouldEqual, Auth)
    36  		So(ToCode(errors.New("tagged", Auth.WithDetails(Details{
    37  			Package: "zzz",
    38  		}))), ShouldEqual, Auth)
    39  
    40  		err1 := errors.Annotate(errors.New("tagged", Auth), "deeper").Err()
    41  		So(ToCode(err1), ShouldEqual, Auth)
    42  
    43  		err2 := errors.Annotate(errors.New("tagged", Auth), "deeper").Tag(IO).Err()
    44  		So(ToCode(err2), ShouldEqual, IO)
    45  
    46  		err3 := errors.MultiError{
    47  			fmt.Errorf("old school"),
    48  			errors.New("1", Auth),
    49  			errors.New("1", IO),
    50  		}
    51  		So(ToCode(err3), ShouldEqual, Auth)
    52  
    53  		err4 := errors.Annotate(context.DeadlineExceeded, "blah").Err()
    54  		So(ToCode(err4), ShouldEqual, Timeout)
    55  	})
    56  
    57  	Convey("Details", t, func() {
    58  		So(ToDetails(nil), ShouldBeNil)
    59  		So(ToDetails(errors.New("blah", Auth)), ShouldBeNil)
    60  
    61  		d := Details{Package: "a", Version: "b"}
    62  		So(ToDetails(errors.New("blah", Auth.WithDetails(d))), ShouldResemble, &d)
    63  
    64  		var err error
    65  		AttachDetails(&err, d)
    66  		So(err, ShouldBeNil)
    67  
    68  		err = errors.New("blah", Auth)
    69  		AttachDetails(&err, d)
    70  		So(ToCode(err), ShouldEqual, Auth)
    71  		So(ToDetails(err), ShouldResemble, &d)
    72  
    73  		err = errors.New("blah", Auth.WithDetails(Details{Package: "zzz"}))
    74  		AttachDetails(&err, d)
    75  		So(ToCode(err), ShouldEqual, Auth)
    76  		So(ToDetails(err), ShouldResemble, &d)
    77  	})
    78  }