ms-auth/pkg/errors.go

38 lines
916 B
Go
Raw Normal View History

2024-12-30 20:04:26 +05:00
package pkg
import (
"errors"
"fmt"
2025-02-25 18:33:15 +05:00
"net/http"
2024-12-30 20:04:26 +05:00
)
var (
NoPermission = errors.New("no permission")
ErrUnauthenticated = errors.New("unauthenticated")
ErrUnhandled = errors.New("unhandled")
ErrNotFound = errors.New("not found")
ErrBadInput = errors.New("bad input")
ErrInternal = errors.New("internal")
)
func Wrap(basic error, err error, op string, msg string) error {
return errors.Join(basic, err, fmt.Errorf("during %s: %s", op, msg))
}
2025-02-25 18:33:15 +05:00
func ToREST(err error) int {
2024-12-30 20:04:26 +05:00
switch {
case errors.Is(err, ErrUnauthenticated):
2025-02-25 18:33:15 +05:00
return http.StatusUnauthorized
2024-12-30 20:04:26 +05:00
case errors.Is(err, ErrBadInput):
2025-02-25 18:33:15 +05:00
return http.StatusBadRequest
2024-12-30 20:04:26 +05:00
case errors.Is(err, ErrNotFound):
2025-02-25 18:33:15 +05:00
return http.StatusNotFound
2024-12-30 20:04:26 +05:00
case errors.Is(err, ErrInternal):
2025-02-25 18:33:15 +05:00
return http.StatusInternalServerError
2024-12-30 20:04:26 +05:00
case errors.Is(err, NoPermission):
2025-02-25 18:33:15 +05:00
return http.StatusForbidden
2024-12-30 20:04:26 +05:00
}
2025-02-25 18:33:15 +05:00
return http.StatusInternalServerError
2024-12-30 20:04:26 +05:00
}