本文整理汇总了Golang中github.com/tideland/golib/errors.Annotate函数的典型用法代码示例。如果您正苦于以下问题:Golang Annotate函数的具体用法?Golang Annotate怎么用?Golang Annotate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Annotate函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: ProcessEvent
// ProcessEvent reads, validates and emits a configuration.
func (b *configuratorBehavior) ProcessEvent(event cells.Event) error {
switch event.Topic() {
case ReadConfigurationTopic:
// Read configuration
filename, ok := event.Payload().GetString(ConfigurationFilenamePayload)
if !ok {
logger.Errorf("cannot read configuration without filename payload")
return nil
}
logger.Infof("reading configuration from %q", filename)
cfg, err := etc.ReadFile(filename)
if err != nil {
return errors.Annotate(err, ErrCannotReadConfiguration, errorMessages)
}
// If wanted then validate it.
if b.validate != nil {
err = b.validate(cfg)
if err != nil {
return errors.Annotate(err, ErrCannotValidateConfiguration, errorMessages)
}
}
// All done, emit it.
pvs := cells.PayloadValues{
ConfigurationPayload: cfg,
}
b.cell.EmitNewContext(ConfigurationTopic, pvs, event.Context())
}
return nil
}
开发者ID:tideland,项目名称:gocells,代码行数:30,代码来源:configurator.go
示例2: receiveResponse
// receiveResponse retrieves a response from the server.
func (r *resp) receiveResponse() *response {
// Receive first line.
line, err := r.reader.ReadBytes('\n')
if err != nil {
rerr := errors.Annotate(err, ErrConnectionBroken, errorMessages, "receive after "+r.cmd)
return &response{receivingError, 0, nil, rerr}
}
content := line[1 : len(line)-2]
// First byte defines kind.
switch line[0] {
case '+':
// Status response.
return &response{statusResponse, 0, line[:len(line)-2], nil}
case '-':
// Error response.
return &response{errorResponse, 0, line[:len(line)-2], nil}
case ':':
// Integer response.
return &response{integerResponse, 0, content, nil}
case '$':
// Bulk response or null bulk response.
count, err := strconv.Atoi(string(content))
if err != nil {
return &response{receivingError, 0, nil, errors.Annotate(err, ErrServerResponse, errorMessages)}
}
if count == -1 {
// Null bulk response.
return &response{nullBulkResponse, 0, nil, nil}
}
// Receive the bulk data.
toRead := count + 2
buffer := make([]byte, toRead)
n, err := io.ReadFull(r.reader, buffer)
if err != nil {
return &response{receivingError, 0, nil, err}
}
if n < toRead {
return &response{receivingError, 0, nil, errors.New(ErrServerResponse, errorMessages)}
}
return &response{bulkResponse, 0, buffer[0:count], nil}
case '*':
// Array reply. Check for timeout.
length, err := strconv.Atoi(string(content))
if err != nil {
return &response{receivingError, 0, nil, errors.Annotate(err, ErrServerResponse, errorMessages)}
}
if length == -1 {
// Timeout.
return &response{timeoutError, 0, nil, nil}
}
return &response{arrayResponse, length, nil, nil}
}
return &response{receivingError, 0, nil, errors.New(ErrInvalidResponse, errorMessages, string(line))}
}
开发者ID:kung-foo,项目名称:golib,代码行数:55,代码来源:resp.go
示例3: Read
// Read reads the SML source of the configuration from a
// reader, parses it, and returns the configuration instance.
func Read(source io.Reader) (Configuration, error) {
builder := sml.NewKeyStringValueTreeBuilder()
err := sml.ReadSML(source, builder)
if err != nil {
return nil, errors.Annotate(err, ErrIllegalSourceFormat, errorMessages)
}
tree, err := builder.Tree()
if err != nil {
return nil, errors.Annotate(err, ErrIllegalSourceFormat, errorMessages)
}
if err := tree.At("config").Error(); err != nil {
return nil, errors.Annotate(err, ErrIllegalSourceFormat, errorMessages)
}
return &configuration{tree}, nil
}
开发者ID:tjyang,项目名称:golib,代码行数:17,代码来源:configuration.go
示例4: ReadFile
// ReadFile reads the SML source of a configuration file,
// parses it, and returns the configuration instance.
func ReadFile(filename string) (Configuration, error) {
source, err := ioutil.ReadFile(filename)
if err != nil {
return nil, errors.Annotate(err, ErrCannotReadFile, errorMessages, filename)
}
return ReadString(string(source))
}
开发者ID:kung-foo,项目名称:golib,代码行数:9,代码来源:configuration.go
示例5: Post
// Post is specified on the PostResourceHandler interface.
func (h *FileUploadHandler) Post(ctx Context) (bool, error) {
if err := ctx.Request().ParseMultipartForm(defaultMaxMemory); err != nil {
return false, errors.Annotate(err, ErrUploadingFile, errorMessages)
}
for _, headers := range ctx.Request().MultipartForm.File {
for _, header := range headers {
logger.Infof("receiving file %q", header.Filename)
// Open file and process it.
if infile, err := header.Open(); err != nil {
return false, errors.Annotate(err, ErrUploadingFile, errorMessages)
} else if err := h.processor(ctx, header, infile); err != nil {
return false, errors.Annotate(err, ErrUploadingFile, errorMessages)
}
}
}
return true, nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:18,代码来源:handlers.go
示例6: DoAll
// DoAll implements the Set interface.
func (s *set) DoAll(f func(v interface{}) error) error {
for v := range s.values {
if err := f(v); err != nil {
return errors.Annotate(err, ErrDoAll, errorMessages)
}
}
return nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:9,代码来源:sets.go
示例7: Validate
// Validate checks if the GUID is valid.
func (g *GUID) Validate() error {
if g.IsPermaLink {
if _, err := url.Parse(g.GUID); err != nil {
return errors.Annotate(err, ErrParsing, errorMessages, "item GUID")
}
}
return nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:9,代码来源:rss.go
示例8: selectDatabase
// selectDatabase selects the database.
func (r *resp) selectDatabase() error {
err := r.sendCommand("select", r.database.index)
if err != nil {
return errors.Annotate(err, ErrSelectDatabase, errorMessages)
}
result, err := r.receiveResultSet()
if err != nil {
return errors.Annotate(err, ErrSelectDatabase, errorMessages)
}
value, err := result.ValueAt(0)
if err != nil {
return errors.Annotate(err, ErrSelectDatabase, errorMessages)
}
if !value.IsOK() {
return errors.New(ErrSelectDatabase, errorMessages)
}
return nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:19,代码来源:resp.go
示例9: authenticate
// authenticate authenticates against the server if configured.
func (r *resp) authenticate() error {
if r.database.password != "" {
err := r.sendCommand("auth", r.database.password)
if err != nil {
return errors.Annotate(err, ErrAuthenticate, errorMessages)
}
result, err := r.receiveResultSet()
if err != nil {
return errors.Annotate(err, ErrAuthenticate, errorMessages)
}
value, err := result.ValueAt(0)
if err != nil {
return errors.Annotate(err, ErrAuthenticate, errorMessages)
}
if !value.IsOK() {
return errors.New(ErrAuthenticate, errorMessages)
}
}
return nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:21,代码来源:resp.go
示例10: cleanupAllProps
// cleanupAllProps cleans all props.
func (s *scene) cleanupAllProps() error {
for _, box := range s.props {
if box.cleanup != nil {
err := box.cleanup(box.key, box.prop)
if err != nil {
return errors.Annotate(err, ErrCleanupFailed, errorMessages, box.key)
}
}
}
return nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:12,代码来源:scene.go
示例11: GetFloat64
// GetFloat64 implements the Value interface.
func (v *value) GetFloat64() (float64, error) {
raw, err := v.Get()
if err != nil {
return 0.0, err
}
fv, err := strconv.ParseFloat(raw, 64)
if err != nil {
return 0.0, errors.Annotate(err, ErrInvalidFormat, errorMessages, raw)
}
return fv, nil
}
开发者ID:tjyang,项目名称:golib,代码行数:12,代码来源:configuration.go
示例12: GetInt
// GetInt implements the Value interface.
func (v *value) GetInt() (int, error) {
raw, err := v.Get()
if err != nil {
return 0, err
}
iv, err := strconv.ParseInt(raw, 10, 0)
if err != nil {
return 0, errors.Annotate(err, ErrInvalidFormat, errorMessages, raw)
}
return int(iv), nil
}
开发者ID:tjyang,项目名称:golib,代码行数:12,代码来源:configuration.go
示例13: GetTime
// GetTime implements the Value interface.
func (v *value) GetTime() (time.Time, error) {
raw, err := v.Get()
if err != nil {
return time.Time{}, err
}
tv, err := time.Parse(time.RFC3339, raw)
if err != nil {
return tv, errors.Annotate(err, ErrInvalidFormat, errorMessages, raw)
}
return tv, nil
}
开发者ID:tjyang,项目名称:golib,代码行数:12,代码来源:configuration.go
示例14: GetBool
// GetBool implements the Value interface.
func (v *value) GetBool() (bool, error) {
raw, err := v.Get()
if err != nil {
return false, err
}
bv, err := strconv.ParseBool(raw)
if err != nil {
return false, errors.Annotate(err, ErrInvalidFormat, errorMessages, raw)
}
return bv, nil
}
开发者ID:tjyang,项目名称:golib,代码行数:12,代码来源:configuration.go
示例15: GetDuration
// GetDuration implements the Value interface.
func (v *value) GetDuration() (time.Duration, error) {
raw, err := v.Get()
if err != nil {
return time.Duration(0), err
}
dv, err := time.ParseDuration(raw)
if err != nil {
return dv, errors.Annotate(err, ErrInvalidFormat, errorMessages, raw)
}
return dv, nil
}
开发者ID:tjyang,项目名称:golib,代码行数:12,代码来源:configuration.go
示例16: newCell
// newCell create a new cell around a behavior.
func newCell(env *environment, id string, behavior Behavior) (*cell, error) {
logger.Infof("cell '%s' starts", id)
// Init cell runtime.
c := &cell{
env: env,
id: id,
measuringID: identifier.Identifier("cells", env.id, "cell", id),
behavior: behavior,
emitters: newConnections(),
subscribers: newConnections(),
emitTimeoutTicker: time.NewTicker(5 * time.Second),
}
// Set configuration.
if bebs, ok := behavior.(BehaviorEventBufferSize); ok {
size := bebs.EventBufferSize()
if size < minEventBufferSize {
size = minEventBufferSize
}
c.eventc = make(chan Event, size)
} else {
c.eventc = make(chan Event, minEventBufferSize)
}
if brf, ok := behavior.(BehaviorRecoveringFrequency); ok {
number, duration := brf.RecoveringFrequency()
if duration.Seconds()/float64(number) < 0.1 {
number = minRecoveringNumber
duration = minRecoveringDuration
}
c.recoveringNumber = number
c.recoveringDuration = duration
} else {
c.recoveringNumber = minRecoveringNumber
c.recoveringDuration = minRecoveringDuration
}
if bet, ok := behavior.(BehaviorEmitTimeout); ok {
timeout := bet.EmitTimeout()
switch {
case timeout < minEmitTimeout:
timeout = minEmitTimeout
case timeout > maxEmitTimeout:
timeout = maxEmitTimeout
}
c.emitTimeout = int(timeout.Seconds() / 5)
} else {
c.emitTimeout = int(maxEmitTimeout.Seconds() / 5)
}
// Init behavior.
if err := behavior.Init(c); err != nil {
return nil, errors.Annotate(err, ErrCellInit, errorMessages, id)
}
// Start backend.
c.loop = loop.GoRecoverable(c.backendLoop, c.checkRecovering, id)
return c, nil
}
开发者ID:tideland,项目名称:gocells,代码行数:55,代码来源:cell.go
示例17: NewUUIDByHex
// NewUUIDByHex creates a UUID based on the passed hex string which has to
// have the length of 32 bytes.
func NewUUIDByHex(source string) (UUID, error) {
uuid := UUID{}
if len([]byte(source)) != 32 {
return uuid, errors.New(ErrInvalidHexLength, errorMessages)
}
raw, err := hex.DecodeString(source)
if err != nil {
return uuid, errors.Annotate(err, ErrInvalidHexValue, errorMessages)
}
copy(uuid[:], raw)
return uuid, nil
}
开发者ID:kung-foo,项目名称:cellchat,代码行数:14,代码来源:uuid.go
示例18: checkRecovering
// checkRecovering checks if the cell may recover after a panic. It will
// signal an error and let the cell stop working if there have been 12 recoverings
// during the last minute or the behaviors Recover() signals, that it cannot
// handle the error.
func (c *cell) checkRecovering(rs loop.Recoverings) (loop.Recoverings, error) {
logger.Errorf("recovering cell %q after error: %v", c.id, rs.Last().Reason)
// Check frequency.
if rs.Frequency(c.recoveringNumber, c.recoveringDuration) {
return nil, errors.New(ErrRecoveredTooOften, errorMessages, rs.Last().Reason)
}
// Try to recover.
if err := c.behavior.Recover(rs.Last().Reason); err != nil {
return nil, errors.Annotate(err, ErrEventRecovering, errorMessages, rs.Last().Reason)
}
return rs.Trim(c.recoveringNumber), nil
}
开发者ID:jmptrader,项目名称:golib,代码行数:16,代码来源:cell.go
示例19: newResp
// newResp establishes a connection to a Redis database
// based on the configuration of the passed database
// configuration.
func newResp(db *Database) (*resp, error) {
// Dial the database and create the protocol instance.
conn, err := net.DialTimeout(db.network, db.address, db.timeout)
if err != nil {
return nil, errors.Annotate(err, ErrConnectionEstablishing, errorMessages)
}
r := &resp{
database: db,
conn: conn,
reader: bufio.NewReader(conn),
}
return r, nil
}
开发者ID:kung-foo,项目名称:golib,代码行数:16,代码来源:resp.go
示例20: TestAnnotation
// Test the annotation of errors.
func TestAnnotation(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
ec := 123
messages := errors.Messages{ec: "annotated"}
aerr := testError("wrapped")
err := errors.Annotate(aerr, ec, messages)
assert.ErrorMatch(err, `\[ERRORS_TEST:123\] annotated: wrapped`)
assert.Equal(errors.Annotated(err), aerr)
assert.True(errors.IsInvalidTypeError(errors.Annotated(aerr)))
assert.Length(errors.Stack(err), 2)
}
开发者ID:jmptrader,项目名称:golib,代码行数:14,代码来源:errors_test.go
注:本文中的github.com/tideland/golib/errors.Annotate函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论