// Unwrap unwraps a wrapped error recursively, returning its inner error.
//
// If the supplied error is not nil, Unwrap will never return nil. If a
// wrapped error reports that its InnerError is nil, that error will be
// returned.
320393
func Unwrap(err error) error {
	for {
		wrap, ok := err.(Wrapped)
		if !ok {
			return err
		}

		inner := wrap.InnerError()
		if inner == nil {
			break
		}
		err = inner
	}
	return err
}
644587
func WrapS(e error, skip int) error {
	if e == nil {
		return nil
	}
	return errors.Wrap(e, skip+1)
}
1016884
func Wrap(err error, msg string) error {
	renamed := errors.New(msg + ": " + err.Error())
	return NewRenamedError(err, renamed)
}
44107
func WrapError(err error, m interface{}) Composer {
	return NewErrorWrappedComposer(err, ConvertToComposer(level.Priority(0), m))
}
70253
func (eu ServerErrorUnwrapper) UnwrapError(arg interface{}) (appError error, dispatchError error) {
	s, ok := arg.(*keybase1.Status)
	if !ok {
		return nil, errors.New("Error converting arg to keybase1.Status object in ServerErrorUnwrapper.UnwrapError")
	}

	if s == nil || s.Code == 0 {
		return nil, nil
	}

	switch s.Code {
	case StatusCodeServerError:
		appError = ServerError{Msg: s.Desc}
		break
	case StatusCodeServerErrorBadRequest:
		appError = ServerErrorBadRequest{Msg: s.Desc}
		break
	case StatusCodeServerErrorUnauthorized:
		appError = ServerErrorUnauthorized{Msg: s.Desc}
		break
	case StatusCodeServerErrorOverQuota:
		quotaErr := ServerErrorOverQuota{Msg: s.Desc}
		for _, f := range s.Fields {
			if f.Key == "QUOTA_USAGE" {
				quotaErr.Usage, _ = strconv.ParseInt(f.Value, 10, 64)
			} else if f.Key == "QUOTA_LIMIT" {
				quotaErr.Limit, _ = strconv.ParseInt(f.Value, 10, 64)
			} else if f.Key == "QUOTA_THROTTLE" {
				quotaErr.Throttled, _ = strconv.ParseBool(f.Value)
			}
		}
		appError = quotaErr
		break
	case StatusCodeServerErrorBlockNonExistent:
		appError = ServerErrorBlockNonExistent{Msg: s.Desc}
		break
	case StatusCodeServerErrorBlockArchived:
		appError = ServerErrorBlockArchived{Msg: s.Desc}
		break
	case StatusCodeServerErrorNoPermission:
		appError = ServerErrorNoPermission{Msg: s.Desc}
		break
	case StatusCodeServerErrorThrottle:
		appError = ServerErrorThrottle{Msg: s.Desc}
		break
	case StatusCodeServerErrorBlockDeleted:
		appError = ServerErrorBlockDeleted{Msg: s.Desc}
		break
	case StatusCodeServerErrorNonceNonExistent:
		appError = ServerErrorNonceNonExistent{Msg: s.Desc}
		break
	case StatusCodeServerErrorMaxRefExceeded:
		appError = ServerErrorMaxRefExceeded{Msg: s.Desc}
		break
	default:
		ase := libkb.AppStatusError{
			Code:   s.Code,
			Name:   s.Name,
			Desc:   s.Desc,
			Fields: make(map[string]string),
		}
		for _, f := range s.Fields {
			ase.Fields[f.Key] = f.Value
		}
		appError = ase
	}

	return appError, nil
}
1264819
func unwrapOutput(i interface{}) (interface{}, error) {
	var (
		ch <-chan interface{}
		ok bool
	)

	if ch, ok = i.(<-chan interface{}); !ok {
		return nil, e.TypeErr(ch, i)
	}

	return <-ch, nil
}
201175
func Wrap(task string, err error) Err {
	if err == nil {
		return nil
	}

	return NewError(task, err)
}
1268103
func Wrap(w DoerAt, p []byte, off int64, wrapAt int64) (n int, err error) {
	var m, fails int

	off %= wrapAt

	for len(p) > 0 {

		if off+int64(len(p)) < wrapAt {
			m, err = w.DoAt(p, off)
		} else {
			space := wrapAt - off
			m, err = w.DoAt(p[:space], off)
		}

		if err != nil && err != io.EOF {
			return n + m, err
		}

		switch m {
		case 0:
			fails++
		default:
			fails = 0
		}

		if fails > maxConsecutiveEmptyActions {
			return n + m, io.ErrNoProgress
		}

		n += m
		p = p[m:]
		off += int64(m)
		off %= wrapAt
	}

	return n, err
}
612488
func ErrorWrap(cause interface{}, format string, args ...interface{}) Error {
	if causeCmnError, ok := cause.(*cmnError); ok {
		msg := fmt.Sprintf(format, args...)
		return causeCmnError.Stacktrace().Trace(1, msg)
	} else if cause == nil {
		return newCmnError(FmtError{format, args}).Stacktrace()
	} else {
		// NOTE: causeCmnError is a typed nil here.
		msg := fmt.Sprintf(format, args...)
		return newCmnError(cause).Stacktrace().Trace(1, msg)
	}
}
645907
func (retry *RetryRequest) Wrap(innerconnection plugin.Connection) plugin.Connection {
	retry.connection = innerconnection
	return retry
}
189852
func Wrap(m *lib.Message) lib.Dnstap {
	t := lib.Dnstap_MESSAGE
	return lib.Dnstap{
		Type:    &t,
		Message: m,
	}
}
832326
func (c *Ctx) UnwrapKey(sh SessionHandle, m []*Mechanism, unwrappingkey ObjectHandle, wrappedkey []byte, a []*Attribute) (ObjectHandle, error) {
	var key C.CK_OBJECT_HANDLE
	attrarena, ac, aclen := cAttributeList(a)
	defer attrarena.Free()
	mecharena, mech := cMechanism(m)
	defer mecharena.Free()
	e := C.UnwrapKey(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(unwrappingkey), C.CK_BYTE_PTR(unsafe.Pointer(&wrappedkey[0])), C.CK_ULONG(len(wrappedkey)), ac, aclen, &key)
	return ObjectHandle(key), toError(e)
}
496737
func unwrapDouble(w *wrappers.DoubleValue) *float64 {
	if w == nil {
		return nil
	}

	v := w.Value
	return &v
}
677965
func (e *Error) WrappedErrors() []error {
	if e == nil {
		return nil
	}

	result := make([]error, len(e.Errors))
	for i, e := range e.Errors {
		result[i] = errors.New(e)
	}

	return result
}
710122
func Wrap(head error, next error) error {
	if head == nil {
		// The state head==nil && next!=nil is very strange so I leave it as nil
		return nil
	}
	if next == nil {
		return head
	}
	tail := Tail(next)
	return &ErrorChain{
		head: head,
		next: next,
		tail: tail,
	}
}
817168
func Flatten(err error) error {
	// If it isn't an *Error, just return the error as-is
	if _, ok := err.(*Error); !ok {
		return err
	}

	// Otherwise, make the result and flatten away!
	flatErr := new(Error)
	flatten(err, flatErr)
	return flatErr
}
726147
func (ue UnpackingError) Error() string {
	var dv string
	var dt string
	k := ue.Destination.Kind()
	switch k {
	case reflect.Ptr:
		dt = fmt.Sprintf("%s", ue.Destination.Type().Elem())
		if !ue.Destination.IsNil() {
			dv = fmt.Sprintf("%v", ue.Destination.Elem().Interface())
		} else {
			dv = "nil"

		}
	case reflect.Invalid:
		dv = "invalid"
		dt = dv
	default:
		dv = fmt.Sprintf("%v", ue.Destination.Interface())
		dt = fmt.Sprintf("%s", ue.Destination.Type())
	}

	return fmt.Sprintf("Error unpacking %v(%T) into %s(%s):%v",
		ue.Source,
		ue.Source,
		dv,
		dt,
		ue.Err)
}