Skip to content

The only tool you need to know what is happening and how to fix it.

properly handling no rows found with query

dduxba8088cde040
diff --git a/backend/app/repositories/endpoint.repository.go b/backend/app/repositories/endpoint.repository.go
index c24df1e0..72741340 100644
--- a/backend/app/repositories/endpoint.repository.go
+++ b/backend/app/repositories/endpoint.repository.go
@@ -4,7 +4,9 @@ import (
"backend/app/chdb"
"backend/app/models"
"context"
+ "database/sql"
"encoding/json"
+ "errors"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
@@ -240,6 +242,9 @@ func (e *endpointRepository) FindById(ctx context.Context, projectId, endpointId
&t.StatusCode, &t.BodySize, &t.ClientIP, &scopeJSON, &t.AppVersion, &t.ServerName)
if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil, nil
+ }
return nil, err
}
diff --git a/backend/app/repositories/exception_stack_trace.repository.go b/backend/app/repositories/exception_stack_trace.repository.go
index 2597ccc5..22d59805 100644
--- a/backend/app/repositories/exception_stack_trace.repository.go
+++ b/backend/app/repositories/exception_stack_trace.repository.go
@@ -4,7 +4,9 @@ import (
"backend/app/chdb"
"backend/app/models"
"context"
+ "database/sql"
"encoding/json"
+ "errors"
"strings"
"time"
@@ -152,7 +154,9 @@ func (e *exceptionStackTraceRepository) FindByHash(ctx context.Context, projectI
"SELECT exception_hash, any(stack_trace), max(recorded_at) as last_seen, min(recorded_at) as first_seen, count() as count FROM exception_stack_traces WHERE project_id = ? AND exception_hash = ? GROUP BY exception_hash",
projectId, exceptionHash).Scan(&group.ExceptionHash, &group.StackTrace, &group.LastSeen, &group.FirstSeen, &group.Count)
if err != nil {
- // ClickHouse returns error when no rows found in QueryRow
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil, nil, 0, nil
+ }
return nil, nil, 0, err
}
@@ -402,6 +406,9 @@ func (e *exceptionStackTraceRepository) FindById(ctx context.Context, projectId
&est.RecordedAt, &scopeJSON, &est.AppVersion, &est.ServerName, &isMessage)
if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil, nil
+ }
return nil, err
}
diff --git a/backend/app/repositories/project.repository.go b/backend/app/repositories/project.repository.go
index f55e6434..1c9ac9a4 100644
--- a/backend/app/repositories/project.repository.go
+++ b/backend/app/repositories/project.repository.go
@@ -4,6 +4,8 @@ import (
"backend/app/chdb"
"backend/app/models"
"context"
+ "database/sql"
+ "errors"
"strings"
"github.com/google/uuid"
@@ -34,6 +36,9 @@ func (p *projectRepository) FindByToken(ctx context.Context, token string) (*mod
err := (*chdb.Conn).QueryRow(ctx, "SELECT id, name, token, framework, created_at FROM projects WHERE token = ?", token).
Scan(&proj.Id, &proj.Name, &proj.Token, &proj.Framework, &proj.CreatedAt)
if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil, nil
+ }
return nil, err
}
return &proj, nil
@@ -44,6 +49,9 @@ func (p *projectRepository) FindById(ctx context.Context, id uuid.UUID) (*models
err := (*chdb.Conn).QueryRow(ctx, "SELECT id, name, token, framework, created_at FROM projects WHERE id = ?", id).
Scan(&proj.Id, &proj.Name, &proj.Token, &proj.Framework, &proj.CreatedAt)
if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil, nil
+ }
return nil, err
}
return &proj, nil
diff --git a/backend/app/repositories/task.repository.go b/backend/app/repositories/task.repository.go
index 673f355a..9e2825da 100644
--- a/backend/app/repositories/task.repository.go
+++ b/backend/app/repositories/task.repository.go
@@ -4,7 +4,9 @@ import (
"backend/app/chdb"
"backend/app/models"
"context"
+ "database/sql"
"encoding/json"
+ "errors"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
@@ -213,6 +215,9 @@ func (e *taskRepository) FindById(ctx context.Context, projectId, taskId uuid.UU
&t.ClientIP, &scopeJSON, &t.AppVersion, &t.ServerName)
if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil, nil
+ }
return nil, err
}