当前位置: 首页>>代码示例>>Golang>>正文


Golang errors.Wrap函数代码示例

本文整理汇总了Golang中github.com/go-errors/errors.Wrap函数的典型用法代码示例。如果您正苦于以下问题:Golang Wrap函数的具体用法?Golang Wrap怎么用?Golang Wrap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Wrap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ParseContents

// ParseContents sends a Composition for each block of the source container
func (g *Genie) ParseContents(onComp CompositionListener) error {
	patchWire := g.PatchWire

	// for each file, the patch contains a SyncHeader followed by a series of
	// operations, always ending in HEY_YOU_DID_IT
	sh := &pwr.SyncHeader{}
	for fileIndex, f := range g.SourceContainer.Files {
		sh.Reset()
		err := patchWire.ReadMessage(sh)
		if err != nil {
			return errors.Wrap(err, 1)
		}

		if sh.FileIndex != int64(fileIndex) {
			fmt.Printf("expected fileIndex = %d, got fileIndex %d\n", fileIndex, sh.FileIndex)
			return errors.Wrap(pwr.ErrMalformedPatch, 1)
		}

		err = g.analyzeFile(patchWire, int64(fileIndex), f.Size, onComp)
		if err != nil {
			return errors.Wrap(err, 1)
		}
	}

	return nil
}
开发者ID:itchio,项目名称:wharf,代码行数:27,代码来源:genie.go

示例2: GetReader

// GetReader returns an io.Reader for the file at index fileIndex
// Successive calls to `GetReader` will attempt to re-use the last
// returned reader if the file index is similar. The cache size is 1, so
// reading in parallel from different files is not supported.
func (cfp *ZipPool) GetReader(fileIndex int64) (io.Reader, error) {
	if cfp.fileIndex != fileIndex {
		if cfp.reader != nil {
			err := cfp.reader.Close()
			if err != nil {
				return nil, errors.Wrap(err, 1)
			}
			cfp.reader = nil
			cfp.fileIndex = -1
		}

		relPath := cfp.GetRelativePath(fileIndex)
		f := cfp.fmap[relPath]
		if f == nil {
			if os.Getenv("VERBOSE_ZIP_POOL") != "" {
				fmt.Printf("\nzip contents:\n")
				for k := range cfp.fmap {
					fmt.Printf("\n- %s", k)
				}
				fmt.Println()
			}
			return nil, errors.WrapPrefix(os.ErrNotExist, relPath, 1)
		}

		reader, err := f.Open()

		if err != nil {
			return nil, errors.Wrap(err, 1)
		}
		cfp.reader = reader
		cfp.fileIndex = fileIndex
	}

	return cfp.reader, nil
}
开发者ID:itchio,项目名称:wharf,代码行数:39,代码来源:zippool.go

示例3: ApplyPatch

// ApplyPatch applies the difference to the target.
func (ctx *Context) ApplyPatch(output io.Writer, pool Pool, ops chan Operation) error {
	blockSize := int64(ctx.blockSize)

	for op := range ops {
		switch op.Type {
		case OpBlockRange:
			target, err := pool.GetReadSeeker(op.FileIndex)
			if err != nil {
				return errors.Wrap(err, 1)
			}

			_, err = target.Seek(blockSize*op.BlockIndex, os.SEEK_SET)
			if err != nil {
				return errors.Wrap(err, 1)
			}

			_, err = io.CopyN(output, target, blockSize*op.BlockSpan)
			if err != nil {
				// EOF is actually expected, since we want to copy short
				// blocks at the end of files. Other errors aren't expected.
				if err != io.EOF {
					return errors.Wrap(fmt.Errorf("While copying %d bytes: %s", blockSize*op.BlockSpan, err.Error()), 1)
				}
			}
		case OpData:
			_, err := output.Write(op.Data)
			if err != nil {
				return errors.Wrap(err, 1)
			}
		}
	}

	return nil
}
开发者ID:itchio,项目名称:wharf,代码行数:35,代码来源:algo.go

示例4: ParseHeader

// ParseHeader is the first step of the genie's operation - it reads both
// containers, leaving the caller a chance to use them later, when parsing
// the contents
func (g *Genie) ParseHeader(patchReader io.Reader) error {
	rawPatchWire := wire.NewReadContext(patchReader)
	err := rawPatchWire.ExpectMagic(pwr.PatchMagic)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	header := &pwr.PatchHeader{}
	err = rawPatchWire.ReadMessage(header)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	patchWire, err := pwr.DecompressWire(rawPatchWire, header.Compression)
	if err != nil {
		return errors.Wrap(err, 1)
	}
	g.PatchWire = patchWire

	g.TargetContainer = &tlc.Container{}
	err = patchWire.ReadMessage(g.TargetContainer)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	g.SourceContainer = &tlc.Container{}
	err = patchWire.ReadMessage(g.SourceContainer)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	return nil
}
开发者ID:itchio,项目名称:wharf,代码行数:36,代码来源:genie.go

示例5: ExtractPath

