// panicRecover catches any panics from the resolvers and sets an error
// code of server failure
540748
func panicRecover(f func(w dns.ResponseWriter, r *dns.Msg)) func(w dns.ResponseWriter, r *dns.Msg) {
	return func(w dns.ResponseWriter, r *dns.Msg) {
		defer func() {
			if rec := recover(); rec != nil {
				m := new(dns.Msg)
				m.SetRcode(r, 2)
				if err := w.WriteMsg(m); err != nil {
					logging.Error.Println(err)
				}
				logging.Error.Println(rec)
			}
		}()
		f(w, r)
	}
}
1083764
func (s *Service) SmsNumberRecover(ctx context.Context, accountIdentity string) (*SmsNumber, error) {
	var smsNumber SmsNumber
	return &smsNumber, s.Post(ctx, &smsNumber, fmt.Sprintf("/users/%v/sms-number/actions/recover", accountIdentity), nil)
}
622695
func Panicf(format string, args ...interface{}) {
	logrus.Warnf(format, args...)
	err := DumpGoProfile()
	if err != nil {
		logrus.Fatal(err)
	}
	DumpHeap()
	os.Exit(6)
}
440775
func (m Middleware) Recover(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if err := recover(); err != nil {
				var protocol string
				if r.TLS == nil {
					protocol = "http"
				} else {
					protocol = "https"
				}

				var log *zap.Logger
				v := r.Context().Value(CtxLoggerKey)
				if v != nil {
					var ok bool
					log, ok = v.(*zap.Logger)
					if !ok {
						panic("cannot get derived request id logger from context object")
					}
					// log with the request_id scoped logger
					log.Error(fmt.Sprintf("%s request error", protocol),
						zap.String("method", r.Method),
						zap.String("uri", r.RequestURI),
						zap.Bool("tls", r.TLS != nil),
						zap.String("protocol", r.Proto),
						zap.String("host", r.Host),
						zap.String("remote_addr", r.RemoteAddr),
						zap.String("error", fmt.Sprintf("%+v", err)),
					)
				} else {
					// log with the logger attached to middleware struct if
					// cannot find request_id scoped logger
					m.Log.Error(fmt.Sprintf("%s request error", protocol),
						zap.String("method", r.Method),
						zap.String("uri", r.RequestURI),
						zap.Bool("tls", r.TLS != nil),
						zap.String("protocol", r.Proto),
						zap.String("host", r.Host),
						zap.String("remote_addr", r.RemoteAddr),
						zap.String("error", fmt.Sprintf("%+v", err)),
					)
				}

				// Return a http 500 with the HTTP body of "Internal Server Error"
				http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
			}
		}()

		next.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}
580035
func (i *i) Panic(fn func()) {
	var r interface{}
	func() {
		defer func() {
			r = recover()
		}()
		fn()
	}()
	if r == nil {
		i.Log("expected panic")
	}
}
176254
func (l *Logger) Panic(format string, v ...interface{}) {
	s := fmt.Sprintf(format, v...)
	l.base.SetPrefix(l2s[LFatal])
	l.base.Output(2, s)
	panic(s)
}
87016
func (s *state) errRecover(errp *error) {
	if e := recover(); e != nil {
		switch e := e.(type) {
		case runtime.Error:
			*errp = s.errFromNode("%s: %v\n%v", s.callAnnotation(), e, string(debug.Stack()))
		default:
			*errp = s.errFromNode("%s: %v", s.callAnnotation(), e)
		}
	}
}
866257
func RecoverFromPanic(err *error) {
	if r := recover(); r != nil {
		// Same as stdlib http server code. Manually allocate stack trace buffer size
		// to prevent excessively large logs
		const size = 64 << 10
		stacktrace := make([]byte, size)
		stacktrace = stacktrace[:runtime.Stack(stacktrace, false)]

		*err = fmt.Errorf(
			"recovered from panic %q. (err=%v) Call stack:\n%s",
			r,
			*err,
			stacktrace)
	}
}
47969
func doRecover(errp *error) {
	e := recover()
	if e != nil {
		if _, ok := e.(runtime.Error); ok {
			panic(e)
		}
		if err, ok := e.(error); ok {
			*errp = err
		} else if errStr, ok := e.(string); ok {
			*errp = errors.New(errStr)
		} else {
			*errp = fmt.Errorf("%v", e)
		}
	}
	return
}
535834
func Recoverer(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		reqID := GetReqID(*c)

		defer func() {
			if err := recover(); err != nil {
				printPanic(reqID, err)
				debug.PrintStack()
				http.Error(w, http.StatusText(500), 500)
			}
		}()

		h.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}
