1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-09 02:05:59 +01:00

Improve HTTP error for unknown permissions.

This commit is contained in:
Juliusz Chroboczek 2024-11-08 17:07:11 +01:00
parent 75d6d87c23
commit a73c42ce08
2 changed files with 7 additions and 2 deletions

View file

@ -17,6 +17,7 @@ import (
var ErrTagMismatch = errors.New("tag mismatch") var ErrTagMismatch = errors.New("tag mismatch")
var ErrDescriptionsNotWritable = &NotAuthorisedError{} var ErrDescriptionsNotWritable = &NotAuthorisedError{}
var ErrUnknownPermission = errors.New("unknown permission")
type Permissions struct { type Permissions struct {
// non-empty for a named permissions set // non-empty for a named permissions set
@ -37,7 +38,7 @@ var permissionsMap = map[string][]string{
func NewPermissions(name string) (Permissions, error) { func NewPermissions(name string) (Permissions, error) {
_, ok := permissionsMap[name] _, ok := permissionsMap[name]
if !ok { if !ok {
return Permissions{}, errors.New("unknown permission") return Permissions{}, ErrUnknownPermission
} }
return Permissions{ return Permissions{
name: name, name: name,
@ -112,7 +113,7 @@ func (p *Permissions) UnmarshalJSON(b []byte) error {
if err == nil { if err == nil {
_, ok := permissionsMap[s] _, ok := permissionsMap[s]
if !ok { if !ok {
return errors.New("Unknown permission " + s) return ErrUnknownPermission
} }
*p = Permissions{ *p = Permissions{
name: s, name: s,

View file

@ -124,6 +124,10 @@ func httpError(w http.ResponseWriter, err error) {
notFound(w) notFound(w)
return return
} }
if errors.Is(err, group.ErrUnknownPermission) {
http.Error(w, "unknown permission", http.StatusBadRequest)
return
}
var autherr *group.NotAuthorisedError var autherr *group.NotAuthorisedError
if errors.As(err, &autherr) { if errors.As(err, &autherr) {
log.Printf("HTTP server error: %v", err) log.Printf("HTTP server error: %v", err)