MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / ServeHTTP

Method ServeHTTP

pkg/webhandlers/error_template.go:58–96  ·  view source on GitHub ↗

ServeHTTP renders the non-frontend handled errors.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

56
57// ServeHTTP renders the non-frontend handled errors.
58func (t *ErrorTemplate) ServeHTTP(w http.ResponseWriter, r *http.Request) {
59 err := RetrieveError(r)
60 if err == nil {
61 return
62 }
63 errMsg, _ := json.MarshalIndent(err, "", " ")
64 errorID := "n/a"
65 errorCorrelationID := "n/a"
66 var errorTitle string
67 if ttnErr, ok := errors.From(err); ok {
68 errorID = ttnErr.FullName()
69 errorCorrelationID = ttnErr.CorrelationID()
70 errorTitle = ttnErr.FormatMessage(ttnErr.Attributes())
71 }
72 var errorMessage string
73 isGenericNotFound := errors.Resemble(err, errRouteNotFound)
74 switch errors.Code(err) {
75 case http.StatusNotFound:
76 if isGenericNotFound {
77 errorTitle = "Page not found"
78 }
79 errorMessage = "The resource you requested cannot be found."
80 case http.StatusUnauthorized:
81 errorMessage = "You are not allowed to perform this action."
82 default:
83 errorMessage = "An unknown error occurred."
84 }
85 if err := t.template.Execute(w, Data{
86 ErrorTitle: errorTitle,
87 ErrorMessage: errorMessage,
88 ErrorID: errorID,
89 CorrelationID: errorCorrelationID,
90 BackendErrorDetails: string(errMsg),
91 Year: time.Now().Year(),
92 IsGenericNotFound: isGenericNotFound,
93 }); err != nil {
94 log.FromContext(r.Context()).WithError(err).Warn("Failed to execute template")
95 }
96}

Callers 2

WithErrorHandlersFunction · 0.45
ErrorFunction · 0.45

Calls 14

FromFunction · 0.92
ResembleFunction · 0.92
CodeFunction · 0.92
FromContextFunction · 0.92
RetrieveErrorFunction · 0.85
FormatMessageMethod · 0.80
FullNameMethod · 0.65
CorrelationIDMethod · 0.65
AttributesMethod · 0.65
ExecuteMethod · 0.65
NowMethod · 0.65
WarnMethod · 0.65

Tested by

no test coverage detected