1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright © 2019 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package wrap_test
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/gonvenience/wrap"
)
var _ = Describe("wrap package tests", func() {
var exampleErr = fmt.Errorf("failed to do x, because of y")
Context("wrapping errors in context", func() {
var err = Error(
exampleErr,
"issue setting up z",
)
It("should behave and render like a standard error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(BeEquivalentTo("issue setting up z: failed to do x, because of y"))
})
It("should fall back to a simple error if no cause is provided", func() {
err := Error(nil, "failed to do thing A")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(BeEquivalentTo("failed to do thing A"))
})
It("should be able to just extract the context string", func() {
switch contextError := err.(type) {
case ContextError:
Expect(contextError.Context()).To(BeEquivalentTo("issue setting up z"))
default:
Fail("failed to type cast to ContextError")
}
})
It("should be able to just extract the error cause", func() {
switch contextError := err.(type) {
case ContextError:
Expect(contextError.Cause().Error()).To(BeEquivalentTo("failed to do x, because of y"))
default:
Fail("failed to type cast to ContextError")
}
})
})
Context("wrapping multiple errors with context", func() {
var (
err = Errorsf(
[]error{
fmt.Errorf("issue setting up x"),
fmt.Errorf("issue setting up y"),
fmt.Errorf("issue setting up z"),
},
"failed to setup component %s", "A",
)
)
It("should behave and render like a standard error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(BeEquivalentTo("failed to setup component A:\n- issue setting up x\n- issue setting up y\n- issue setting up z"))
})
It("should fall back to a simple error if no cause is provided", func() {
err := Errors(nil, "failed to do thing A")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(BeEquivalentTo("failed to do thing A"))
})
It("should render like a simple wrapped error if there is only one error list entry", func() {
err := Errorsf(
[]error{
fmt.Errorf("issue setting up x"),
},
"failed to setup component %s", "A",
)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(BeEquivalentTo("failed to setup component A: issue setting up x"))
})
It("should be able to just extract the context string", func() {
switch contextError := err.(type) {
case ListOfErrors:
Expect(contextError.Context()).To(BeEquivalentTo("failed to setup component A"))
default:
Fail("failed to type cast to ContextError")
}
})
It("should be able to just extract the list of errors", func() {
switch contextError := err.(type) {
case ListOfErrors:
Expect(contextError.Errors()).To(BeEquivalentTo([]error{
fmt.Errorf("issue setting up x"),
fmt.Errorf("issue setting up y"),
fmt.Errorf("issue setting up z"),
}))
default:
Fail("failed to type cast to ContextError")
}
})
})
Context("projects using wrap package", func() {
It("should be possible to use an error to wrap with context", func() {
err := Errorf(exampleErr,
"unable to set up %s and %s",
"A",
"B")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(BeEquivalentTo("unable to set up A and B: failed to do x, because of y"))
})
})
})
|