louislam/dockge · error · Error

Data must be an object

Error message

Data must be an object

What it means

The 'addAgent' socket event expects a single requestData object carrying url, username, password, and name. Because the argument is unknown, the handler checks typeof(requestData) === 'object' and throws this Error otherwise, preventing destructure/type errors and undefined-field calls into instanceManager.test/add.

Source

Thrown at backend/socket-handlers/manage-agent-socket-handler.ts:17

import { SocketHandler } from "../socket-handler.js";
import { DockgeServer } from "../dockge-server";
import { log } from "../log";
import { callbackError, callbackResult, checkLogin, DockgeSocket } from "../util-server";
import { LooseObject } from "../../common/util-common";

export class ManageAgentSocketHandler extends SocketHandler {

    create(socket : DockgeSocket, server : DockgeServer) {
        // addAgent
        socket.on("addAgent", async (requestData : unknown, callback : unknown) => {
            try {
                log.debug("manage-agent-socket-handler", "addAgent");
                checkLogin(socket);

                if (typeof(requestData) !== "object") {
                    throw new Error("Data must be an object");
                }

                let data = requestData as LooseObject;
                let manager = socket.instanceManager;
                await manager.test(data.url, data.username, data.password);
                await manager.add(data.url, data.username, data.password, data.name);

                // connect to the agent
                manager.connect(data.url, data.username, data.password);

                // Refresh another sockets
                // It is a bit difficult to control another browser sessions to connect/disconnect agents, so force them to refresh the page will be easier.
                server.disconnectAllSocketClients(undefined, socket.id);
                manager.sendAgentList();

                callbackResult({
                    ok: true,
                    msg: "agentAddedSuccessfully",

View on GitHub (pinned to f809ae192b)

Solutions

  1. Emit a single object: { url, username, password, name }.
  2. Guard with typeof data === 'object' && data !== null before emitting.
  3. Ensure all fields (url, username, password, name) are populated strings.

Example fix

// before
socket.emit('addAgent', 'http://agent:5001', cb);
// after
socket.emit('addAgent', { url: 'http://agent:5001', username: 'user', password: 'pass1', name: 'agent1' }, cb);
Defensive patterns

Strategy: type-guard

Validate before calling

function isAddAgentPayload(v) {
    return typeof v === 'object' && v !== null
        && typeof v.url === 'string'
        && typeof v.username === 'string'
        && typeof v.password === 'string';
}

Type guard

function isObject(v) { return typeof v === 'object' && v !== null; }

Try / catch

if (!isAddAgentPayload(data)) {
    throw new Error('Data must be an object');
}
socket.emit('addAgent', data, cb);

Prevention

When it happens

Trigger: Emitting 'addAgent' with null (typeof null === 'object' but the check is per-source; a null/undefined/primitive such as a bare URL string or number) instead of a request object.

Common situations: Scripts emitting only the URL string instead of the object; unserialized payload arriving undefined; frontend sending separate positional args rather than one object.

Understand the failure class

Background: "Wrong argument type", "must be a string", "expected Array or Prism::Scope": TypeError and ArgumentError when a library receives a value of the wrong type — this error's family across 28 libraries.

Related errors


AI-assisted analysis of louislam/dockge@f809ae192b (2026-08-31). Data as JSON: /api/errors/a22bf5b9ea2c8b62. Report an issue: GitHub.