func ExtractPath(archive string, destPath string, settings ExtractSettings) (*ExtractResult, error) {
	var result *ExtractResult
	var err error

	file, err := eos.Open(archive)
	if err != nil {
		return nil, errors.Wrap(err, 1)
	}

	stat, err := file.Stat()
	if err != nil {
		return nil, errors.Wrap(err, 1)
	}

	defer func() {
		if cErr := file.Close(); cErr != nil && err == nil {
			err = errors.Wrap(cErr, 1)
		}
	}()

	result, err = Extract(file, stat.Size(), destPath, settings)

	if err != nil {
		return nil, errors.Wrap(err, 1)
	}
	return result, nil
}
开发者ID:itchio,项目名称:wharf,代码行数:27,代码来源:archiver.go

示例6: Mkdir

func Mkdir(dstpath string) error {
	dirstat, err := os.Lstat(dstpath)
	if err != nil {
		// main case - dir doesn't exist yet
		err = os.MkdirAll(dstpath, DirMode)
		if err != nil {
			return errors.Wrap(err, 1)
		}
		return nil
	}

	if dirstat.IsDir() {
		// is already a dir, good!
	} else {
		// is a file or symlink for example, turn into a dir
		err = os.Remove(dstpath)
		if err != nil {
			return errors.Wrap(err, 1)
		}
		err = os.MkdirAll(dstpath, DirMode)
		if err != nil {
			return errors.Wrap(err, 1)
		}
	}

	return nil
}
开发者ID:itchio,项目名称:wharf,代码行数:27,代码来源:archiver.go

示例7: GetOrderBookSummary

// GetOrderBookSummary loads a summary of an order book identified by a
// selling/buying pair. It is designed to drive an order book summary client
// interface (bid/ask spread, prices and volume, etc).
func (q *Q) GetOrderBookSummary(dest interface{}, selling xdr.Asset, buying xdr.Asset) error {
	var sql bytes.Buffer
	var oq orderbookQueryBuilder
	err := selling.Extract(&oq.SellingType, &oq.SellingCode, &oq.SellingIssuer)
	if err != nil {
		return err
	}
	err = buying.Extract(&oq.BuyingType, &oq.BuyingCode, &oq.BuyingIssuer)
	if err != nil {
		return err
	}

	oq.pushArg(20)

	err = orderbookQueryTemplate.Execute(&sql, &oq)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	err = q.SelectRaw(dest, sql.String(), oq.args...)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	return nil
}
开发者ID:irisli,项目名称:horizon,代码行数:29,代码来源:order_book_summary.go

示例8: WalkAny

// WalkAny tries to retrieve container information on containerPath. It supports:
// the empty container (/dev/null), local directories, zip archives
func WalkAny(containerPath string, filter FilterFunc) (*Container, error) {
	// empty container case
	if containerPath == NullPath {
		return &Container{}, nil
	}

	file, err := eos.Open(containerPath)
	if err != nil {
		return nil, errors.Wrap(err, 1)
	}

	defer file.Close()

	stat, err := file.Stat()
	if err != nil {
		return nil, errors.Wrap(err, 1)
	}

	if stat.IsDir() {
		if err != nil {
			return nil, errors.Wrap(err, 1)
		}

		// local directory case
		return WalkDir(containerPath, filter)
	}

	// zip archive case
	zr, err := zip.NewReader(file, stat.Size())
	if err != nil {
		return nil, errors.Wrap(err, 1)
	}

	return WalkZip(zr, filter)
}
开发者ID:itchio,项目名称:wharf,代码行数:37,代码来源:walk.go

示例9: Fetch

// Fetch reads a block from disk
func (ds *DiskSource) Fetch(loc BlockLocation, data []byte) (int, error) {
	addr := ds.BlockAddresses.Get(loc)
	if addr == "" {
		return 0, errors.Wrap(fmt.Errorf("no address for block %+v", loc), 1)
	}
	path := filepath.Join(ds.BasePath, addr)

	fr, err := os.Open(path)
	if err != nil {
		return 0, errors.Wrap(err, 1)
	}

	defer fr.Close()

	if ds.Decompressor == nil {
		bytesRead, err := io.ReadFull(fr, data)
		if err != nil {
			if err == io.ErrUnexpectedEOF {
				// all good
			} else {
				return 0, errors.Wrap(err, 1)
			}
		}

		return bytesRead, nil
	}

	return ds.Decompressor.Decompress(data, fr)
}
开发者ID:itchio,项目名称:wharf,代码行数:30,代码来源:disksource.go

示例10: Prepare

// Prepare creates all directories, files, and symlinks.
// It also applies the proper permissions if the files already exist
func (c *Container) Prepare(basePath string) error {
	err := os.MkdirAll(basePath, 0755)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	for _, dirEntry := range c.Dirs {
		err := c.prepareDir(basePath, dirEntry)
		if err != nil {
			return errors.Wrap(err, 1)
		}
	}

	for _, fileEntry := range c.Files {
		err := c.prepareFile(basePath, fileEntry)
		if err != nil {
			return errors.Wrap(err, 1)
		}
	}

	for _, link := range c.Symlinks {
		err := c.prepareSymlink(basePath, link)
		if err != nil {
			return errors.Wrap(err, 1)
		}
	}

	return nil
}
开发者ID:itchio,项目名称:wharf,代码行数:31,代码来源:prepare.go

