2019-01-05 22:44:33 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2020-06-06 15:45:51 +00:00
|
|
|
"errors"
|
2019-01-05 22:44:33 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2024-12-17 00:01:55 +00:00
|
|
|
libErrors "github.com/gtsteffaniak/filebrowser/backend/errors"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func errToStatus(err error) int {
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return http.StatusOK
|
|
|
|
case os.IsPermission(err):
|
|
|
|
return http.StatusForbidden
|
2020-06-06 15:45:51 +00:00
|
|
|
case os.IsNotExist(err), err == libErrors.ErrNotExist:
|
2019-01-05 22:44:33 +00:00
|
|
|
return http.StatusNotFound
|
2020-06-06 15:45:51 +00:00
|
|
|
case os.IsExist(err), err == libErrors.ErrExist:
|
2019-01-05 22:44:33 +00:00
|
|
|
return http.StatusConflict
|
2020-06-06 15:45:51 +00:00
|
|
|
case errors.Is(err, libErrors.ErrPermissionDenied):
|
|
|
|
return http.StatusForbidden
|
|
|
|
case errors.Is(err, libErrors.ErrInvalidRequestParams):
|
|
|
|
return http.StatusBadRequest
|
2021-01-11 21:33:36 +00:00
|
|
|
case errors.Is(err, libErrors.ErrRootUserDeletion):
|
|
|
|
return http.StatusForbidden
|
2019-01-05 22:44:33 +00:00
|
|
|
default:
|
|
|
|
return http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
}
|