Skip to content

Commit

Permalink
return correct status code for api v2 (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
C0rby authored May 5, 2020
1 parent af76f2b commit cf692f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ const (
PermissionAll Permissions = (1 << (iota - 1)) - 1
)

var (
// ErrPermissionAboveRange defines a permission specific error.
ErrPermissionAboveRange = fmt.Errorf("The provided permission is higher than %d", PermissionAll)
)

// NewPermissions creates a new Permissions instanz.
// The value must be in the valid range.
func NewPermissions(val int) (Permissions, error) {
if val <= int(PermissionInvalid) || int(PermissionAll) < val {
if val <= int(PermissionInvalid) {
return PermissionInvalid, fmt.Errorf("permissions %d out of range %d - %d", val, PermissionRead, PermissionAll)
} else if int(PermissionAll) < val {
return PermissionInvalid, ErrPermissionAboveRange
}
return Permissions(val), nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ func (h *Handler) createShare(w http.ResponseWriter, r *http.Request) {
}
permissions, err = conversions.NewPermissions(pint)
if err != nil {
response.WriteOCSError(w, r, response.MetaBadRequest.StatusCode, err.Error(), nil)
if err == conversions.ErrPermissionAboveRange {
response.WriteOCSError(w, r, http.StatusNotFound, err.Error(), nil)
} else {
response.WriteOCSError(w, r, response.MetaBadRequest.StatusCode, err.Error(), nil)
}
return
}
role = conversions.Permissions2Role(permissions)
Expand Down

0 comments on commit cf692f7

Please sign in to comment.