ServeHTTP renders the non-frontend handled errors.
(w http.ResponseWriter, r *http.Request)
| 56 | |
| 57 | // ServeHTTP renders the non-frontend handled errors. |
| 58 | func (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 | } |
no test coverage detected