diff --git a/server/cellsv2.go b/server/cellsv2.go index 92e5d1f866..b1e9672dde 100644 --- a/server/cellsv2.go +++ b/server/cellsv2.go @@ -127,6 +127,11 @@ func (s *Service) CellIDV2(w http.ResponseWriter, r *http.Request) { return } cell, err := s.Store.Cells(ctx).FindCellByID(ctx, req.CellID) + if err == platform.ErrCellNotFound { + Error(w, http.StatusNotFound, err.Error(), s.Logger) + return + } + if err != nil { Error(w, http.StatusInternalServerError, fmt.Sprintf("Error loading cell: %v", err), s.Logger) return @@ -159,7 +164,13 @@ func (s *Service) RemoveCellV2(w http.ResponseWriter, r *http.Request) { Error(w, http.StatusBadRequest, err.Error(), s.Logger) return } - if err := s.Store.Cells(ctx).DeleteCell(ctx, req.CellID); err != nil { + err = s.Store.Cells(ctx).DeleteCell(ctx, req.CellID) + if err == platform.ErrCellNotFound { + Error(w, http.StatusNotFound, err.Error(), s.Logger) + return + } + + if err != nil { Error(w, http.StatusInternalServerError, fmt.Sprintf("Error deleting cell: %v", err), s.Logger) return } @@ -188,6 +199,11 @@ func (s *Service) UpdateCellV2(w http.ResponseWriter, r *http.Request) { return } cell, err := s.Store.Cells(ctx).UpdateCell(ctx, req.CellID, req.Upd) + if err == platform.ErrCellNotFound { + Error(w, http.StatusNotFound, err.Error(), s.Logger) + return + } + if err != nil { Error(w, http.StatusInternalServerError, fmt.Sprintf("Error updating cell: %v", err), s.Logger) return diff --git a/server/cellvs2_test.go b/server/cellvs2_test.go index ed79695410..297fa34f3c 100644 --- a/server/cellvs2_test.go +++ b/server/cellvs2_test.go @@ -112,6 +112,28 @@ func TestService_CellsV2(t *testing.T) { } } ] +}`, + }, + }, + { + name: "get all cells when there are none", + fields: fields{ + &mocks.CellService{ + FindCellsF: func(ctx context.Context, filter platform.CellFilter) ([]*platform.Cell, int, error) { + return []*platform.Cell{}, 0, nil + }, + }, + }, + args: args{}, + wants: wants{ + statusCode: http.StatusOK, + contentType: "application/json", + body: ` +{ + "links": { + "self": "/chronograf/v2/cells" + }, + "cells": [] }`, }, }, @@ -215,6 +237,24 @@ func TestService_CellIDV2(t *testing.T) { `, }, }, + { + name: "not found", + fields: fields{ + &mocks.CellService{ + FindCellByIDF: func(ctx context.Context, id platform.ID) (*platform.Cell, error) { + return nil, platform.ErrCellNotFound + }, + }, + }, + args: args{ + id: "2", + }, + wants: wants{ + statusCode: http.StatusNotFound, + contentType: "application/json", + body: `{"code":404,"message":"cell not found"}`, + }, + }, } for _, tt := range tests { @@ -412,6 +452,24 @@ func TestService_RemoveCellV2(t *testing.T) { statusCode: http.StatusNoContent, }, }, + { + name: "cell not found", + fields: fields{ + &mocks.CellService{ + DeleteCellF: func(ctx context.Context, id platform.ID) error { + return platform.ErrCellNotFound + }, + }, + }, + args: args{ + id: "2", + }, + wants: wants{ + statusCode: http.StatusNotFound, + contentType: "application/json", + body: `{"code":404,"message":"cell not found"}`, + }, + }, } for _, tt := range tests { @@ -569,6 +627,25 @@ func TestService_UpdateCellV2(t *testing.T) { body: `{"code":400,"message":"expected at least one attribute to be updated"}`, }, }, + { + name: "cell not found", + fields: fields{ + &mocks.CellService{ + UpdateCellF: func(ctx context.Context, id platform.ID, upd platform.CellUpdate) (*platform.Cell, error) { + return nil, platform.ErrCellNotFound + }, + }, + }, + args: args{ + id: "2", + name: "hello", + }, + wants: wants{ + statusCode: http.StatusNotFound, + contentType: "application/json", + body: `{"code":404,"message":"cell not found"}`, + }, + }, } for _, tt := range tests {