-
Notifications
You must be signed in to change notification settings - Fork 41
/
error_handler.go
35 lines (28 loc) · 1.07 KB
/
error_handler.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
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func (p *Plugin) handleError(w http.ResponseWriter, internalErr error) {
p.handleErrorWithCode(w, http.StatusInternalServerError, "An internal error has occurred. Check app server logs for details.", internalErr)
}
// handleErrorWithCode logs the internal error and sends the public facing error
// message as JSON in a response with the provided code.
func (p *Plugin) handleErrorWithCode(w http.ResponseWriter, code int, publicErrorMsg string, internalErr error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
details := ""
if internalErr != nil {
details = internalErr.Error()
}
p.LogError(fmt.Sprintf("public error message: %v; internal details: %v", publicErrorMsg, details))
responseMsg, _ := json.Marshal(struct {
Error string `json:"error"` // A public facing message providing details about the error.
}{
Error: publicErrorMsg,
})
_, _ = w.Write(responseMsg)
}