Skip to content

Commit

Permalink
[BACK-3245] Respond with appropriate error code when unable to retrieve
Browse files Browse the repository at this point in the history
contents of a device log.
  • Loading branch information
lostlevels committed Nov 2, 2024
1 parent 6c92dd3 commit d1d4795
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions blob/service/api/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ func (r *Router) GetDeviceLogsContent(res rest.ResponseWriter, req *rest.Request

content, err := blobClient.GetDeviceLogsContent(req.Context(), *deviceLogMetadata.ID)
if err != nil {
responder.Error(http.StatusInternalServerError, err)
responder.CodedError(err)
return
}
if content == nil || content.Body == nil {
responder.Error(http.StatusNotFound, request.ErrorResourceNotFoundWithID(deviceLogID))
responder.CodedError(request.ErrorResourceNotFoundWithID(deviceLogID))
return
}
defer content.Body.Close()
Expand Down
27 changes: 27 additions & 0 deletions blob/service/api/v1/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,20 @@ var _ = Describe("V1", func() {
errorsTest.ExpectErrorJSON(request.ErrorResourceNotFoundWithID(id), res.WriteInputs[0])
})

It("responds with not found error when the client returns a blob but the blob has no content", func() {
// This has happened when blob creation succeeded but the request context canceled before the contents finished uploading to S3 so there is a "stray" blob w/o content
deviceLogsBlob := blobTest.RandomDeviceLogsBlob()
client.GetDeviceLogsBlobOutputs = []blobTest.GetDeviceLogsBlobOutput{{Blob: deviceLogsBlob}}
client.GetDeviceLogsContentOutputs = []blobTest.GetDeviceLogsContentOutput{{Error: request.ErrorResourceNotFoundWithID(*deviceLogsBlob.ID)}}
res.WriteOutputs = []testRest.WriteOutput{{BytesWritten: 0, Error: nil}}

handlerFunc(res, req)
Expect(res.WriteHeaderInputs).To(Equal([]int{http.StatusNotFound}))
Expect(res.HeaderOutput).To(Equal(&http.Header{"Content-Type": []string{"application/json; charset=utf-8"}}))
Expect(res.WriteInputs).To(HaveLen(1))
errorsTest.ExpectErrorJSON(request.ErrorResourceNotFoundWithID(*deviceLogsBlob.ID), res.WriteInputs[0])
})

It("responds successfully with headers", func() {
deviceLogsBlob := blobTest.RandomDeviceLogsBlob()
content := blob.NewDeviceLogsContent()
Expand Down Expand Up @@ -607,6 +621,19 @@ var _ = Describe("V1", func() {
errorsTest.ExpectErrorJSON(request.ErrorResourceNotFoundWithID(id), res.WriteInputs[0])
})

It("responds with not found error when the client returns a blob but the blob has no content", func() {
deviceLogsBlob := blobTest.RandomDeviceLogsBlob()
deviceLogsBlob.UserID = pointer.FromString(userID)
client.GetDeviceLogsBlobOutputs = []blobTest.GetDeviceLogsBlobOutput{{Blob: deviceLogsBlob}}
client.GetDeviceLogsContentOutputs = []blobTest.GetDeviceLogsContentOutput{{Error: request.ErrorResourceNotFoundWithID(*deviceLogsBlob.ID)}}
res.WriteOutputs = []testRest.WriteOutput{{BytesWritten: 0, Error: nil}}

handlerFunc(res, req)
Expect(res.WriteHeaderInputs).To(Equal([]int{http.StatusNotFound}))
Expect(res.HeaderOutput).To(Equal(&http.Header{"Content-Type": []string{"application/json; charset=utf-8"}}))
Expect(res.WriteInputs).To(HaveLen(1))
errorsTest.ExpectErrorJSON(request.ErrorResourceNotFoundWithID(*deviceLogsBlob.ID), res.WriteInputs[0])
})
It("responds successfully with headers for user's own logs content", func() {
deviceLogsBlob := blobTest.RandomDeviceLogsBlob()
deviceLogsBlob.UserID = pointer.FromString(userID)
Expand Down
4 changes: 4 additions & 0 deletions request/responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func (r *Responder) Error(statusCode int, err error, mutators ...ResponseMutator
}
}

func (r *Responder) CodedError(err error, mutators ...ResponseMutator) {
r.Error(StatusCodeForError(err), err, mutators...)
}

func (r *Responder) InternalServerError(err error, mutators ...ResponseMutator) {
if err == nil {
err = ErrorInternalServerError(errors.New("error is missing"))
Expand Down

0 comments on commit d1d4795

Please sign in to comment.