本文整理汇总了Golang中github.com/vektra/errors.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: extendDockerId
// short docker id (12 char) is extended
func (d *docker) extendDockerId(shortId string) (string, error) {
for _, cinfo := range d.containersInfo {
if strings.HasPrefix(cinfo.Id, shortId) {
return cinfo.Id, nil
}
}
return "", errors.New(fmt.Sprintf("Could not find long docker id for %s\n", shortId))
}
开发者ID:sandlbn,项目名称:snap-plugin-collector-docker,代码行数:11,代码来源:docker.go
示例2: Abandon
func (c *Client) Abandon(name string) error {
sess, err := c.Session()
if err != nil {
return err
}
s, err := sess.Open()
if err != nil {
return err
}
defer s.Close()
_, err = s.Write([]byte{uint8(AbandonType)})
if err != nil {
return c.checkError(err)
}
enc := codec.NewEncoder(s, &msgpack)
msg := Abandon{
Name: name,
}
err = enc.Encode(&msg)
if err != nil {
return c.checkError(err)
}
buf := []byte{0}
_, err = io.ReadFull(s, buf)
if err != nil {
return c.checkError(err)
}
switch MessageType(buf[0]) {
case ErrorType:
var msgerr Error
err = codec.NewDecoder(s, &msgpack).Decode(&msgerr)
if err != nil {
return c.checkError(err)
}
return errors.New(msgerr.Error)
case SuccessType:
return nil
default:
return c.checkError(EProtocolError)
}
}
开发者ID:40a,项目名称:vega,代码行数:52,代码来源:service.go
示例3: nack
func (c *Client) nack(id MessageId) error {
sess, err := c.Session()
if err != nil {
return err
}
s, err := sess.Open()
if err != nil {
return err
}
defer s.Close()
_, err = s.Write([]byte{uint8(NackType)})
if err != nil {
return c.checkError(err)
}
enc := codec.NewEncoder(s, &msgpack)
msg := NackMessage{
MessageId: id,
}
if err := enc.Encode(&msg); err != nil {
return c.checkError(err)
}
buf := []byte{0}
_, err = io.ReadFull(s, buf)
if err != nil {
return c.checkError(err)
}
switch MessageType(buf[0]) {
case ErrorType:
var msgerr Error
err = codec.NewDecoder(s, &msgpack).Decode(&msgerr)
if err != nil {
return c.checkError(err)
}
return errors.New(msgerr.Error)
case SuccessType:
return nil
default:
return c.checkError(EProtocolError)
}
}
开发者ID:40a,项目名称:vega,代码行数:52,代码来源:service.go
示例4: Stats
func (c *Client) Stats() (*ClientStats, error) {
if c.conn == nil {
return nil, nil
}
sess, err := c.Session()
if err != nil {
return nil, err
}
s, err := sess.Open()
if err != nil {
return nil, err
}
_, err = s.Write([]byte{uint8(StatsType)})
buf := []byte{0}
_, err = io.ReadFull(s, buf)
if err != nil {
return nil, err
}
switch MessageType(buf[0]) {
case StatsResultType:
dec := codec.NewDecoder(s, &msgpack)
var res ClientStats
if err := dec.Decode(&res); err != nil {
return nil, c.checkError(err)
}
return &res, nil
case ErrorType:
var msgerr Error
err = codec.NewDecoder(s, &msgpack).Decode(&msgerr)
if err != nil {
return nil, c.checkError(err)
}
return nil, errors.New(msgerr.Error)
case SuccessType:
return nil, nil
default:
return nil, c.checkError(EProtocolError)
}
}
开发者ID:40a,项目名称:vega,代码行数:50,代码来源:service.go
示例5: Close
func (c *Client) Close() (err error) {
if c.conn == nil {
return nil
}
sess, err := c.Session()
if err != nil {
return err
}
s, err := sess.Open()
if err != nil {
return err
}
_, err = s.Write([]byte{uint8(CloseType)})
defer func() {
s.Close()
err = c.sess.Close()
c.sess = nil
c.conn = nil
}()
buf := []byte{0}
io.ReadFull(s, buf)
if err == nil {
switch MessageType(buf[0]) {
case ErrorType:
var msgerr Error
err = codec.NewDecoder(s, &msgpack).Decode(&msgerr)
if err != nil {
return c.checkError(err)
}
return errors.New(msgerr.Error)
case SuccessType:
return nil
default:
return c.checkError(EProtocolError)
}
}
return err
}
开发者ID:40a,项目名称:vega,代码行数:48,代码来源:service.go
示例6: Push
func (c *Client) Push(name string, body *Message) error {
sess, err := c.Session()
if err != nil {
return err
}
s, err := sess.Open()
if err != nil {
return err
}
defer s.Close()
_, err = s.Write([]byte{uint8(PushType)})
if err != nil {
return c.checkError(err)
}
enc := codec.NewEncoder(s, &msgpack)
msg := Push{
Name: name,
Message: body,
}
debugf("client %s: sending push request\n", c.addr)
if err := enc.Encode(&msg); err != nil {
return c.checkError(err)
}
buf := []byte{0}
debugf("client %s: waiting for response\n", c.addr)
_, err = io.ReadFull(s, buf)
if err != nil {
return c.checkError(err)
}
switch MessageType(buf[0]) {
case ErrorType:
debugf("client %s: got error\n", c.addr)
var msgerr Error
err = codec.NewDecoder(s, &msgpack).Decode(&msgerr)
if err != nil {
return c.checkError(err)
}
return errors.New(msgerr.Error)
case SuccessType:
debugf("client %s: got success\n", c.addr)
return nil
default:
debugf("client %s: got protocol error\n", c.addr)
return c.checkError(EProtocolError)
}
}
开发者ID:40a,项目名称:vega,代码行数:62,代码来源:service.go
示例7: LongPollCancelable
func (c *Client) LongPollCancelable(name string, til time.Duration, done chan struct{}) (*Delivery, error) {
sess, err := c.Session()
if err != nil {
return nil, err
}
s, err := sess.Open()
if err != nil {
return nil, err
}
defer s.Close()
_, err = s.Write([]byte{uint8(LongPollType)})
if err != nil {
return nil, c.checkError(err)
}
enc := codec.NewEncoder(s, &msgpack)
msg := LongPoll{
Name: name,
Duration: til.String(),
}
if err := enc.Encode(&msg); err != nil {
return nil, c.checkError(err)
}
delivered := make(chan struct{})
buf := []byte{0}
go func() {
_, err = io.ReadFull(s, buf)
close(delivered)
}()
select {
case <-done:
return nil, nil
case <-delivered:
// do the rest
}
switch MessageType(buf[0]) {
case ErrorType:
var msgerr Error
err = codec.NewDecoder(s, &msgpack).Decode(&msgerr)
if err != nil {
return nil, c.checkError(err)
}
return nil, errors.New(msgerr.Error)
case PollResultType:
dec := codec.NewDecoder(s, &msgpack)
var res PollResult
if err := dec.Decode(&res); err != nil {
return nil, c.checkError(err)
}
if res.Message == nil {
return nil, nil
}
del := &Delivery{
Message: res.Message,
Ack: func() error { return c.ack(res.Message.MessageId) },
Nack: func() error { return c.nack(res.Message.MessageId) },
}
return del, nil
default:
return nil, c.checkError(EProtocolError)
}
}
开发者ID:40a,项目名称:vega,代码行数:79,代码来源:service.go
示例8: Poll
func (c *Client) Poll(name string) (*Delivery, error) {
sess, err := c.Session()
if err != nil {
return nil, err
}
s, err := sess.Open()
if err != nil {
return nil, err
}
defer s.Close()
_, err = s.Write([]byte{uint8(PollType)})
if err != nil {
return nil, c.checkError(err)
}
enc := codec.NewEncoder(s, &msgpack)
msg := Poll{
Name: name,
}
if err := enc.Encode(&msg); err != nil {
return nil, c.checkError(err)
}
buf := []byte{0}
_, err = io.ReadFull(s, buf)
if err != nil {
return nil, c.checkError(err)
}
switch MessageType(buf[0]) {
case ErrorType:
var msgerr Error
err = codec.NewDecoder(s, &msgpack).Decode(&msgerr)
if err != nil {
return nil, c.checkError(err)
}
return nil, errors.New(msgerr.Error)
case PollResultType:
dec := codec.NewDecoder(s, &msgpack)
var res PollResult
if err := dec.Decode(&res); err != nil {
return nil, c.checkError(err)
}
if res.Message == nil {
return nil, nil
}
del := &Delivery{
Message: res.Message,
Ack: func() error { return c.ack(res.Message.MessageId) },
Nack: func() error { return c.nack(res.Message.MessageId) },
}
return del, nil
default:
return nil, c.checkError(EProtocolError)
}
}
开发者ID:40a,项目名称:vega,代码行数:69,代码来源:service.go
示例9: TestNewAuthorizeRequest
// Should pass
//
// * https://openid.net/specs/oauth-v2-multiple-response-types-1_0.html#Terminology
// The OAuth 2.0 specification allows for registration of space-separated response_type parameter values.
// If a Response Type contains one of more space characters (%20), it is compared as a space-delimited list of
// values in which the order of values does not matter.
func TestNewAuthorizeRequest(t *testing.T) {
ctrl := gomock.NewController(t)
store := NewMockStorage(ctrl)
defer ctrl.Finish()
redir, _ := url.Parse("https://foo.bar/cb")
for k, c := range []struct {
desc string
conf *Fosite
r *http.Request
query url.Values
expectedError error
mock func()
expect *AuthorizeRequest
}{
/* empty request */
{
desc: "empty request fails",
conf: &Fosite{Store: store},
r: &http.Request{},
expectedError: ErrInvalidClient,
mock: func() {
store.EXPECT().GetClient(gomock.Any()).Return(nil, errors.New("foo"))
},
},
/* invalid redirect uri */
{
desc: "invalid redirect uri fails",
conf: &Fosite{Store: store},
query: url.Values{"redirect_uri": []string{"invalid"}},
expectedError: ErrInvalidClient,
mock: func() {
store.EXPECT().GetClient(gomock.Any()).Return(nil, errors.New("foo"))
},
},
/* invalid client */
{
desc: "invalid client fails",
conf: &Fosite{Store: store},
query: url.Values{"redirect_uri": []string{"https://foo.bar/cb"}},
expectedError: ErrInvalidClient,
mock: func() {
store.EXPECT().GetClient(gomock.Any()).Return(nil, errors.New("foo"))
},
},
/* redirect client mismatch */
{
desc: "client and request redirects mismatch",
conf: &Fosite{Store: store},
query: url.Values{
"client_id": []string{"1234"},
},
expectedError: ErrInvalidRequest,
mock: func() {
store.EXPECT().GetClient("1234").Return(&SecureClient{RedirectURIs: []string{"invalid"}}, nil)
},
},
/* redirect client mismatch */
{
desc: "client and request redirects mismatch",
conf: &Fosite{Store: store},
query: url.Values{
"redirect_uri": []string{""},
"client_id": []string{"1234"},
},
expectedError: ErrInvalidRequest,
mock: func() {
store.EXPECT().GetClient("1234").Return(&SecureClient{RedirectURIs: []string{"invalid"}}, nil)
},
},
/* redirect client mismatch */
{
desc: "client and request redirects mismatch",
conf: &Fosite{Store: store},
query: url.Values{
"redirect_uri": []string{"https://foo.bar/cb"},
"client_id": []string{"1234"},
},
expectedError: ErrInvalidRequest,
mock: func() {
store.EXPECT().GetClient("1234").Return(&SecureClient{RedirectURIs: []string{"invalid"}}, nil)
},
},
/* no state */
{
desc: "no state",
conf: &Fosite{Store: store},
query: url.Values{
"redirect_uri": []string{"https://foo.bar/cb"},
"client_id": []string{"1234"},
"response_type": []string{"code"},
},
expectedError: ErrInvalidState,
mock: func() {
//.........这里部分代码省略.........
开发者ID:jesseward,项目名称:fosite,代码行数:101,代码来源:authorize_request_handler_test.go
示例10:
fsUtil.On("Mkdirs", mock.AnythingOfType("string")).Return(mkdirsError)
fsUtil.On("Copy", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(copyError)
fsUtil.On("CopyAll", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(copyAllError)
if initMap {
resMap = mapset.Init()
}
worker := new(common.ConfigWorker)
worker.ResourceMap = resMap
worker.FSUtil = fsUtil
err := getter.GetLocalResources("", resources, "dest", "subfolder", recursively, worker, constants.Standards)
assert.Equal(GinkgoT(), expectedError, err)
},
Entry("Bad input to reserve", false, true, []string{""}, nil, nil, nil, mapset.ErrEmptyInput),
Entry("Successful recursive copy", true, true, []string{"res"}, nil, nil, nil, nil),
Entry("Successful single copy", false, true, []string{"res"}, nil, nil, nil, nil),
Entry("Failure of single copy", false, true, []string{"res"}, nil, errors.New("single copy fail"), nil, errors.New("single copy fail")),
Entry("Mkdirs", false, true, []string{"res"}, errors.New("mkdirs error"), nil, nil, errors.New("mkdirs error")),
)
})
Describe("GetRemoteResources", func() {
DescribeTable("", func(downloadEntryError, tempDirError, openAndReadFileError, getResourcesError, parseV1_0_0Error, expectedError error) {
entries := []common.Entry{
{
Path: "",
},
}
getter := VCSAndLocalFSGetter{}
worker := new(common.ConfigWorker)
downloader := new(mocks.EntryDownloader)
downloader.On("DownloadEntry", entries[0], mock.AnythingOfType("string")).Return(downloadEntryError)
worker.Downloader = downloader
开发者ID:geramirez,项目名称:compliance-masonry,代码行数:31,代码来源:resourceGetter_test.go
示例11:
Context("when the ParseV1_0_0 will not be called", func() {
It("should not call it when passing in 1.0", func() {
Parse(parser, []byte(`schema_version: "1.0"`))
parser.AssertNotCalled(GinkgoT(), "ParseV1_0_0", data)
})
})
Context("when the ParseV1_0_0 will is called", func() {
BeforeEach(func() {
data = []byte(`schema_version: "1.0.0"`)
})
Context("when ParseV1_0_0 is passed valid data", func() {
It("should call ParseV1_0_0", func() {
_, err = Parse(parser, data)
parser.AssertCalled(GinkgoT(), "ParseV1_0_0", data)
assert.Equal(GinkgoT(), expectedError, err)
})
})
Context("when ParseV1_0_0 is passed invalid data", func() {
BeforeEach(func() {
expectedError = errors.New("can't parse yaml")
})
It("should call ParseV1_0_0 but return an error", func() {
_, err = Parse(parser, data)
parser.AssertCalled(GinkgoT(), "ParseV1_0_0", data)
assert.Equal(GinkgoT(), expectedError, err)
})
})
})
})
})
开发者ID:geramirez,项目名称:compliance-masonry,代码行数:30,代码来源:parse_test.go
示例12: Push
return nil, nil
}
default:
}
if val == nil {
return nil, nil
}
return NewDelivery(mailbox, val), nil
case <-time.Tick(til):
return nil, nil
}
}
var ENoMailbox = errors.New("No such mailbox available")
func (r *Registry) Push(name string, value *Message) error {
r.Lock()
defer r.Unlock()
if mailbox, ok := r.mailboxes[name]; ok {
return mailbox.Push(value)
}
return errors.Subject(ENoMailbox, name)
}
func (r *Registry) Declare(name string) error {
r.Lock()
defer r.Unlock()
开发者ID:40a,项目名称:vega,代码行数:31,代码来源:registry.go
示例13: Init
// Init returns an initialized NestedMap
func Init() MapSet {
return MapSet{mapOfSet: make(map[string]*set.Set)}
}
// Result is the result from any NestedMap operations
type Result struct {
Value string
Error error
Success bool
}
var (
// ErrEmptyInput represents that the input into the operation is not complete
ErrEmptyInput = errors.New("One or more inputs are empty")
)
// Reserve will put a space into the map for the value given that key. Will return false if there is already an entry.
func (m *MapSet) Reserve(key string, value string) (result Result) {
if key == "" || value == "" {
result.Error = ErrEmptyInput
return
}
var innerSet *set.Set
if _, ok := m.mapOfSet[key]; !ok {
innerSet = set.New()
m.mapOfSet[key] = innerSet
}
if m.mapOfSet[key].Has(value) {
result.Success = false
开发者ID:opencontrol,项目名称:compliance-masonry,代码行数:30,代码来源:map.go
示例14: HandleOffer
package kbase
import (
"github.com/gl-works/kbase/rpc"
"github.com/vektra/errors"
"golang.org/x/net/context"
)
var (
emptyresponse *rpc.KeyResponse = &rpc.KeyResponse{}
ErrMissingKspace = errors.New("ErrMissingKspace")
)
func (this *Kbase) HandleOffer(ctx context.Context, offer *rpc.Offer) (response *rpc.KeyResponse, err error) {
if kspace, ok := this.kspaces[offer.Space]; ok {
kspace.putOffer(offer.Key, offer.Server, offer.Value)
response = emptyresponse
} else {
err = ErrMissingKspace
}
return
}
func (this *Kbase) HandlePoll(ctx context.Context, poll *rpc.Poll) (response *rpc.KeyResponse, err error) {
if kspace, ok := this.kspaces[poll.Space]; ok {
kspace.lockoffers.RLock()
values := kspace.offers[poll.Key]
kspace.lockoffers.RUnlock()
response = &rpc.KeyResponse{Values: values}
} else {
开发者ID:gl-works,项目名称:kbase,代码行数:31,代码来源:handlers.go
示例15:
"net"
"strings"
"sync"
"time"
"github.com/hashicorp/yamux"
"github.com/ugorji/go/codec"
"github.com/vektra/errors"
"github.com/vektra/seconn"
)
const DefaultPort = 8475
var msgpack codec.MsgpackHandle
var EProtocolError = errors.New("protocol error")
var muxConfig = yamux.DefaultConfig()
type Service struct {
Address string
Registry Storage
listener net.Listener
wg sync.WaitGroup
closed bool
shutdown chan struct{}
lock sync.Mutex
}
开发者ID:40a,项目名称:vega,代码行数:31,代码来源:service.go
示例16: GeneratePrologueNote
}
g.printf("\n")
}
func (g *Generator) GeneratePrologueNote(note string) {
if note != "" {
g.printf("\n")
for _, n := range strings.Split(note, "\\n") {
g.printf("// %s\n", n)
}
g.printf("\n")
}
}
var ErrNotInterface = errors.New("expression not an interface")
func (g *Generator) printf(s string, vals ...interface{}) {
fmt.Fprintf(&g.buf, s, vals...)
}
var builtinTypes = map[string]bool{
"ComplexType": true,
"FloatType": true,
"IntegerType": true,
"Type": true,
"Type1": true,
"bool": true,
"byte": true,
"complex128": true,
"complex64": true,
开发者ID:itsarbit,项目名称:mockery,代码行数:31,代码来源:generator.go
示例17: Init
// Init returns an initialized NestedMap
func Init() MapSet {
return MapSet{mapOfSet: make(map[string]*set.Set)}
}
// Result is the result from any NestedMap operations
type Result struct {
Value string
Error error
Success bool
}
var (
// ErrEmptyInput represents that the input into the operation is not complete
ErrEmptyInput = errors.New("One or more inputs are empty")
// ErrAlreadyExists represents when a value already exists in the set.
ErrAlreadyExists = errors.New("Already exists")
)
// Reserve will put a space into the map for the value given that key. Will return false if there is already an entry.
func (m *MapSet) Reserve(key string, value string) (result Result) {
if key == "" || value == "" {
result.Error = ErrEmptyInput
return
}
var innerSet *set.Set
if _, ok := m.mapOfSet[key]; !ok {
innerSet = set.New()
m.mapOfSet[key] = innerSet
}
开发者ID:geramirez,项目名称:compliance-masonry,代码行数:30,代码来源:map.go
注:本文中的github.com/vektra/errors.New函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论