{"record":{"id":"8b626c2ffca964d6","repo":"AlistGo/alist","slug":"label-name-is-exists","errorCode":null,"errorMessage":"label name is exists","messagePattern":"label name is exists","errorType":"http","errorClass":null,"httpStatus":401,"severity":"warning","filePath":"server/handles/label.go","lineNumber":55,"sourceCode":"\t\tcommon.ErrorResp(c, err, 400)\n\t\treturn\n\t}\n\tlabel, err := db.GetLabelById(uint(id))\n\tif err != nil {\n\t\tcommon.ErrorResp(c, err, 500, true)\n\t\treturn\n\t}\n\tcommon.SuccessResp(c, label)\n}\n\nfunc CreateLabel(c *gin.Context) {\n\tvar req model.Label\n\tif err := c.ShouldBind(&req); err != nil {\n\t\tcommon.ErrorResp(c, err, 400)\n\t\treturn\n\t}\n\tif db.GetLabelByName(req.Name) {\n\t\tcommon.ErrorResp(c, errors.New(\"label name is exists\"), 401)\n\t\treturn\n\t}\n\tif id, err := db.CreateLabel(req); err != nil {\n\t\tcommon.ErrorWithDataResp(c, err, 500, gin.H{\n\t\t\t\"id\": id,\n\t\t}, true)\n\t} else {\n\t\tcommon.SuccessResp(c, gin.H{\n\t\t\t\"id\": id,\n\t\t})\n\t}\n}\n\nfunc UpdateLabel(c *gin.Context) {\n\tvar req model.Label\n\tif err := c.ShouldBind(&req); err != nil {\n\t\tcommon.ErrorResp(c, err, 400)\n\t\treturn","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/AlistGo/alist/blob/843d9dc8149126976b2625911e45a4d3ffd6f2f5/server/handles/label.go#L37-L73","documentation":"Returned by the CreateLabel HTTP handler when db.GetLabelByName reports a label with the same name already exists. Label names are treated as globally unique; the handler returns HTTP 401 (an unusual status choice for a duplicate-resource condition — semantically 409 would fit). The check is the only uniqueness validation before db.CreateLabel.","triggerScenarios":"POST to the label-creation endpoint (/api/fs/labels or the label route group) with a body whose name equals an existing label's name.","commonSituations":"Re-running an import/migration script that creates labels idempotently-by-name; two admins creating the same tag; a UI retry after a timeout that actually succeeded server-side.","solutions":["Choose a different, unique label name","If the label already exists, reuse it (fetch by name) instead of creating a duplicate","For scripts: check existence with the get-label-by-name endpoint before POSTing"],"exampleFix":"// before\nPOST /api/label/create {\"name\": \"important\"}  // 'important' already exists\n// after\nGET /api/label/name/important -> 200, reuse its id","handlingStrategy":"validation","validationCode":"// Before creating, check the name is free\nif existing := db.GetLabelByName(req.Name); existing {\n    return fmt.Errorf(\"label %q already exists\", req.Name)\n}","typeGuard":null,"tryCatchPattern":"resp, err := client.R().SetBody(label).Post(\"/api/label/create\")\nif err == nil && resp.StatusCode() == 401 && strings.Contains(resp.String(), \"label name is exists\") {\n    // fetch the existing label by name and reuse its id\n}","preventionTips":["Make label-creation scripts idempotent: lookup-by-name first, create only on miss","Note the endpoint reports duplicates as HTTP 401 — match on the message, not a conventional 409","Trim/normalize label names client-side to avoid near-duplicate churn"],"tags":["labels","http-api","validation","duplicate"],"backgroundTag":null,"analyzedSha":"843d9dc8149126976b2625911e45a4d3ffd6f2f5","analyzedAt":"2026-08-15T12:14:11.722Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}