diff --git a/routers/api/v1/licensing/download.go b/routers/api/v1/licensing/download.go index 8cb4319e63..32adc6f852 100644 --- a/routers/api/v1/licensing/download.go +++ b/routers/api/v1/licensing/download.go @@ -27,40 +27,40 @@ func ServeDownload(ctx *context.APIContext) { version, ok := licensing_service.ParseDownloadParams(versionFile) if !ok { - ctx.Error(http.StatusBadRequest, "invalid version format", nil) + ctx.APIError(http.StatusBadRequest, "invalid version format") return } expires, ok := licensing_service.ParseExpires(expiresStr) if !ok || token == "" || dlid == "" { - ctx.Error(http.StatusForbidden, "missing or invalid download parameters", nil) + ctx.APIError(http.StatusForbidden, "missing or invalid download parameters") return } // Verify signed token if !licensing_service.VerifyDownloadToken(token, product, version, dlid, expires) { - ctx.Error(http.StatusForbidden, "invalid or expired download token", nil) + ctx.APIError(http.StatusForbidden, "invalid or expired download token") return } // Verify DLID is still valid license, err := licensing_model.GetLicenseByDLID(ctx, dlid) if err != nil || license == nil || !license.IsActive() { - ctx.Error(http.StatusForbidden, "license invalid or expired", nil) + ctx.APIError(http.StatusForbidden, "license invalid or expired") return } // Verify entitlement has, _ := licensing_model.HasEntitlement(ctx, license.ID, product) if !has { - ctx.Error(http.StatusForbidden, "no entitlement for product", nil) + ctx.APIError(http.StatusForbidden, "no entitlement for product") return } // Resolve repo from entitlement ents, err := licensing_model.GetEntitlementsByLicense(ctx, license.ID) if err != nil { - ctx.Error(http.StatusInternalServerError, "failed to get entitlements", err) + ctx.APIError(http.StatusInternalServerError, "failed to get entitlements") return } var repoOwner, repoName string @@ -72,14 +72,14 @@ func ServeDownload(ctx *context.APIContext) { } } if repoName == "" { - ctx.Error(http.StatusNotFound, "product repo not found", nil) + ctx.APIError(http.StatusNotFound, "product repo not found") return } // Find repo repo, err := repo_model.GetRepositoryByOwnerAndName(ctx, repoOwner, repoName) if err != nil || repo == nil { - ctx.Error(http.StatusNotFound, "repository not found", err) + ctx.APIError(http.StatusNotFound, "repository not found") return } @@ -91,7 +91,7 @@ func ServeDownload(ctx *context.APIContext) { IncludeTags: false, }) if err != nil { - ctx.Error(http.StatusInternalServerError, "failed to list releases", err) + ctx.APIError(http.StatusInternalServerError, "failed to list releases") return } @@ -108,14 +108,14 @@ func ServeDownload(ctx *context.APIContext) { } } if targetRelease == nil { - ctx.Error(http.StatusNotFound, fmt.Sprintf("release version %s not found", version), nil) + ctx.APIError(http.StatusNotFound, fmt.Sprintf("release version %s not found", version)) return } // Find ZIP attachment attachments, err := repo_model.GetAttachmentsByReleaseID(ctx, targetRelease.ID) if err != nil { - ctx.Error(http.StatusInternalServerError, "failed to get attachments", err) + ctx.APIError(http.StatusInternalServerError, "failed to get attachments") return } @@ -127,7 +127,7 @@ func ServeDownload(ctx *context.APIContext) { } } if zipAttachment == nil { - ctx.Error(http.StatusNotFound, "no zip attachment found for release", nil) + ctx.APIError(http.StatusNotFound, "no zip attachment found for release") return } @@ -138,7 +138,7 @@ func ServeDownload(ctx *context.APIContext) { // Serve the file fr, err := storage.Attachments.Open(zipAttachment.RelativePath()) if err != nil { - ctx.Error(http.StatusInternalServerError, "failed to open attachment", err) + ctx.APIError(http.StatusInternalServerError, "failed to open attachment") return } defer fr.Close() diff --git a/routers/api/v1/licensing/manage.go b/routers/api/v1/licensing/manage.go index d3df99b2d9..f27d378d6b 100644 --- a/routers/api/v1/licensing/manage.go +++ b/routers/api/v1/licensing/manage.go @@ -29,7 +29,7 @@ type createLicenseRequest struct { func CreateLicense(ctx *context.APIContext) { var req createLicenseRequest if err := ctx.BindJSON(&req); err != nil { - ctx.Error(http.StatusBadRequest, "invalid request body", err) + ctx.APIError(http.StatusBadRequest, "invalid request body") return } @@ -52,7 +52,7 @@ func CreateLicense(ctx *context.APIContext) { license, err := licensing_model.CreateLicense(ctx, req.UserID, req.Tier, maxDomains, expiresAt) if err != nil { - ctx.Error(http.StatusInternalServerError, "failed to create license", err) + ctx.APIError(http.StatusInternalServerError, "failed to create license") return } @@ -85,7 +85,7 @@ func ListLicenses(ctx *context.APIContext) { var licenses []*licensing_model.License err := ctx.Orm().Limit(limit, (page-1)*limit).Find(&licenses) if err != nil { - ctx.Error(http.StatusInternalServerError, "failed to list licenses", err) + ctx.APIError(http.StatusInternalServerError, "failed to list licenses") return } @@ -100,7 +100,7 @@ func ListLicenses(ctx *context.APIContext) { func GetLicense(ctx *context.APIContext) { id, err := strconv.ParseInt(ctx.PathParam("id"), 10, 64) if err != nil { - ctx.Error(http.StatusBadRequest, "invalid license ID", err) + ctx.APIError(http.StatusBadRequest, "invalid license ID") return } @@ -154,7 +154,7 @@ type updateLicenseRequest struct { func UpdateLicense(ctx *context.APIContext) { id, err := strconv.ParseInt(ctx.PathParam("id"), 10, 64) if err != nil { - ctx.Error(http.StatusBadRequest, "invalid license ID", err) + ctx.APIError(http.StatusBadRequest, "invalid license ID") return } @@ -166,13 +166,13 @@ func UpdateLicense(ctx *context.APIContext) { var req updateLicenseRequest if err := ctx.BindJSON(&req); err != nil { - ctx.Error(http.StatusBadRequest, "invalid request body", err) + ctx.APIError(http.StatusBadRequest, "invalid request body") return } if req.Tier != nil && *req.Tier != license.Tier { if err := licensing_model.UpdateLicenseTier(ctx, id, *req.Tier); err != nil { - ctx.Error(http.StatusInternalServerError, "failed to update tier", err) + ctx.APIError(http.StatusInternalServerError, "failed to update tier") return } license.Tier = *req.Tier @@ -180,7 +180,7 @@ func UpdateLicense(ctx *context.APIContext) { if req.Status != nil && *req.Status != license.Status { if err := licensing_model.SetLicenseStatus(ctx, id, *req.Status); err != nil { - ctx.Error(http.StatusInternalServerError, "failed to update status", err) + ctx.APIError(http.StatusInternalServerError, "failed to update status") return } license.Status = *req.Status @@ -215,12 +215,12 @@ func UpdateLicense(ctx *context.APIContext) { func DeleteLicense(ctx *context.APIContext) { id, err := strconv.ParseInt(ctx.PathParam("id"), 10, 64) if err != nil { - ctx.Error(http.StatusBadRequest, "invalid license ID", err) + ctx.APIError(http.StatusBadRequest, "invalid license ID") return } if err := licensing_model.RevokeLicense(ctx, id); err != nil { - ctx.Error(http.StatusInternalServerError, "failed to revoke license", err) + ctx.APIError(http.StatusInternalServerError, "failed to revoke license") return } @@ -233,7 +233,7 @@ func DeleteLicense(ctx *context.APIContext) { func MyLicenses(ctx *context.APIContext) { licenses, err := licensing_model.GetLicensesByUser(ctx, ctx.Doer.ID) if err != nil { - ctx.Error(http.StatusInternalServerError, "failed to list licenses", err) + ctx.APIError(http.StatusInternalServerError, "failed to list licenses") return } @@ -248,7 +248,7 @@ func MyLicenses(ctx *context.APIContext) { func MyLicenseDomains(ctx *context.APIContext) { id, err := strconv.ParseInt(ctx.PathParam("id"), 10, 64) if err != nil { - ctx.Error(http.StatusBadRequest, "invalid license ID", err) + ctx.APIError(http.StatusBadRequest, "invalid license ID") return } @@ -260,7 +260,7 @@ func MyLicenseDomains(ctx *context.APIContext) { acts, err := licensing_model.GetActivationsByLicense(ctx, id) if err != nil { - ctx.Error(http.StatusInternalServerError, "failed to list domains", err) + ctx.APIError(http.StatusInternalServerError, "failed to list domains") return } @@ -279,7 +279,7 @@ func MyLicenseDomains(ctx *context.APIContext) { func MyDeactivateDomain(ctx *context.APIContext) { id, err := strconv.ParseInt(ctx.PathParam("id"), 10, 64) if err != nil { - ctx.Error(http.StatusBadRequest, "invalid license ID", err) + ctx.APIError(http.StatusBadRequest, "invalid license ID") return } @@ -291,7 +291,7 @@ func MyDeactivateDomain(ctx *context.APIContext) { domain := ctx.PathParam("domain") if err := licensing_model.DeactivateDomain(ctx, id, domain); err != nil { - ctx.Error(http.StatusInternalServerError, "failed to deactivate domain", err) + ctx.APIError(http.StatusInternalServerError, "failed to deactivate domain") return } @@ -304,7 +304,7 @@ func MyDeactivateDomain(ctx *context.APIContext) { func ListTiers(ctx *context.APIContext) { tiers, err := licensing_model.GetAllProductTiers(ctx) if err != nil { - ctx.Error(http.StatusInternalServerError, "failed to list tiers", err) + ctx.APIError(http.StatusInternalServerError, "failed to list tiers") return } @@ -327,7 +327,7 @@ type createTierRequest struct { func CreateTier(ctx *context.APIContext) { var req createTierRequest if err := ctx.BindJSON(&req); err != nil { - ctx.Error(http.StatusBadRequest, "invalid request body", err) + ctx.APIError(http.StatusBadRequest, "invalid request body") return } @@ -342,7 +342,7 @@ func CreateTier(ctx *context.APIContext) { _, err := ctx.Orm().Insert(tier) if err != nil { - ctx.Error(http.StatusInternalServerError, "failed to create tier", err) + ctx.APIError(http.StatusInternalServerError, "failed to create tier") return } @@ -360,7 +360,7 @@ type updateTierRequest struct { func UpdateTier(ctx *context.APIContext) { id, err := strconv.ParseInt(ctx.PathParam("id"), 10, 64) if err != nil { - ctx.Error(http.StatusBadRequest, "invalid tier ID", err) + ctx.APIError(http.StatusBadRequest, "invalid tier ID") return } @@ -373,7 +373,7 @@ func UpdateTier(ctx *context.APIContext) { var req updateTierRequest if err := ctx.BindJSON(&req); err != nil { - ctx.Error(http.StatusBadRequest, "invalid request body", err) + ctx.APIError(http.StatusBadRequest, "invalid request body") return } @@ -407,7 +407,7 @@ func UpdateTier(ctx *context.APIContext) { func DeleteTier(ctx *context.APIContext) { id, err := strconv.ParseInt(ctx.PathParam("id"), 10, 64) if err != nil { - ctx.Error(http.StatusBadRequest, "invalid tier ID", err) + ctx.APIError(http.StatusBadRequest, "invalid tier ID") return } @@ -421,7 +421,7 @@ func DeleteTier(ctx *context.APIContext) { count, _ := ctx.Orm().Where("tier = ?", tier.TierKey).Count(new(licensing_model.License)) if count > 0 { - ctx.Error(http.StatusConflict, "cannot delete tier with active licenses", nil) + ctx.APIError(http.StatusConflict, "cannot delete tier with active licenses") return }