Skip to content

GET /api/requests/:id returns 500 FetchError for any request that has a selected download #261

Closed

Description

@tikibozo

Summary

GET /api/requests/[id] throws while serializing its response and returns
500 {"error":"FetchError","message":"Failed to fetch request"} for essentially every request that
has ever been downloaded. The endpoint only works for requests that have no selected
DownloadHistory row
— i.e. brand-new or never-grabbed ones.

The cause is a BigInt in the response payload: DownloadHistory.torrentSizeBytes is
BigInt? in the Prisma schema, and JSON.stringify (used by NextResponse.json) throws
TypeError: Do not know how to serialize a BigInt.

Reproduce

  1. Let any request complete a download (so it gets a DownloadHistory row with selected = true).
  2. GET /api/requests/<that id> with a valid session JWT.
  3. → 500 {"error":"FetchError","message":"Failed to fetch request"}

For contrast, a request with no download history returns 200 normally.

Observed on my instance — same auth, same code path, back to back:

bf21d9e3-…  (0 selected DownloadHistory rows) -> {"success":true,"request":{…}}
f3383e9f-…  (1 selected DownloadHistory row)  -> {"error":"FetchError","message":"Failed to fetch request"}

Server log

The real error is only visible in the container log; the client just gets the generic FetchError:

[ERROR] [API.RequestById] Failed to get request
  "error": "Do not know how to serialize a BigInt"

Cause

src/app/api/requests/[id]/route.ts — the GET handler includes downloadHistory in the Prisma
query (line ~48) and then hands the record straight to NextResponse.json (line ~74):

const requestRecord = await prisma.request.findFirst({
  where: { id, deletedAt: null },
  include: {
    audiobook: true,
    user: { select: { id: true, plexUsername: true } },
    downloadHistory: { where: { selected: true }, take: 1 },   // <-- carries torrentSizeBytes: BigInt
    jobs: { orderBy: { createdAt: 'desc' }, take: 5 },
  },
});
…
return NextResponse.json({ success: true, request: requestRecord });   // <-- throws on the BigInt

prisma/schema.prisma:

model DownloadHistory {
  torrentSizeBytes BigInt?  @map("torrent_size_bytes")   // line ~318
}

The throw is swallowed by the handler's own catch, which is why it surfaces as a generic
FetchError rather than the underlying TypeError.

Two other BigInt columns exist and would hit the same wall wherever they reach a JSON response:
PlexLibrary.duration (line ~135) and Audiobook.fileSizeBytes (line ~191).

Suggested fix

Convert before serializing. Any of these work; pick whichever matches project style:

a) Local, minimal — map the field at the call site:

return NextResponse.json({
  success: true,
  request: {
    ...requestRecord,
    downloadHistory: requestRecord.downloadHistory.map(h => ({
      ...h,
      torrentSizeBytes: h.torrentSizeBytes?.toString() ?? null,
    })),
  },
});

(Note .toString() vs Number() — sizes are well under Number.MAX_SAFE_INTEGER in practice, but a
string is lossless and matches how Prisma serializes BigInt elsewhere. Either is fine; just be
consistent, since it is a response-shape change for API consumers.)

b) Global — a Prisma client extension or a superjson-style serializer so no route has to
remember. Given three BigInt columns and more likely to come, this is probably the durable fix.

c) Schema — if the value never exceeds 2^53, Int/Float sidesteps the problem entirely, though
that is a migration.

Whichever route is taken, it would also help to log the underlying error message in the catch (it
already does) and return something more specific than FetchError, since a serialization bug and a
genuine DB failure are currently indistinguishable to the caller.

Impact

Anything that reads a single request by id is affected. In my case it is an automation agent that
triages the request queue — it can list requests and read the blocklist fine, but cannot fetch the
per-request detail view (download history + recent jobs) that would be the natural way to diagnose a
failure. It is working around this by reconstructing state from GET /api/requests plus

Version

Version: v1.2.1 ({"version":"v1.2.1","commit":"96aa01a"}). The route file is byte-identical on
main as of commit af0a72b, so this is not fixed there.
Backend: Audiobookshelf mode, Postgres (bundled unified container).
GET /api/admin/blocklist/by-request/:id.

I have not checked whether the web UI hits this endpoint; if it does, the request detail view is
presumably broken in the same way.

Happy to open a PR if you would like — just say which of the three approaches you prefer.

Activity

  1. Eyji commented on Oct 4, 2026

    @Eyji
    Contributor

    Still there on release 1.2.3

  2. added
    dev-completedev work is completed, being tested or awaiting release
    on Oct 27, 2026
  3. kikootwo commented on Oct 27, 2026

    @kikootwo
    Owner

    @tikibozo fixed in #462. GET /api/requests/:id returns the request again when it has a selected download, API token calls included. The download's size field was what broke the response. It'll be in the next release. Thanks for the report!

  4. added a commit that references this issue on Nov 2, 2026
    8ee4a84
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ReleasedbugSomething isn't workingdev-completedev work is completed, being tested or awaiting release

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions