Mintplex-Labs/anything-llm · error

Failed to serve image

Error message

Failed to serve image

What it means

Catch-all HTTP 500 from the generated-image serve handler. Any throw outside the inner readFile guard — userFromSession failures that slip through, an exception from findFileSource's database query, response.send errors, header issues — is logged server-side as '[agentFileServer] Image serve error: <message>' and returned to the client as this generic message. The real cause only exists in the server console.

Source

Thrown at server/endpoints/agentFileServer.js:135

          return response
            .status(404)
            .json({ error: "Image not found or access denied" });

        const imagePath = path.resolve(generatedImagesPath, filename);
        let imageBuffer;
        try {
          imageBuffer = await fs.promises.readFile(imagePath);
        } catch {
          return response
            .status(404)
            .json({ error: "Image not found in storage" });
        }

        response.setHeader("Content-Type", "image/png");
        return response.send(imageBuffer);
      } catch (error) {
        console.error("[agentFileServer] Image serve error:", error.message);
        return response.status(500).json({ error: "Failed to serve image" });
      }
    }
  );
}

/**
 * Locates the source record (a workspace chat or a scheduled job run) that
 * references the given storage filename, and confirms the requester has access.
 *
 * Search order:
 *   1. Workspace chats the user can access (per multi-user permissions).
 *   2. Scheduled job runs — single-user only, so no per-user access check.
 *
 * @param {string} storageFilename
 * @param {{ user: object|null, isMultiUser: boolean }} ctx
 * @returns {Promise<{workspaceId: number|null, displayFilename: string}|null>}
 */
async function findFileSource(storageFilename, { user, isMultiUser }) {

View on GitHub (pinned to 3aec848f28)

Solutions

  1. Check the server console for '[agentFileServer] Image serve error:' at the request time — it carries the underlying message
  2. Reload/retry once; aborted-connection races and transient DB errors often clear
  3. If every image fails, test the DB queries behind findFileSource directly — a systemic 500 points at the authorization lookup, not the files
  4. Log the full error object temporarily to obtain a stack for the specific fault

Example fix

// before: message-only logging loses stacks
console.error('[agentFileServer] Image serve error:', error.message);

// after: full error while debugging
console.error('[agentFileServer] Image serve error:', error);
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const res = await fetch(imgUrl, {credentials: 'include'});
  if (res.status === 500) {
    // generic by design — check the '[agentFileServer] Image serve error:' server log
    // for the real cause; a single retry covers aborted-connection races
    return await fetch(imgUrl, {credentials: 'include'});
  }
} catch (e) {
  // network-level failure reaching the server
}

Prevention

When it happens

Trigger: A database error inside findFileSource (the workspace-chat lookup) while authorizing the image; response.send failing after a client abort; an unexpected throw from userFromSession for a malformed session cookie; fs errors other than ENOENT surfacing from the surrounding logic.

Common situations: Broken <img> tags in production chats with no client-side detail; intermittent failures tied to DB load; post-upgrade schema mismatches in the chat tables used by findFileSource.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@3aec848f28 (2026-08-18). Data as JSON: /api/errors/a34c25cb27153c80. Report an issue: GitHub.