本文整理匯總了Golang中github.com/dropbox/godropbox/errors.Wrap函數的典型用法代碼示例。如果您正苦於以下問題:Golang Wrap函數的具體用法?Golang Wrap怎麽用?Golang Wrap使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Wrap函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: send
func (s *fileSender) send(entry *logrus.Entry) (err error) {
msg, err := entry.String()
if err != nil {
return
}
file, err := os.OpenFile(utils.GetLogPath(),
os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
err = &WriteError{
errors.Wrap(err, "logger: Failed to open log file"),
}
return
}
defer file.Close()
_, err = file.WriteString(msg)
if err != nil {
err = &WriteError{
errors.Wrap(err, "logger: Failed to write to log file"),
}
return
}
return
}
示例2: CreateImage
// Writes an image to a file. The extension of the file is used to determine the
// encoding method.
func CreateImage(i image.Image, file string) error {
ext := filepath.Ext(file)
m := mime.TypeByExtension(ext)
switch m {
case "image/jpeg", "image/png", "image/tiff":
default:
return errors.Newf("unsupported extension/mime type: %s %s", ext, m)
}
f, err := os.Create(file)
if err != nil {
return errors.Wrap(err, "could not create file")
}
defer f.Close()
switch m {
case "image/jpeg":
err = jpeg.Encode(f, i, &jpeg.Options{Quality: 98})
case "image/png":
err = png.Encode(f, i)
case "image/tiff":
err = tiff.Encode(f, i, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
default:
panic("unreachable")
}
if err != nil {
return errors.Wrap(err, "could not encode image")
}
return nil
}
示例3: Authorize
func (s *Saml) Authorize(state, respEncoded string) (
data *UserData, err error) {
resp, err := saml.ParseEncodedResponse(respEncoded)
if err != nil {
err = &SamlError{
errors.Wrap(err, "saml: Failed to parse response"),
}
return
}
err = resp.Validate(&s.provider)
if err != nil {
err = &SamlError{
errors.Wrap(err, "saml: Failed to validate response"),
}
return
}
data = &UserData{
Username: resp.GetAttribute("username"),
Email: resp.GetAttribute("email"),
Org: resp.GetAttribute("org"),
Secondary: resp.GetAttribute("secondary"),
}
if data.Username == "" {
data.Username = resp.Assertion.Subject.NameID.Value
}
return
}
示例4: GetJson
func (c *Oauth2Client) GetJson(url string, resp interface{}) (err error) {
httpResp, err := c.client.Get(url)
if err != nil {
err = &errortypes.UnknownError{
errors.Wrap(err, "oauth.oauth2: Unknown api error"),
}
return
}
defer httpResp.Body.Close()
body, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
err = &errortypes.UnknownError{
errors.Wrap(err, "oauth.oauth2: Unknown parse error"),
}
return
}
err = json.Unmarshal(body, resp)
if err != nil {
err = &errortypes.UnknownError{
errors.Wrap(err, "oauth.oauth2: Unknown parse error"),
}
return
}
return
}
示例5: GetLocalIPs
// This returns the list of local ip addresses which other hosts can connect
// to (NOTE: Loopback ip is ignored).
// Also resolves Hostname to an address and adds it to the list too, so
// IPs from /etc/hosts can work too.
func GetLocalIPs() ([]*net.IP, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, errors.Wrap(err, "Failed to lookup hostname")
}
// Resolves IP Address from Hostname, this way overrides in /etc/hosts
// can work too for IP resolution.
ipInfo, err := net.ResolveIPAddr("ip4", hostname)
if err != nil {
return nil, errors.Wrap(err, "Failed to resolve ip")
}
ips := []*net.IP{&ipInfo.IP}
// TODO(zviad): Is rest of the code really necessary?
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, errors.Wrap(err, "Failed to get interface addresses.")
}
for _, addr := range addrs {
ipnet, ok := addr.(*net.IPNet)
if !ok {
continue
}
if ipnet.IP.IsLoopback() {
continue
}
ips = append(ips, &ipnet.IP)
}
return ips, nil
}
示例6: Page
func Page(q PageQuery) (*PageResponse, error) {
if q.ID == 0 {
return nil, errors.New("a page id is required")
}
resp, err := http.Get(q.urlString())
if err != nil {
return nil, errors.Wrap(err, "could not get http response")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.Wrapf(errors.New(resp.Status), "StatusCode: %d; URL: %s", resp.StatusCode, q.urlString())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read http response body")
}
var response PageResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, errors.Wrap(err, "could not unmarshal http response")
}
return &response, nil
}
示例7: Do
// Performs the HTTP request using our HTTP client
func (pool *SimplePool) Do(req *http.Request) (resp *http.Response, err error) {
conn, err := pool.Get()
if err != nil {
return nil, errors.Wrap(err, err.Error())
}
if pool.params.UseSSL {
req.URL.Scheme = "https"
} else {
req.URL.Scheme = "http"
}
if pool.params.HostHeader != nil {
req.URL.Host = *pool.params.HostHeader
} else {
req.URL.Host = pool.addr
}
resp, err = conn.Do(req)
if err != nil {
if urlErr, ok := err.(*url.Error); ok &&
strings.HasPrefix(urlErr.Err.Error(), dialErrorMsgPrefix) {
err = DialError{errors.Wrap(err, "SimplePool: Dial Error")}
} else {
err = errors.Wrap(err, err.Error())
}
}
return
}
示例8: clean
func clean() (err error) {
paths := []string{
filepath.Join(pathSep, "usr", "local", "bin", "pritunl-openvpn"),
filepath.Join(pathSep, "private", "var", "db", "receipts",
"com.pritunl.pkg.Pritunl.bom"),
filepath.Join(pathSep, "private", "var", "db", "receipts",
"com.pritunl.pkg.Pritunl.plist"),
filepath.Join(pathSep, "private", "tmp", "pritunl"),
filepath.Join(pathSep, "Applications", "Pritunl.app"),
filepath.Join(pathSep, "Library", "LaunchAgents",
"com.pritunl.client.plist"),
}
homesPath := filepath.Join(pathSep, "Users")
homes, err := ioutil.ReadDir(homesPath)
if err != nil {
err = &ParseError{
errors.Wrap(err, "autoclean: Failed to read home directories"),
}
return
}
for _, home := range homes {
if !home.IsDir() {
continue
}
paths = append(paths, filepath.Join(homesPath, home.Name(),
"Library", "Application Support", "pritunl"))
paths = append(paths, filepath.Join(homesPath, home.Name(),
"Library", "Caches", "pritunl"))
paths = append(paths, filepath.Join(homesPath, home.Name(),
"Library", "Preferences", "com.electron.pritunl.plist"))
}
paths = append(paths, filepath.Join(pathSep, "usr", "local",
"bin", "pritunl-service"))
paths = append(paths, filepath.Join(pathSep, "Library", "LaunchDaemons",
"com.pritunl.service.plist"))
for _, path := range paths {
if len(path) < 20 {
panic("autoclean: Bad path " + path)
}
err = os.RemoveAll(path)
if err != nil {
err = &RemoveError{
errors.Wrap(err, "autoclean: Failed to remove file"),
}
}
}
return
}
示例9: Get
// Checks out an HTTP connection from an instance pool, favoring less loaded instances.
func (pool *LoadBalancedPool) Get() (*http.Client, error) {
_, instance, _, err := pool.getInstance()
if err != nil {
return nil, errors.Wrap(err, "can't get HTTP connection")
}
conn, err := instance.Get()
if err != nil {
return nil, errors.Wrap(err, "couldn't Get from LoadBalancedPool")
}
return conn, err
}
示例10: fetchAndWriteStationData
func (q *StationQuery) fetchAndWriteStationData(offset int) (stationCount int, err error) {
u := fmt.Sprintf("%s/cdo-web/api/v2/stations?limit=%d&datasetid=GHCND&offset=%d", BaseURL, StationPageLimit, offset)
body, err := request(u, q.APITokens)
if err != nil {
return 0, err
}
db, err := bolt.Open(q.CachePath, 0600, nil)
if err != nil {
return 0, errors.Wrap(err, "could not open station cache")
}
defer db.Close()
// if !NorthAmerica.Contains(geo.NewPoint(r.Latitude, r.Longitude)) {
// fmt.Println("IS NOT CONTAINED IN NORTH AMERICA")
// continue
// }
var response stationAPI
if err := json.Unmarshal(body, &response); err != nil {
return 0, errors.Wrap(err, "could not unmarshal response")
}
if err := db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte("stations"))
if err != nil {
return err
}
for _, r := range response.Results {
value, err := json.Marshal(r)
if err != nil {
return errors.Wrap(err, "could not marshal station value")
}
if err := bucket.Put([]byte(r.ID), value); err != nil {
return err
}
}
return nil
}); err != nil {
return 0, errors.Wrap(err, "could not update station collection")
}
return response.Meta.ResultSet.Count, nil
}
示例11: OpenImage
// Opens and decodes (see image.Decode) an image file.
func OpenImage(file string) (image.Image, error) {
f, err := os.Open(file)
if err != nil {
return nil, errors.Wrap(err, "could not open file")
}
defer f.Close()
i, _, err := image.Decode(f)
if err != nil {
return nil, errors.Wrap(err, "could not decode image")
}
return i, nil
}
示例12: request
func (s *SearchQuery) request(page int) error {
select {
case <-s.tmb.Dying():
return nil
default:
}
resp, err := http.Get(s.url(page))
if err != nil {
return errors.Wrap(err, "could not get http response")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errors.Wrapf(errors.New(resp.Status), "StatusCode: %d; URL: %s", resp.StatusCode, s.url(page))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "could not read http response body")
}
var response results
if err := json.Unmarshal(body, &response); err != nil {
return errors.Wrap(err, "could not unmarshal http response")
}
for i := range response.Results {
s.ch <- response.Results[i]
}
if page > 1 {
return nil
}
// If this the first page, schedule the remaining requests
totalRequests := math.Ceil(response.TotalResults / response.ItemsPerPage)
var wg sync.WaitGroup
for i := 2; i <= int(totalRequests); i++ {
func(count int) {
wg.Add(1)
go s.tmb.Go(func() error {
defer wg.Done()
return s.request(count)
})
}(i)
}
wg.Wait()
return nil
}
示例13: PublicKeyFile
func PublicKeyFile(file string) (auth ssh.AuthMethod, err error) {
buffer, err := ioutil.ReadFile(file)
if err != nil {
err = errors.Wrap(err, "")
return
}
key, err := ssh.ParsePrivateKey(buffer)
if err != nil {
err = errors.Wrap(err, "")
return
}
return ssh.PublicKeys(key), nil
}
示例14: Stat
// See Client interface for documentation.
func (c *ShardedClient) Stat(statsKey string) StatResponse {
statEntries := make(map[int](map[string]string))
var err error
for shard, conn := range c.manager.GetAllShards() {
response := c.statHelper(shard, conn, statsKey)
if response.Error() != nil {
if err == nil {
err = response.Error()
} else {
err = errors.Wrap(response.Error(), err.Error())
}
}
for shardId, entries := range response.Entries() {
statEntries[shardId] = entries
}
}
if err != nil {
return NewStatErrorResponse(err, statEntries)
}
return NewStatResponse(StatusNoError, statEntries)
}
示例15: countCache
func (q *StationQuery) countCache() (count int, err error) {
db, err := bolt.Open(q.CachePath, 0600, nil)
if err != nil {
return 0, errors.Wrap(err, "could not open station cache")
}
defer db.Close()
if err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("stations"))
if b == nil {
return ErrStationCacheEmpty
}
c := b.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
count++
}
return nil
}); err != nil {
return 0, err
}
return count, nil
}