Skip to content

Deleting a post without a database throws TypeError instead of a meaningful error #904

Description

@rmdes

Deleting a post without a database configured fails with an unhandled TypeError rather than a meaningful error.

MongoDB is documented as optional, and as required for “viewing, editing, deleting and restoring previously published posts”. So needing a database here is expected — the problem is what happens when it is absent.

Cause

packages/endpoint-micropub/lib/post-data.js, in read():

const postsCollection = application?.collections?.get("posts");

const data = await postsCollection.findOne(query);

The lookup uses optional chaining, acknowledging that collections may be absent, and the next line then calls findOne() on it unguarded. With no database, postsCollection is undefined and this throws.

create(), nineteen lines above, already guards the same lookup:

const postsCollection = application?.collections?.get("posts");
if (postsCollection) {

which is why publishing works without a database and reading does not.

update(), delete() and undelete() each call read() before touching the collection, so one missing guard affects four operations.

Reproducing

Run Indiekit with no MONGO_URL, publish a post through the Micropub API, then delete it:

curl -i -X POST "$INDIEKIT/micropub" \
  -H "Authorization: Bearer $TOKEN" \
  -d "action=delete" --data-urlencode "url=$POST_URL"

Publishing returns 202. The delete throws instead of returning a useful response, and the post stays published — so the natural reaction is to try again.

Suggested fix

Guard the lookup in read() and throw a documented error. Because the other three operations route through read(), that one guard covers all of them:

if (!postsCollection) {
  throw IndiekitError.notImplemented(
    "Reading, updating, deleting and restoring posts requires a database",
  );
}

PR follows, with a regression test. Found while building a starter template that runs Indiekit without a database.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions