@@ -12,6 +12,7 @@ import (
1212 "github.com/gotify/server/v2/auth"
1313 "github.com/gotify/server/v2/model"
1414 "github.com/h2non/filetype"
15+ "gorm.io/gorm"
1516)
1617
1718// The ApplicationDatabase interface for encapsulating database access.
@@ -102,7 +103,8 @@ func (a *ApplicationAPI) CreateApplication(ctx *gin.Context) {
102103 Internal : false ,
103104 }
104105
105- if success := successOrAbort (ctx , 500 , a .DB .CreateApplication (& app )); ! success {
106+ if err := a .DB .CreateApplication (& app ); err != nil {
107+ handleApplicationError (ctx , err )
106108 return
107109 }
108110 ctx .JSON (200 , withResolvedImage (& app ))
@@ -262,7 +264,8 @@ func (a *ApplicationAPI) UpdateApplication(ctx *gin.Context) {
262264 app .SortKey = applicationParams .SortKey
263265 }
264266
265- if success := successOrAbort (ctx , 500 , a .DB .UpdateApplication (app )); ! success {
267+ if err := a .DB .UpdateApplication (app ); err != nil {
268+ handleApplicationError (ctx , err )
266269 return
267270 }
268271 ctx .JSON (200 , withResolvedImage (app ))
@@ -477,3 +480,11 @@ func ValidApplicationImageExt(ext string) bool {
477480 return false
478481 }
479482}
483+
484+ func handleApplicationError (ctx * gin.Context , err error ) {
485+ if errors .Is (err , gorm .ErrDuplicatedKey ) {
486+ ctx .AbortWithError (400 , errors .New ("sort key is not unique" ))
487+ } else {
488+ ctx .AbortWithError (500 , err )
489+ }
490+ }
0 commit comments