You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

panic.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2013 Martini Authors
  2. // Copyright 2014 The Macaron Authors
  3. // Copyright 2019 The Gitea Authors. All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  6. // not use this file except in compliance with the License. You may obtain
  7. // a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. // License for the specific language governing permissions and limitations
  15. // under the License.
  16. package context
  17. import (
  18. "fmt"
  19. "code.gitea.io/gitea/modules/log"
  20. "gitea.com/macaron/macaron"
  21. )
  22. // Recovery returns a middleware that recovers from any panics and writes a 500 and a log if so.
  23. // Although similar to macaron.Recovery() the main difference is that this error will be created
  24. // with the gitea 500 page.
  25. func Recovery() macaron.Handler {
  26. return func(ctx *Context) {
  27. defer func() {
  28. if err := recover(); err != nil {
  29. combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2))
  30. ctx.ServerError("PANIC:", combinedErr)
  31. }
  32. }()
  33. ctx.Next()
  34. }
  35. }