@@ -2,11 +2,9 @@ package api
22
33import (
44 "context"
5- "errors"
65 "log/slog"
76
87 "connectrpc.com/connect"
9- "github.com/jackc/pgx/v5"
108 "github.com/oklog/ulid/v2"
119 "google.golang.org/protobuf/types/known/timestamppb"
1210
@@ -49,31 +47,31 @@ func (h *AssignmentHandler) CreateAssignment(ctx context.Context, req *connect.R
4947 case pm .AssignmentSourceType_ASSIGNMENT_SOURCE_TYPE_ACTION :
5048 _ , err := h .store .Queries ().GetActionByID (ctx , req .Msg .SourceId )
5149 if err != nil {
52- if errors . Is (err , pgx . ErrNoRows ) {
50+ if store . IsNotFound (err ) {
5351 return nil , apiErrorCtx (ctx , ErrActionNotFound , connect .CodeNotFound , "action not found" )
5452 }
5553 return nil , apiErrorCtx (ctx , ErrInternal , connect .CodeInternal , "failed to get action" )
5654 }
5755 case pm .AssignmentSourceType_ASSIGNMENT_SOURCE_TYPE_ACTION_SET :
5856 _ , err := h .store .Queries ().GetActionSetByID (ctx , req .Msg .SourceId )
5957 if err != nil {
60- if errors . Is (err , pgx . ErrNoRows ) {
58+ if store . IsNotFound (err ) {
6159 return nil , apiErrorCtx (ctx , ErrActionSetNotFound , connect .CodeNotFound , "action set not found" )
6260 }
6361 return nil , apiErrorCtx (ctx , ErrInternal , connect .CodeInternal , "failed to get action set" )
6462 }
6563 case pm .AssignmentSourceType_ASSIGNMENT_SOURCE_TYPE_DEFINITION :
6664 _ , err := h .store .Queries ().GetDefinitionByID (ctx , req .Msg .SourceId )
6765 if err != nil {
68- if errors . Is (err , pgx . ErrNoRows ) {
66+ if store . IsNotFound (err ) {
6967 return nil , apiErrorCtx (ctx , ErrDefinitionNotFound , connect .CodeNotFound , "definition not found" )
7068 }
7169 return nil , apiErrorCtx (ctx , ErrInternal , connect .CodeInternal , "failed to get definition" )
7270 }
7371 case pm .AssignmentSourceType_ASSIGNMENT_SOURCE_TYPE_COMPLIANCE_POLICY :
7472 _ , err := h .store .Queries ().GetCompliancePolicyByID (ctx , req .Msg .SourceId )
7573 if err != nil {
76- if errors . Is (err , pgx . ErrNoRows ) {
74+ if store . IsNotFound (err ) {
7775 return nil , apiErrorCtx (ctx , ErrCompliancePolicyNotFound , connect .CodeNotFound , "compliance policy not found" )
7876 }
7977 return nil , apiErrorCtx (ctx , ErrInternal , connect .CodeInternal , "failed to get compliance policy" )
@@ -85,31 +83,31 @@ func (h *AssignmentHandler) CreateAssignment(ctx context.Context, req *connect.R
8583 case pm .AssignmentTargetType_ASSIGNMENT_TARGET_TYPE_DEVICE :
8684 _ , err := h .store .Queries ().GetDeviceByID (ctx , db.GetDeviceByIDParams {ID : req .Msg .TargetId })
8785 if err != nil {
88- if errors . Is (err , pgx . ErrNoRows ) {
86+ if store . IsNotFound (err ) {
8987 return nil , apiErrorCtx (ctx , ErrDeviceNotFound , connect .CodeNotFound , "device not found" )
9088 }
9189 return nil , apiErrorCtx (ctx , ErrInternal , connect .CodeInternal , "failed to get device" )
9290 }
9391 case pm .AssignmentTargetType_ASSIGNMENT_TARGET_TYPE_DEVICE_GROUP :
9492 _ , err := h .store .Queries ().GetDeviceGroupByID (ctx , req .Msg .TargetId )
9593 if err != nil {
96- if errors . Is (err , pgx . ErrNoRows ) {
94+ if store . IsNotFound (err ) {
9795 return nil , apiErrorCtx (ctx , ErrDeviceGroupNotFound , connect .CodeNotFound , "device group not found" )
9896 }
9997 return nil , apiErrorCtx (ctx , ErrInternal , connect .CodeInternal , "failed to get device group" )
10098 }
10199 case pm .AssignmentTargetType_ASSIGNMENT_TARGET_TYPE_USER :
102100 _ , err := h .store .Queries ().GetUserByID (ctx , req .Msg .TargetId )
103101 if err != nil {
104- if errors . Is (err , pgx . ErrNoRows ) {
102+ if store . IsNotFound (err ) {
105103 return nil , apiErrorCtx (ctx , ErrUserNotFound , connect .CodeNotFound , "user not found" )
106104 }
107105 return nil , apiErrorCtx (ctx , ErrInternal , connect .CodeInternal , "failed to get user" )
108106 }
109107 case pm .AssignmentTargetType_ASSIGNMENT_TARGET_TYPE_USER_GROUP :
110108 _ , err := h .store .Queries ().GetUserGroupByID (ctx , req .Msg .TargetId )
111109 if err != nil {
112- if errors . Is (err , pgx . ErrNoRows ) {
110+ if store . IsNotFound (err ) {
113111 return nil , apiErrorCtx (ctx , ErrUserGroupNotFound , connect .CodeNotFound , "user group not found" )
114112 }
115113 return nil , apiErrorCtx (ctx , ErrInternal , connect .CodeInternal , "failed to get user group" )
@@ -135,7 +133,7 @@ func (h *AssignmentHandler) CreateAssignment(ctx context.Context, req *connect.R
135133 return connect .NewResponse (& pm.CreateAssignmentResponse {
136134 Assignment : h .assignmentToProto (existingAssignment ),
137135 }), nil
138- } else if ! errors . Is (err , pgx . ErrNoRows ) {
136+ } else if ! store . IsNotFound (err ) {
139137 return nil , apiErrorCtx (ctx , ErrInternal , connect .CodeInternal , "failed to check existing assignment" )
140138 }
141139
0 commit comments