feat(server): return special error if cell not found

pull/10616/head
Michael Desa 2018-07-16 12:55:52 -04:00
parent 0a85e016e6
commit 1a94dc6b1b
2 changed files with 94 additions and 1 deletions

View File

@ -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

View File

@ -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 {