-
Notifications
You must be signed in to change notification settings - Fork 7
/
deprecated.go
39 lines (32 loc) · 1.31 KB
/
deprecated.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package jsendx
import (
"net/http"
"runtime/debug"
"github.com/Vonage/gosrvlib/pkg/httpserver"
"github.com/Vonage/gosrvlib/pkg/logging"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
)
// NewRouter is deprecated.
// Deprecated: Set instead the router error handlers with the following options:
//
// httpserver.WithNotFoundHandlerFunc(jsendx.DefaultNotFoundHandlerFunc(appInfo))
// httpserver.WithMethodNotAllowedHandlerFunc(jsendx.DefaultMethodNotAllowedHandlerFunc(appInfo))
// httpserver.WithPanicHandlerFunc(jsendx.DefaultPanicHandlerFunc(appInfo))
func NewRouter(info *AppInfo, instrumentHandler httpserver.InstrumentHandler) *httprouter.Router {
r := httprouter.New()
r.NotFound = instrumentHandler("404", func(w http.ResponseWriter, r *http.Request) {
Send(r.Context(), w, http.StatusNotFound, info, "invalid endpoint")
})
r.MethodNotAllowed = instrumentHandler("405", func(w http.ResponseWriter, r *http.Request) {
Send(r.Context(), w, http.StatusMethodNotAllowed, info, "the request cannot be routed")
})
r.PanicHandler = func(w http.ResponseWriter, r *http.Request, p any) {
logging.FromContext(r.Context()).Error("panic",
zap.Any("err", p),
zap.String("stacktrace", string(debug.Stack())),
)
Send(r.Context(), w, http.StatusInternalServerError, info, "internal error")
}
return r
}