本文整理汇总了Golang中github.com/tideland/golib/errors.Annotate函数的典型用法代码示例。如果您正苦于以下问题:Golang Annotate函数的具体用法?Golang Annotate怎么用?Golang Annotate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Annotate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
示例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))}
}
示例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
}
示例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))
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}