-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Return 404 from Contents API when items don't exist #10323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
c94671c
1ef8fdc
b618883
3b72c5c
4297c11
6326239
6dd5071
4d8dd1b
4a9d221
2fc065d
7132fc1
dd35741
21423db
ecc9837
3bbf36f
bb49646
77ff95f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -362,9 +362,15 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) { | |
// responses: | ||
// "200": | ||
// "$ref": "#/responses/FileDeleteResponse" | ||
// "400": | ||
// "$ref": "#/responses/error" | ||
// "403": | ||
// "$ref": "#/responses/error" | ||
// "404": | ||
// "$ref": "#/responses/error" | ||
|
||
if !CanWriteFiles(ctx.Repo) { | ||
ctx.Error(http.StatusInternalServerError, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{ | ||
ctx.Error(http.StatusForbidden, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hesitate to think this should maybe kept as In case you want to keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you have read only access it should be forbidden status, in case you don't have any access it should be not found status but not internal server error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to get to DeleteFile you have to pass: m.Delete("", bind(api.DeleteFileOptions{}), repo.DeleteFile)
}, reqRepoWriter(models.UnitTypeCode), reqToken())
}, reqRepoReader(models.UnitTypeCode))
...
}, repoAssignment()) CanWriteFiles does the following in addition: // CanWriteFiles returns true if repository is editable and user has proper access level.
func CanWriteFiles(r *context.Repository) bool {
return r.Permission.CanWrite(models.UnitTypeCode) && !r.Repository.IsMirror && !r.Repository.IsArchived
} Therefore HttpStatusForbidden or HttpBadRequest are the correct statuses not 404 nor 500. |
||
UserID: ctx.User.ID, | ||
RepoName: ctx.Repo.Repository.LowerName, | ||
}) | ||
|
@@ -402,9 +408,23 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) { | |
} | ||
|
||
if fileResponse, err := repofiles.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil { | ||
if git.IsErrBranchNotExist(err) || models.IsErrRepoFileDoesNotExist(err) || git.IsErrNotExist(err) { | ||
ctx.Error(http.StatusNotFound, "DeleteFile", err) | ||
return | ||
} else if models.IsErrBranchAlreadyExists(err) || | ||
models.IsErrFilenameInvalid(err) || | ||
models.IsErrSHADoesNotMatch(err) || | ||
models.IsErrCommitIDDoesNotMatch(err) || | ||
models.IsErrSHAOrCommitIDNotProvided(err) { | ||
ctx.Error(http.StatusBadRequest, "DeleteFile", err) | ||
return | ||
} else if models.IsErrUserCannotCommit(err) { | ||
ctx.Error(http.StatusForbidden, "DeleteFile", err) | ||
return | ||
} | ||
ctx.Error(http.StatusInternalServerError, "DeleteFile", err) | ||
} else { | ||
ctx.JSON(http.StatusOK, fileResponse) | ||
ctx.JSON(http.StatusOK, fileResponse) // FIXME on APIv2: return http.StatusNoContent | ||
} | ||
} | ||
|
||
|
@@ -439,6 +459,8 @@ func GetContents(ctx *context.APIContext) { | |
// responses: | ||
// "200": | ||
// "$ref": "#/responses/ContentsResponse" | ||
// "404": | ||
// "$ref": "#/responses/notFound" | ||
|
||
if !CanReadFiles(ctx.Repo) { | ||
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{ | ||
|
@@ -452,6 +474,10 @@ func GetContents(ctx *context.APIContext) { | |
ref := ctx.QueryTrim("ref") | ||
|
||
if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil { | ||
if git.IsErrNotExist(err) { | ||
ctx.NotFound("GetContentsOrList", err) | ||
return | ||
} | ||
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err) | ||
} else { | ||
ctx.JSON(http.StatusOK, fileList) | ||
|
@@ -484,6 +510,8 @@ func GetContentsList(ctx *context.APIContext) { | |
// responses: | ||
// "200": | ||
// "$ref": "#/responses/ContentsListResponse" | ||
// "404": | ||
// "$ref": "#/responses/notFound" | ||
|
||
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface | ||
GetContents(ctx) | ||
|
Uh oh!
There was an error while loading. Please reload this page.