Return a user-friendly error for an invalid file mode

This commit is contained in:
Matthew Penner 2020-12-25 14:41:38 -07:00
parent e05c601325
commit 0a45ea44a4

View File

@ -440,6 +440,8 @@ type chmodFile struct {
Mode string `json:"mode"` Mode string `json:"mode"`
} }
var errInvalidFileMode = errors.New("invalid file mode")
func postServerChmodFile(c *gin.Context) { func postServerChmodFile(c *gin.Context) {
s := GetServer(c.Param("server")) s := GetServer(c.Param("server"))
@ -471,7 +473,7 @@ func postServerChmodFile(c *gin.Context) {
default: default:
mode, err := strconv.ParseUint(p.Mode, 8, 32) mode, err := strconv.ParseUint(p.Mode, 8, 32)
if err != nil { if err != nil {
return err return errInvalidFileMode
} }
if err := s.Filesystem().Chmod(path.Join(data.Root, p.File), os.FileMode(mode)); err != nil { if err := s.Filesystem().Chmod(path.Join(data.Root, p.File), os.FileMode(mode)); err != nil {
@ -490,6 +492,13 @@ func postServerChmodFile(c *gin.Context) {
} }
if err := g.Wait(); err != nil { if err := g.Wait(); err != nil {
if errors.Is(err, errInvalidFileMode) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": "Invalid file mode.",
})
return
}
NewServerError(err, s).AbortFilesystemError(c) NewServerError(err, s).AbortFilesystemError(c)
return return
} }