caddy-keydb-codebreaker/errors.go

58 lines
1.4 KiB
Go
Raw Normal View History

2024-08-13 16:28:06 -07:00
package keydbextension
import (
"encoding/json"
"fmt"
"net/http"
"sync"
)
const (
ErrNoAuthToken = "ErrNoAuthToken"
ErrUnauthorized = "ErrUnauthorized"
ErrIncompleteReq = "ErrIncompleteReq"
ErrConnFailed = "ErrConnFailed"
ErrReqNotProcessed = "ErrReqNotProcessed"
ErrUnknown = "ErrUnknown"
)
var (
errorResponses map[string][]byte
errorStatusCodes map[string]int
errorResponsesOnce sync.Once
)
func initErrorResponses() {
errorResponses = make(map[string][]byte)
errorStatusCodes = make(map[string]int)
errors := []struct {
key string
code int
message string
}{
{ErrNoAuthToken, http.StatusUnauthorized, "No auth token provided"},
{ErrUnauthorized, http.StatusForbidden, "Unauthorized access"},
{ErrIncompleteReq, http.StatusBadRequest, "Request was incomplete"},
{ErrConnFailed, 523, "Connection to GuardianPT failed"},
{ErrReqNotProcessed, 522, "Request could not be processed"},
{ErrUnknown, 520, "An unknown error occurred"},
}
for _, err := range errors {
error_code := fmt.Sprintf("0x%08X", 0xC0043293+err.code)
response, _ := json.Marshal(map[string]interface{}{
"error": true,
"code": error_code,
"message": err.message,
})
errorResponses[err.key] = response
errorStatusCodes[err.key] = err.code
}
}
func getHTTPStatusCode(key string) int {
errorResponsesOnce.Do(initErrorResponses)
return errorStatusCodes[key]
}