Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

chore: return proper http codes on unique constraint error (#6428)

Vikrant Gupta01fda5195936
diff --git a/ee/query-service/license/db.go b/ee/query-service/license/db.go
index 12df69233d..eae48e266d 100644
--- a/ee/query-service/license/db.go
+++ b/ee/query-service/license/db.go
@@ -8,6 +8,7 @@ import (
"time"
"github.com/jmoiron/sqlx"
+ "github.com/mattn/go-sqlite3"
"go.signoz.io/signoz/ee/query-service/license/sqlite"
"go.signoz.io/signoz/ee/query-service/model"
@@ -274,14 +275,14 @@ func (r *Repo) InitFeatures(req basemodel.FeatureSet) error {
}
// InsertLicenseV3 inserts a new license v3 in db
-func (r *Repo) InsertLicenseV3(ctx context.Context, l *model.LicenseV3) error {
+func (r *Repo) InsertLicenseV3(ctx context.Context, l *model.LicenseV3) *model.ApiError {
query := `INSERT INTO licenses_v3 (id, key, data) VALUES ($1, $2, $3)`
// licsense is the entity of zeus so putting the entire license here without defining schema
licenseData, err := json.Marshal(l.Data)
if err != nil {
- return fmt.Errorf("insert license failed: license marshal error")
+ return &model.ApiError{Typ: basemodel.ErrorBadData, Err: err}
}
_, err = r.db.ExecContext(ctx,
@@ -292,8 +293,14 @@ func (r *Repo) InsertLicenseV3(ctx context.Context, l *model.LicenseV3) error {
)
if err != nil {
+ if sqliteErr, ok := err.(sqlite3.Error); ok {
+ if sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
+ zap.L().Error("error in inserting license data: ", zap.Error(sqliteErr))
+ return &model.ApiError{Typ: model.ErrorConflict, Err: sqliteErr}
+ }
+ }
zap.L().Error("error in inserting license data: ", zap.Error(err))
- return fmt.Errorf("failed to insert license in db: %v", err)
+ return &model.ApiError{Typ: basemodel.ErrorExec, Err: err}
}
return nil
diff --git a/ee/query-service/license/manager.go b/ee/query-service/license/manager.go
index 13b869da8c..6dcc704e3a 100644
--- a/ee/query-service/license/manager.go
+++ b/ee/query-service/license/manager.go
@@ -463,7 +463,7 @@ func (lm *Manager) ActivateV3(ctx context.Context, licenseKey string) (licenseRe
err := lm.repo.InsertLicenseV3(ctx, license)
if err != nil {
zap.L().Error("failed to activate license", zap.Error(err))
- return nil, model.InternalError(err)
+ return nil, err
}
// license is valid, activate it
diff --git a/pkg/query-service/app/http_handler.go b/pkg/query-service/app/http_handler.go
index 0f6d351af7..6586e21d98 100644
--- a/pkg/query-service/app/http_handler.go
+++ b/pkg/query-service/app/http_handler.go
@@ -332,6 +332,8 @@ func RespondError(w http.ResponseWriter, apiErr model.BaseApiError, data interfa
code = http.StatusUnauthorized
case model.ErrorForbidden:
code = http.StatusForbidden
+ case model.ErrorConflict:
+ code = http.StatusConflict
default:
code = http.StatusInternalServerError
}