Skip to content

Commit

Permalink
REST API improve DDF upload error reporting (#7791)
Browse files Browse the repository at this point in the history
Most errors in the returned JSON array were empty. The response now tells if there was invalid DDF bundle data or an internal error.
  • Loading branch information
manup authored May 27, 2024
1 parent 4ddd08a commit 4f55da6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions rest_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define ERR_INVALID_VALUE 7
#define ERR_PARAMETER_NOT_MODIFIABLE 8
#define ERR_TOO_MANY_ITEMS 11
#define ERR_INVALID_DDF_BUNDLE 12
#define ERR_DUPLICATE_EXIST 100 // de extension
#define ERR_NOT_ALLOWED_SENSOR_TYPE 501
#define ERR_SENSOR_LIST_FULL 502
Expand Down
18 changes: 17 additions & 1 deletion rest_ddf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ int REST_DDF_PostBundles(const ApiRequest &req, ApiResponse &rsp)
{
ScratchMemWaypoint swp;

// general error response
const QVariantMap errInvalidData = errorToMap(ERR_INVALID_DDF_BUNDLE, "/ddf/bundles", "body contains invalid DDF bundle(s) data");
const QVariantMap errServiceNotAvailable = errorToMap(ERR_INTERNAL_ERROR, "/ddf/bundles", "Internal error, can't process request");

// TEST call
// curl -F 'data=@./starkvind_air_purifier_toolbox.ddf' 127.0.0.1:8090/api/12345/ddf/bundles

Expand All @@ -425,18 +429,21 @@ int REST_DDF_PostBundles(const ApiRequest &req, ApiResponse &rsp)
if (U_sstream_starts_with(&ss, "multipart/form-data") == 0)
{
rsp.httpStatus = HttpStatusBadRequest;
rsp.list.append(errInvalidData);
return REQ_READY_SEND;
}

if (U_sstream_find(&ss, "boundary=") == 0) // no boundary marker?
{
rsp.httpStatus = HttpStatusBadRequest;
rsp.list.append(errInvalidData);
return REQ_READY_SEND;
}

if (U_sstream_find(&ss, "=") == 0)
{
rsp.httpStatus = HttpStatusBadRequest;
rsp.list.append(errInvalidData);
return REQ_READY_SEND;
}
ss.pos++;
Expand All @@ -447,6 +454,7 @@ int REST_DDF_PostBundles(const ApiRequest &req, ApiResponse &rsp)
if (!boundary)
{
rsp.httpStatus = HttpStatusServiceUnavailable;
rsp.list.append(errServiceNotAvailable);
return REQ_READY_SEND;
}
U_memcpy(boundary, &ss.str[ss.pos], boundaryLen);
Expand All @@ -456,6 +464,7 @@ int REST_DDF_PostBundles(const ApiRequest &req, ApiResponse &rsp)
if (MAX_BUNDLE_SIZE < dataSize)
{
rsp.httpStatus = HttpStatusBadRequest;
rsp.list.append(errInvalidData);
return REQ_READY_SEND;
}

Expand All @@ -464,6 +473,7 @@ int REST_DDF_PostBundles(const ApiRequest &req, ApiResponse &rsp)
if (!data)
{
rsp.httpStatus = HttpStatusServiceUnavailable;
rsp.list.append(errServiceNotAvailable);
return REQ_READY_SEND;
}

Expand All @@ -480,6 +490,7 @@ int REST_DDF_PostBundles(const ApiRequest &req, ApiResponse &rsp)
if (U_sstream_find(&ss, boundary) == 0) // there might be a preample before the first boundary
{
rsp.httpStatus = HttpStatusBadRequest;
rsp.list.append(errInvalidData);
return REQ_READY_SEND;
}

Expand All @@ -500,6 +511,7 @@ int REST_DDF_PostBundles(const ApiRequest &req, ApiResponse &rsp)
if (U_sstream_find(&ss, boundary) == 0)
{
rsp.httpStatus = HttpStatusBadRequest;
rsp.list.append(errInvalidData);
return REQ_READY_SEND;
}

Expand Down Expand Up @@ -533,13 +545,15 @@ int REST_DDF_PostBundles(const ApiRequest &req, ApiResponse &rsp)
if (!bundleHash || !bundleHashStr)
{
rsp.httpStatus = HttpStatusServiceUnavailable;
rsp.list.append(errServiceNotAvailable);
return REQ_READY_SEND;
}


if (IsValidDDFBundle(&bs, bundleHash) == 0)
{
rsp.httpStatus = HttpStatusBadRequest;
rsp.list.append(errInvalidData);
return REQ_READY_SEND;
}

Expand All @@ -554,6 +568,7 @@ int REST_DDF_PostBundles(const ApiRequest &req, ApiResponse &rsp)
if (!bundlePath)
{
rsp.httpStatus = HttpStatusServiceUnavailable;
rsp.list.append(errServiceNotAvailable);
return REQ_READY_SEND;
}

Expand All @@ -579,7 +594,8 @@ int REST_DDF_PostBundles(const ApiRequest &req, ApiResponse &rsp)
FS_CloseFile(&fp);
if (n != bs.size)
{
rsp.httpStatus = HttpStatusBadRequest; // TODO different error
rsp.httpStatus = HttpStatusServiceUnavailable;
rsp.list.append(errServiceNotAvailable);
return REQ_READY_SEND;
}

Expand Down

0 comments on commit 4f55da6

Please sign in to comment.