aboutsummaryrefslogtreecommitdiff
path: root/internal/route/api/v1/user/key.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/route/api/v1/user/key.go')
-rw-r--r--internal/route/api/v1/user/key.go29
1 files changed, 15 insertions, 14 deletions
diff --git a/internal/route/api/v1/user/key.go b/internal/route/api/v1/user/key.go
index 759f9008..0d8c4f16 100644
--- a/internal/route/api/v1/user/key.go
+++ b/internal/route/api/v1/user/key.go
@@ -5,21 +5,22 @@
package user
import (
- api "github.com/gogs/go-gogs-client"
- convert2 "gogs.io/gogs/internal/route/api/v1/convert"
- repo2 "gogs.io/gogs/internal/route/api/v1/repo"
"net/http"
+ api "github.com/gogs/go-gogs-client"
+ "github.com/pkg/errors"
+
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
- "gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/route/api/v1/convert"
+ "gogs.io/gogs/internal/route/api/v1/repo"
)
func GetUserByParamsName(c *context.APIContext, name string) *db.User {
user, err := db.GetUserByName(c.Params(name))
if err != nil {
- c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
+ c.NotFoundOrError(err, "get user by name")
return nil
}
return user
@@ -37,14 +38,14 @@ func composePublicKeysAPILink() string {
func listPublicKeys(c *context.APIContext, uid int64) {
keys, err := db.ListPublicKeys(uid)
if err != nil {
- c.ServerError("ListPublicKeys", err)
+ c.Error(err, "list public keys")
return
}
apiLink := composePublicKeysAPILink()
apiKeys := make([]*api.PublicKey, len(keys))
for i := range keys {
- apiKeys[i] = convert2.ToPublicKey(apiLink, keys[i])
+ apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
}
c.JSONSuccess(&apiKeys)
@@ -65,29 +66,29 @@ func ListPublicKeys(c *context.APIContext) {
func GetPublicKey(c *context.APIContext) {
key, err := db.GetPublicKeyByID(c.ParamsInt64(":id"))
if err != nil {
- c.NotFoundOrServerError("GetPublicKeyByID", db.IsErrKeyNotExist, err)
+ c.NotFoundOrError(err, "get public key by ID")
return
}
apiLink := composePublicKeysAPILink()
- c.JSONSuccess(convert2.ToPublicKey(apiLink, key))
+ c.JSONSuccess(convert.ToPublicKey(apiLink, key))
}
// CreateUserPublicKey creates new public key to given user by ID.
func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) {
content, err := db.CheckPublicKeyString(form.Key)
if err != nil {
- repo2.HandleCheckKeyStringError(c, err)
+ repo.HandleCheckKeyStringError(c, err)
return
}
key, err := db.AddPublicKey(uid, form.Title, content)
if err != nil {
- repo2.HandleAddKeyError(c, err)
+ repo.HandleAddKeyError(c, err)
return
}
apiLink := composePublicKeysAPILink()
- c.JSON(http.StatusCreated, convert2.ToPublicKey(apiLink, key))
+ c.JSON(http.StatusCreated, convert.ToPublicKey(apiLink, key))
}
func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
@@ -97,9 +98,9 @@ func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
func DeletePublicKey(c *context.APIContext) {
if err := db.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
if db.IsErrKeyAccessDenied(err) {
- c.Error(http.StatusForbidden, "", "you do not have access to this key")
+ c.ErrorStatus(http.StatusForbidden, errors.New("You do not have access to this key."))
} else {
- c.Error(http.StatusInternalServerError, "DeletePublicKey", err)
+ c.Error(err, "delete public key")
}
return
}