1
0

honeybadger.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package honeybadger
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. )
  7. // VERSION defines the version of the honeybadger package.
  8. const VERSION = "0.4.0"
  9. var (
  10. // client is a pre-defined "global" client.
  11. DefaultClient = New(Configuration{})
  12. // Config is a pointer to the global client's Config.
  13. Config = DefaultClient.Config
  14. // Notices is the feature for sending error reports.
  15. Notices = Feature{"notices"}
  16. )
  17. // Feature references a resource provided by the API service. Its Endpoint maps
  18. // to the collection endpoint of the /v1 API.
  19. type Feature struct {
  20. Endpoint string
  21. }
  22. // CGIData stores variables from the server/request environment indexed by key.
  23. // Header keys should be converted to upercase, all non-alphanumeric characters
  24. // replaced with underscores, and prefixed with HTTP_. For example, the header
  25. // "Content-Type" would become "HTTP_CONTENT_TYPE".
  26. type CGIData hash
  27. // Params stores the form or url values from an HTTP request.
  28. type Params url.Values
  29. // Tags represents tags of the error which is classified errors in Honeybadger.
  30. type Tags []string
  31. // hash is used internally to construct JSON payloads.
  32. type hash map[string]interface{}
  33. func (h *hash) toJSON() []byte {
  34. out, err := json.Marshal(h)
  35. if err == nil {
  36. return out
  37. }
  38. panic(err)
  39. }
  40. // Configure updates configuration of the global client.
  41. func Configure(c Configuration) {
  42. DefaultClient.Configure(c)
  43. }
  44. // SetContext merges c Context into the Context of the global client.
  45. func SetContext(c Context) {
  46. DefaultClient.SetContext(c)
  47. }
  48. // Notify reports the error err to the Honeybadger service.
  49. //
  50. // The first argument err may be an error, a string, or any other type in which
  51. // case its formatted value will be used.
  52. //
  53. // It returns a string UUID which can be used to reference the error from the
  54. // Honeybadger service, and an error as a second argument.
  55. func Notify(err interface{}, extra ...interface{}) (string, error) {
  56. return DefaultClient.Notify(newError(err, 2), extra...)
  57. }
  58. // Monitor is used to automatically notify Honeybadger service of panics which
  59. // happen inside the current function. In order to monitor for panics, defer a
  60. // call to Monitor. For example:
  61. // func main {
  62. // defer honeybadger.Monitor()
  63. // // Do risky stuff...
  64. // }
  65. // The Monitor function re-panics after the notification has been sent, so it's
  66. // still up to the user to recover from panics if desired.
  67. func Monitor() {
  68. if err := recover(); err != nil {
  69. DefaultClient.Notify(newError(err, 2))
  70. DefaultClient.Flush()
  71. panic(err)
  72. }
  73. }
  74. // Flush blocks until all data (normally sent in the background) has been sent
  75. // to the Honeybadger service.
  76. func Flush() {
  77. DefaultClient.Flush()
  78. }
  79. // Handler returns an http.Handler function which automatically reports panics
  80. // to Honeybadger and then re-panics.
  81. func Handler(h http.Handler) http.Handler {
  82. return DefaultClient.Handler(h)
  83. }
  84. // BeforeNotify adds a callback function which is run before a notice is
  85. // reported to Honeybadger. If any function returns an error the notification
  86. // will be skipped, otherwise it will be sent.
  87. func BeforeNotify(handler func(notice *Notice) error) {
  88. DefaultClient.BeforeNotify(handler)
  89. }