Skip to content

Commit

Permalink
added errtype to http code mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
2403905 committed Mar 5, 2024
1 parent 36132e4 commit 98aa848
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
12 changes: 1 addition & 11 deletions internal/http/services/owncloud/ocdav/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,16 +646,6 @@ func (s *svc) unlockReference(ctx context.Context, _ http.ResponseWriter, r *htt
err := s.LockSystem.Unlock(ctx, time.Now(), ref, t)
if err == nil {
return http.StatusNoContent, nil
} else {
switch err.(type) {
case errtypes.Aborted, errtypes.Locked:
return http.StatusLocked, err
case errtypes.PermissionDenied:
return http.StatusForbidden, err
case errtypes.PreconditionFailed:
return http.StatusConflict, err
default:
return http.StatusInternalServerError, err
}
}
return errtypes.NewHTTPStatusCodeFromErrtype(err), err
}
34 changes: 34 additions & 0 deletions pkg/errtypes/errtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,37 @@ func NewErrtypeFromHTTPStatusCode(code int, message string) error {
return InternalError(message)
}
}

// NewHTTPStatusCodeFromErrtype maps an errtype to an http status
func NewHTTPStatusCodeFromErrtype(err error) int {
switch err.(type) {
case NotFound:
return http.StatusNotFound
case AlreadyExists:
return http.StatusConflict
case NotSupported:
return http.StatusNotImplemented
case NotModified:
return http.StatusNotModified
case InvalidCredentials:
return http.StatusUnauthorized
case PermissionDenied:
return http.StatusForbidden
case Locked:
return http.StatusLocked
case Aborted:
return http.StatusPreconditionFailed
case PreconditionFailed:
return http.StatusMethodNotAllowed
case InsufficientStorage:
return http.StatusInsufficientStorage
case BadRequest:
return http.StatusBadRequest
case PartialContent:
return http.StatusPartialContent
case ChecksumMismatch:
return StatusChecksumMismatch
default:
return http.StatusInternalServerError
}
}

0 comments on commit 98aa848

Please sign in to comment.