// UnwrapErrorWithFileContext tries to unwrap an ErrorWithFileContext from err.
// It returns nil if this is not possible.
331562
func UnwrapErrorWithFileContext(err error) *ErrorWithFileContext {
	for err != nil {
		switch v := err.(type) {
		case *ErrorWithFileContext:
			return v
		case causer:
			err = v.Cause()
		default:
			return nil
		}
	}
	return nil
}
625230
func unwrapError(ierr error) (nerr error) {
	// Internal errors have a well defined bit of context.
	if dbxErr, ok := ierr.(DropboxError); ok {
		return dbxErr.GetInner()
	}

	// At this point, if anything goes wrong, just return nil.
	defer func() {
		if x := recover(); x != nil {
			nerr = nil
		}
	}()

	// Go system errors have a convention but paradoxically no
	// interface.  All of these panic on error.
	errV := reflect.ValueOf(ierr).Elem()
	errV = errV.FieldByName("Err")
	return errV.Interface().(error)
}
419539
func FileDescriptorFromContext(ctx context.Context) (*FileDescriptor, bool) {
	val, ok := ctx.Value(fileContextKey).(*FileDescriptor)
	return val, ok
}
8481
func (o *DestroyOneParams) WithContext(ctx context.Context) *DestroyOneParams {
	o.SetContext(ctx)
	return o
}
648747
func (o *GetPrefilterParams) WithContext(ctx context.Context) *GetPrefilterParams {
	o.SetContext(ctx)
	return o
}
974815
func IsContextNotFound(err error) bool {
	if err == nil {
		return false
	}
	if _, ok := err.(*errContextNotFound); ok || err == ErrNoContext {
		return true
	}
	return strings.Contains(err.Error(), "context was not found for specified context")
}
845081
func (eu KBFSErrorUnwrapper) 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 DiskCacheErrorUnwrapper.UnwrapError")
	}

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

	switch s.Code {
	case StatusCodeDiskBlockCacheError:
		appError = DiskBlockCacheError{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
}
150699
func (api *Client) UploadFileContext(ctx context.Context, params FileUploadParameters) (file *File, err error) {
	// Test if user token is valid. This helps because client.Do doesn't like this for some reason. XXX: More
	// investigation needed, but for now this will do.
	_, err = api.AuthTest()
	if err != nil {
		return nil, err
	}
	if params.Filename == "" {
		return nil, fmt.Errorf("files.upload: FileUploadParameters.Filename is mandatory")
	}
	response := &fileResponseFull{}
	values := url.Values{
		"token": {api.token},
	}
	if params.Filetype != "" {
		values.Add("filetype", params.Filetype)
	}
	if params.Filename != "" {
		values.Add("filename", params.Filename)
	}
	if params.Title != "" {
		values.Add("title", params.Title)
	}
	if params.InitialComment != "" {
		values.Add("initial_comment", params.InitialComment)
	}
	if params.ThreadTimestamp != "" {
		values.Add("thread_ts", params.ThreadTimestamp)
	}
	if len(params.Channels) != 0 {
		values.Add("channels", strings.Join(params.Channels, ","))
	}
	if params.Content != "" {
		values.Add("content", params.Content)
		err = api.postMethod(ctx, "files.upload", values, response)
	} else if params.File != "" {
		err = postLocalWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.upload", params.File, "file", values, response, api)
	} else if params.Reader != nil {
		err = postWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.upload", params.Filename, "file", values, params.Reader, response, api)
	}
	if err != nil {
		return nil, err
	}

	return &response.File, response.Err()
}
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
}
406852
func handlerWithContext(ctx context.Context, handler func(context.Context, http.ResponseWriter, *http.Request)) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := dcontext.WithRequest(ctx, r)
		logger := dcontext.GetRequestLogger(ctx)
		ctx = dcontext.WithLogger(ctx, logger)

		handler(ctx, w, r)
	})
}
1100647
func (eu ErrorUnwrapper) UnwrapError(arg interface{}) (appError, dispatchError error) {
	targ, ok := arg.(*Status)
	if !ok {
		dispatchError = errors.New("Error converting status to keybase1.Status object")
		return nil, dispatchError
	}
	if targ == nil {
		return nil, nil
	}
	if targ.Code == int(StatusCode_SCOk) {
		return nil, nil
	}

	if eu.Upcaster != nil {
		appError = eu.Upcaster(*targ)
	} else {
		appError = *targ
	}
	return appError, nil
}
58976
func UnwrapError(err error) (b2Err *Error, ok bool) {
	if e, ok := err.(*url.Error); ok {
		err = e.Err
	}
	if e, ok := err.(*Error); ok {
		return e, true
	}
	return nil, false
}
800396
func (h *BasicRecovery) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) (err error) {
	defer func() {
		if e := errors.Recover(ctx, recover()); e != nil {
			err = e
		}
	}()

	err = h.handler.ServeHTTPContext(ctx, w, r)

	return
}
28375
func AddContext(err error, context string) error {
	if err == nil {
		return nil
	}
	return Extend(err, New(context))
}
844075
func (p11w *Pkcs11Wrapper) InitContext() (err error) {

	// check if lib file exists
	if _, err = os.Stat(p11w.Library.Path); os.IsNotExist(err) {
		return
	}

	// try to initialize
	p11w.Context = pkcs11.New(p11w.Library.Path)
	err = p11w.Context.Initialize()
	if err != nil {
		return
	}

	// get library info
	p11w.Library.Info, err = p11w.Context.GetInfo()

	return

}
125176
func (o *DeleteFqdnCacheParams) WithContext(ctx context.Context) *DeleteFqdnCacheParams {
	o.SetContext(ctx)
	return o
}
649962
func ContextError(ctx context.Context) error {
	if err := ctx.Value(errKey); err != nil {
		return err.(error)
	}
	return nil
}