734824
func (l *Logger) Panicf(format string, args ...interface{}) {
	l.logger().Panicf(format, args...)
}
397767
func PanicHandler(h http.Handler, errH http.Handler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if err := recover(); err != nil {
				if errH != nil {
					ew := &errResponseWriter{w, err}
					errH.ServeHTTP(ew, r)
				} else {
					http.Error(w, fmt.Sprintf("%s", err), http.StatusInternalServerError)
				}
			}
		}()

		// Call the protected handler
		h.ServeHTTP(w, r)
	}
}
256177
func PanicOnError(err error, msg string) {
	if revErr, ok := err.(*Error); (ok && revErr != nil) || (!ok && err != nil) {
		Logger.Panicf("Abort: %s: %s %s", msg, revErr, err)
	}
}
416412
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer func() {
		// We absolutely need to be sure we stay alive up here,
		// even though, in theory, the errors middleware does this.
		if rec := recover(); rec != nil {
			log.Printf("[PANIC] %v", rec)
			DefaultErrorFunc(w, r, http.StatusInternalServerError)
		}
	}()

	w.Header().Set("Server", "Caddy")

	sanitizePath(r)

	status, _ := s.serveHTTP(w, r)

	// Fallback error response in case error handling wasn't chained in
	if status >= 400 {
		DefaultErrorFunc(w, r, status)
	}
}
139621
func Recoverer(c *web.C, h http.Handler) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		// Handle panics
		defer func() {
			if err := recover(); err != nil {
				// write stack backtrace into c.Env, max 64KB
				const size = 64 << 10 // 64KB
				buf := make([]byte, size)
				buf = buf[:runtime.Stack(buf, false)]
				lines := strings.Split(string(buf), "\n")
				//log15.Warn("Panic skipping", "l0", lines[0], "l1", lines[1],
				//	"l2", lines[2])
				c.Env["stack"] = lines[3:]
				Errorf(*c, rw, 500, "panic: %v", err)
			}
		}()
		h.ServeHTTP(rw, r)
	})
}
717322
func (c *Ctx) SignRecover(sh SessionHandle, data []byte) ([]byte, error) {
	var (
		sig    C.CK_BYTE_PTR
		siglen C.CK_ULONG
	)
	e := C.SignRecover(c.ctx, C.CK_SESSION_HANDLE(sh), cMessage(data), C.CK_ULONG(len(data)), &sig, &siglen)
	if toError(e) != nil {
		return nil, toError(e)
	}
	h := C.GoBytes(unsafe.Pointer(sig), C.int(siglen))
	C.free(unsafe.Pointer(sig))
	return h, nil
}
1046395
func (c *OperatorRekeyCommand) backupRetrieve(client *api.Client) int {
	// Handle the different API requests
	var fn func() (*api.RekeyRetrieveResponse, error)
	switch strings.ToLower(strings.TrimSpace(c.flagTarget)) {
	case "barrier":
		fn = client.Sys().RekeyRetrieveBackup
	case "recovery", "hsm":
		fn = client.Sys().RekeyRetrieveRecoveryBackup
	default:
		c.UI.Error(fmt.Sprintf("Unknown target: %s", c.flagTarget))
		return 1
	}

	// Make the request
	storedKeys, err := fn()
	if err != nil {
		c.UI.Error(fmt.Sprintf("Error retrieving rekey stored keys: %s", err))
		return 2
	}

	secret := &api.Secret{
		Data: structs.New(storedKeys).Map(),
	}

	return OutputSecret(c.UI, secret)
}