Skip to content

Commit d1d4795

Browse files
committed
[BACK-3245] Respond with appropriate error code when unable to retrieve
contents of a device log.
1 parent 6c92dd3 commit d1d4795

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

blob/service/api/v1/v1.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ func (r *Router) GetDeviceLogsContent(res rest.ResponseWriter, req *rest.Request
281281

282282
content, err := blobClient.GetDeviceLogsContent(req.Context(), *deviceLogMetadata.ID)
283283
if err != nil {
284-
responder.Error(http.StatusInternalServerError, err)
284+
responder.CodedError(err)
285285
return
286286
}
287287
if content == nil || content.Body == nil {
288-
responder.Error(http.StatusNotFound, request.ErrorResourceNotFoundWithID(deviceLogID))
288+
responder.CodedError(request.ErrorResourceNotFoundWithID(deviceLogID))
289289
return
290290
}
291291
defer content.Body.Close()

blob/service/api/v1/v1_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,20 @@ var _ = Describe("V1", func() {
521521
errorsTest.ExpectErrorJSON(request.ErrorResourceNotFoundWithID(id), res.WriteInputs[0])
522522
})
523523

524+
It("responds with not found error when the client returns a blob but the blob has no content", func() {
525+
// 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
526+
deviceLogsBlob := blobTest.RandomDeviceLogsBlob()
527+
client.GetDeviceLogsBlobOutputs = []blobTest.GetDeviceLogsBlobOutput{{Blob: deviceLogsBlob}}
528+
client.GetDeviceLogsContentOutputs = []blobTest.GetDeviceLogsContentOutput{{Error: request.ErrorResourceNotFoundWithID(*deviceLogsBlob.ID)}}
529+
res.WriteOutputs = []testRest.WriteOutput{{BytesWritten: 0, Error: nil}}
530+
531+
handlerFunc(res, req)
532+
Expect(res.WriteHeaderInputs).To(Equal([]int{http.StatusNotFound}))
533+
Expect(res.HeaderOutput).To(Equal(&http.Header{"Content-Type": []string{"application/json; charset=utf-8"}}))
534+
Expect(res.WriteInputs).To(HaveLen(1))
535+
errorsTest.ExpectErrorJSON(request.ErrorResourceNotFoundWithID(*deviceLogsBlob.ID), res.WriteInputs[0])
536+
})
537+
524538
It("responds successfully with headers", func() {
525539
deviceLogsBlob := blobTest.RandomDeviceLogsBlob()
526540
content := blob.NewDeviceLogsContent()
@@ -607,6 +621,19 @@ var _ = Describe("V1", func() {
607621
errorsTest.ExpectErrorJSON(request.ErrorResourceNotFoundWithID(id), res.WriteInputs[0])
608622
})
609623

624+
It("responds with not found error when the client returns a blob but the blob has no content", func() {
625+
deviceLogsBlob := blobTest.RandomDeviceLogsBlob()
626+
deviceLogsBlob.UserID = pointer.FromString(userID)
627+
client.GetDeviceLogsBlobOutputs = []blobTest.GetDeviceLogsBlobOutput{{Blob: deviceLogsBlob}}
628+
client.GetDeviceLogsContentOutputs = []blobTest.GetDeviceLogsContentOutput{{Error: request.ErrorResourceNotFoundWithID(*deviceLogsBlob.ID)}}
629+
res.WriteOutputs = []testRest.WriteOutput{{BytesWritten: 0, Error: nil}}
630+
631+
handlerFunc(res, req)
632+
Expect(res.WriteHeaderInputs).To(Equal([]int{http.StatusNotFound}))
633+
Expect(res.HeaderOutput).To(Equal(&http.Header{"Content-Type": []string{"application/json; charset=utf-8"}}))
634+
Expect(res.WriteInputs).To(HaveLen(1))
635+
errorsTest.ExpectErrorJSON(request.ErrorResourceNotFoundWithID(*deviceLogsBlob.ID), res.WriteInputs[0])
636+
})
610637
It("responds successfully with headers for user's own logs content", func() {
611638
deviceLogsBlob := blobTest.RandomDeviceLogsBlob()
612639
deviceLogsBlob.UserID = pointer.FromString(userID)

request/responder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ func (r *Responder) Error(statusCode int, err error, mutators ...ResponseMutator
119119
}
120120
}
121121

122+
func (r *Responder) CodedError(err error, mutators ...ResponseMutator) {
123+
r.Error(StatusCodeForError(err), err, mutators...)
124+
}
125+
122126
func (r *Responder) InternalServerError(err error, mutators ...ResponseMutator) {
123127
if err == nil {
124128
err = ErrorInternalServerError(errors.New("error is missing"))

0 commit comments

Comments
 (0)