Search code examples
resthttperror-handlinghttpresponse

Indicate failure in HTTP response from web API that interacts with machine vision cameras (or any external device)


My server is connected to several machine vision cameras and I am creating a REST API that performs some operations using them, such as detecting a visible QR code, calibrating white balance and capturing images.

Some of these operations can fail (for one or more cameras) and I would like to know the most appropriate HTTP response to use to report this to the client.

For example, a request to read a QR code from a number of cameras:

POST /api/camera/readQrCode
{...JSON body identifies the cameras to use...}

(n.b. POST is because it updates server state with details of what was read)

This request can fail for a number of reasons, including:

  1. A requested camera being unavailable (404 Not Found seems sensible here)
  2. Any cameras weren't able to capture an image
  3. Any cameras couldn't detect a QR code

It's specifically #2 and #3 that I'm wondering about the best way to handle. The best I've come up with so far is to return a 200 OK response and send a JSON response that describes the overall success and individual statuses of each camera, something like:

{
  success: false,
  cameraResults: [
    {"cameraId": "123", success: true},
    {"cameraId": "456", success: false},
  ]
}

It just didn't feel necessarily right to use a 2xx code, nor are #2 or #3 client errors (4xx) and I can't see any suitable 5xx codes. I'd be interested to hear what others would consider conventional here.

Thank you


Solution

  • In the end I went with the solution I proposed at the end, which is good enough right now:

    200 OK response and a result object of the form

    {
       success: false,
       cameraResults: [
          {"cameraId": "123", success: true},
          {"cameraId": "456", success: false},
       ]
    }