Skip to content

Commit

Permalink
refactor(webserver): Response cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Jun 16, 2022
1 parent 6e2d2e1 commit 839310a
Show file tree
Hide file tree
Showing 27 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion backend/lib/robots/MiioValetudoRobot.js
Expand Up @@ -302,7 +302,7 @@ class MiioValetudoRobot extends ValetudoRobot {
.update(JSON.stringify(info))
.digest("base64");

res.status(200).send({
res.json({
"info": info,
"timestamp": req.query["timestamp"],
"sign": signature
Expand Down
2 changes: 1 addition & 1 deletion backend/lib/webserver/NetworkAdvertisementManagerRouter.js
Expand Up @@ -31,7 +31,7 @@ class NetworkAdvertisementManagerRouter {

res.sendStatus(200);
} else {
res.status(400).send("invalid request body");
res.sendStatus(400);
}
});

Expand Down
4 changes: 2 additions & 2 deletions backend/lib/webserver/ValetudoEventRouter.js
Expand Up @@ -40,12 +40,12 @@ class ValetudoEventRouter {
try {
await this.valetudoEventStore.interact(event, req.body.interaction);
} catch (e) {
return res.status(400).send("Failed to interact with event: " + e?.message);
return res.status(500).send("Failed to interact with event: " + e?.message);
}

res.json(event);
} else {
res.status(400).send("Missing or invalid request body");
res.sendStatus(400);
}
} else {
res.sendStatus(404);
Expand Down
4 changes: 2 additions & 2 deletions backend/lib/webserver/ValetudoRouter.js
Expand Up @@ -117,15 +117,15 @@ class ValetudoRouter {


if (!options.password && (webserverConfig.basicAuth.enabled === false && options.enabled === true)) {
res.status(400).send("Missing password for basic auth enable. Don't lock yourself out!");
res.sendStatus(400);
} else {
webserverConfig.basicAuth = options;

this.config.set("webserver", webserverConfig);
res.sendStatus(200);
}
} else {
res.status(400).send("bad request body");
res.sendStatus(400);
}

});
Expand Down
2 changes: 1 addition & 1 deletion backend/lib/webserver/WebServer.js
Expand Up @@ -100,7 +100,7 @@ class WebServer {
// even if it is not being validated by the schema
if (req.method === "PUT" || req.method === "POST") {
if (Tools.IS_EMPTY_OBJECT_OR_UNDEFINED_OR_NULL(req.body)) {
res.status(400).send("Missing or empty body");
res.sendStatus(400);
} else {
next();
}
Expand Down
Expand Up @@ -11,7 +11,7 @@ class AutoEmptyDockManualTriggerCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Missing or invalid request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -28,7 +28,7 @@ class BasicControlCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -36,7 +36,7 @@ class CombinedVirtualRestrictionsCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Missing virtualWalls or restrictedZones property in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -27,7 +27,7 @@ class ConsumableMonitoringCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -21,7 +21,7 @@ class DoNotDisturbCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Missing parameters in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -14,7 +14,7 @@ class GoToLocationCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -11,7 +11,7 @@ class LocateCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -39,11 +39,11 @@ class ManualControlCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Missing movementCommand in request body");
res.sendStatus(400);
}
break;
default:
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -11,7 +11,7 @@ class MapResetCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -18,7 +18,7 @@ class MapSegmentEditCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid request");
res.sendStatus(400);
}
break;
case "split_segment":
Expand All @@ -35,11 +35,11 @@ class MapSegmentEditCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid request");
res.sendStatus(400);
}
break;
default:
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -17,10 +17,10 @@ class MapSegmentRenameCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid request");
res.sendStatus(400);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -36,10 +36,10 @@ class MapSegmentationCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Missing segment_ids");
res.sendStatus(400);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -16,7 +16,7 @@ class MapSnapshotCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -11,7 +11,7 @@ class MappingPassCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -32,7 +32,7 @@ class PendingMapChangeHandlingCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Missing parameters in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -19,7 +19,7 @@ class PresetSelectionCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Missing name in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -20,7 +20,7 @@ class QuirksCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Missing parameters in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -11,7 +11,7 @@ class SpeakerTestCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -14,11 +14,8 @@ class SpeakerVolumeControlCapabilityRouter extends CapabilityRouter {

this.router.put("/", this.validator, async (req, res) => {
if (req.body.action === "set_volume") {
if (req.body.value === undefined) {
res.status(400).send("Missing value for set_volume");
return;
} else if (typeof req.body.value !== "number") {
res.status(400).send("Value for set_volume must be a number");
if (typeof req.body.value !== "number") {
res.sendStatus(400);
return;
}

Expand All @@ -30,7 +27,7 @@ class SpeakerVolumeControlCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -26,7 +26,7 @@ class VoicePackManagementCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid request");
res.sendStatus(400);
}
});
}
Expand Down
Expand Up @@ -22,7 +22,7 @@ class ZoneCleaningCapabilityRouter extends CapabilityRouter {
this.sendErrorResponse(req, res, e);
}
} else {
res.status(400).send("Invalid action in request body");
res.sendStatus(400);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions backend/lib/webserver/doc/ValetudoEventRouter.openapi.json
Expand Up @@ -105,8 +105,8 @@
}
}
},
"400": {
"$ref": "#/components/responses/400"
"500": {
"$ref": "#/components/responses/500"
},
"404": {
"$ref": "#/components/responses/404"
Expand Down

0 comments on commit 839310a

Please sign in to comment.