FILE / Unmei/Backend
routes/errors.go
Исходный файл и его история в репозитории.
108 lines
2.0 KiB
Go
108 lines
2.0 KiB
Go
package routes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
)
|
|
|
|
type ApiError struct {
|
|
Code uint8 `json:"code"`
|
|
Text string `json:"text"`
|
|
HTTPCode uint16 `json:"-"`
|
|
}
|
|
|
|
var (
|
|
WrongPassword = ApiError{
|
|
Code: 1,
|
|
Text: "Wrong login and/or password!",
|
|
HTTPCode: http.StatusBadRequest,
|
|
}
|
|
RecaptchaFailed = ApiError{
|
|
Code: 2,
|
|
Text: "Recaptcha check failed!",
|
|
HTTPCode: http.StatusBadRequest,
|
|
}
|
|
NoToken = ApiError{
|
|
Code: 3,
|
|
Text: "No authorization token passed!",
|
|
HTTPCode: http.StatusUnauthorized,
|
|
}
|
|
NoAccess = ApiError{
|
|
Code: 4,
|
|
Text: "No Access!",
|
|
HTTPCode: http.StatusForbidden,
|
|
}
|
|
AlreadyActivated = ApiError{
|
|
Code: 5,
|
|
Text: "Account already activated!",
|
|
HTTPCode: http.StatusBadRequest,
|
|
}
|
|
TokenExpired = ApiError{
|
|
Code: 6,
|
|
Text: "Activate token has expired",
|
|
HTTPCode: 400,
|
|
}
|
|
TooOftenGeneration = ApiError{
|
|
Code: 7,
|
|
Text: "Too often generating links",
|
|
HTTPCode: 400,
|
|
}
|
|
SamePassword = ApiError{
|
|
Code: 8,
|
|
Text: "Same password",
|
|
HTTPCode: 400,
|
|
}
|
|
RecaptchaNeeded = ApiError{
|
|
Code: 9,
|
|
Text: "Recaptcha needed",
|
|
HTTPCode: 0,
|
|
}
|
|
NotFound = ApiError{
|
|
Code: 100,
|
|
Text: "Not found!",
|
|
HTTPCode: http.StatusNotFound,
|
|
}
|
|
TooManyRequests = ApiError{
|
|
Code: 101,
|
|
Text: "Too many requests",
|
|
HTTPCode: 429,
|
|
}
|
|
Unauthorized = ApiError{
|
|
Code: 102,
|
|
Text: "Unauthorized",
|
|
HTTPCode: http.StatusUnauthorized,
|
|
}
|
|
)
|
|
|
|
func (e ApiError) Marshall() string {
|
|
data, err := json.Marshal(e)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
sentry.CaptureException(err)
|
|
return ""
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
func (e ApiError) Ferror(w http.ResponseWriter) {
|
|
const baseError = `{"error":true,"error_data":%v}`
|
|
if e.HTTPCode > 0 {
|
|
w.WriteHeader(int(e.HTTPCode))
|
|
}
|
|
|
|
data, err := json.Marshal(e)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
sentry.CaptureException(err)
|
|
}
|
|
|
|
_, err = fmt.Fprintf(w, baseError, string(data))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
sentry.CaptureException(err)
|
|
}
|
|
}
|