示例11: GetReadSeeker

// GetReadSeeker is like GetReader but the returned object allows seeking
func (cfp *ZipPool) GetReadSeeker(fileIndex int64) (io.ReadSeeker, error) {
	if cfp.seekFileIndex != fileIndex {
		if cfp.readSeeker != nil {
			err := cfp.readSeeker.Close()
			if err != nil {
				return nil, errors.Wrap(err, 1)
			}
			cfp.readSeeker = nil
			cfp.seekFileIndex = -1
		}

		key := cfp.GetRelativePath(fileIndex)
		f := cfp.fmap[key]
		if f == nil {
			return nil, errors.Wrap(os.ErrNotExist, 1)
		}

		reader, err := f.Open()
		if err != nil {
			return nil, errors.Wrap(err, 1)
		}
		defer reader.Close()

		buf, err := ioutil.ReadAll(reader)
		if err != nil {
			return nil, errors.Wrap(err, 1)
		}

		cfp.readSeeker = &closableBuf{bytes.NewReader(buf)}
		cfp.seekFileIndex = fileIndex
	}

	return cfp.readSeeker, nil
}
开发者ID:itchio,项目名称:wharf,代码行数:35,代码来源:zippool.go

示例12: Write

func (dw *Writer) Write(data []byte) (int, error) {
	dataOffset := 0
	totalBytes := len(data)

	for dataOffset < totalBytes {
		writtenBytes := totalBytes - dataOffset
		if writtenBytes > len(dw.Buffer)-dw.offset {
			writtenBytes = len(dw.Buffer) - dw.offset
		}

		copy(dw.Buffer[dw.offset:], data[dataOffset:dataOffset+writtenBytes])
		dataOffset += writtenBytes
		dw.offset += writtenBytes

		if dw.offset == len(dw.Buffer) {
			buf := dw.Buffer

			if dw.Validate != nil {
				err := dw.Validate(buf)
				if err != nil {
					return 0, errors.Wrap(err, 1)
				}
			}

			_, err := dw.Writer.Write(buf)
			if err != nil {
				return 0, errors.Wrap(err, 1)
			}
			dw.offset = 0
		}
	}

	return totalBytes, nil
}
开发者ID:itchio,项目名称:wharf,代码行数:34,代码来源:dripwriter.go

示例13: New

func New(c *tlc.Container, basePath string) (wsync.Pool, error) {
	if basePath == "/dev/null" {
		return fspool.New(c, basePath), nil
	}

	fr, err := eos.Open(basePath)
	if err != nil {
		return nil, errors.Wrap(err, 1)
	}

	targetInfo, err := fr.Stat()
	if err != nil {
		return nil, errors.Wrap(err, 1)
	}

	if targetInfo.IsDir() {
		err := fr.Close()
		if err != nil {
			return nil, err
		}

		return fspool.New(c, basePath), nil
	} else {
		zr, err := zip.NewReader(fr, targetInfo.Size())
		if err != nil {
			return nil, errors.Wrap(err, 1)
		}

		return zippool.New(c, zr), nil
	}
}
开发者ID:itchio,项目名称:wharf,代码行数:31,代码来源:pools.go

示例14: move

func (actx *ApplyContext) move(oldAbsolutePath string, newAbsolutePath string) error {
	err := os.Remove(newAbsolutePath)
	if err != nil {
		if !os.IsNotExist(err) {
			return errors.Wrap(err, 1)
		}
	}

	err = os.MkdirAll(filepath.Dir(newAbsolutePath), os.FileMode(0755))
	if err != nil {
		return errors.Wrap(err, 1)
	}

	if actx.debugBrokenRename {
		err = &os.PathError{}
	} else {
		err = os.Rename(oldAbsolutePath, newAbsolutePath)
	}
	if err != nil {
		cErr := actx.copy(oldAbsolutePath, newAbsolutePath, mkdirBehaviorNever)
		if cErr != nil {
			return cErr
		}

		cErr = os.Remove(oldAbsolutePath)
		if cErr != nil {
			return cErr
		}
	}

	return nil
}
开发者ID:itchio,项目名称:butler,代码行数:32,代码来源:apply.go

示例15: WriteMessage

// WriteMessage serializes a protobuf message and writes it to the stream
func (w *WriteContext) WriteMessage(msg proto.Message) error {
	if DebugWire {
		fmt.Printf("<< %s %+v\n", reflect.TypeOf(msg).Elem().Name(), msg)
	}

	buf, err := proto.Marshal(msg)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	vibuflen := binary.PutUvarint(w.varintBuffer, uint64(len(buf)))
	if err != nil {
		return errors.Wrap(err, 1)
	}
	_, err = w.writer.Write(w.varintBuffer[:vibuflen])
	if err != nil {
		return errors.Wrap(err, 1)
	}

	_, err = w.writer.Write(buf)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	return nil
}
开发者ID:itchio,项目名称:wharf,代码行数:27,代码来源:write.go


注:本文中的github.com/go-errors/errors.Wrap函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。