Repository navigation
Description
Summary
fluree-db-server's error mapping blanket-matches several outer ApiError variants and maps each whole variant to 500 Internal Server Error / err:system/InternalError. Those variants contain client errors, so requests that are simply wrong — naming a ledger that does not exist, asking for a commit that has not happened yet, operating on a dropped resource — are reported to the client as server faults.
The primary case is ApiError::Ledger(_), which contains LedgerError::NotFound. This directly contradicts fluree-db-api's own status table, which maps that nested variant to 404. Probing turned up two further confirmed cases, listed below; they share the root cause and the fix shape, so they are tracked together here.
Individual routes work around it one at a time by testing ApiError::is_not_found() before wrapping, so the observable behavior today is inconsistent across the API surface: some routes 404 correctly, others 500 on identical input.
Root cause
Two arms in fluree-db-server/src/error.rs match the outer variant only:
fluree-db-server/src/error.rs:151—ServerError::Api(ApiError::Ledger(_)) => errors::INTERNALfluree-db-server/src/error.rs:249—ServerError::Api(ApiError::Ledger(_)) => StatusCode::INTERNAL_SERVER_ERROR
The API crate already classifies the nested variant correctly, and has done so independently of the server:
fluree-db-api/src/error.rs:467—ApiError::Ledger(fluree_db_ledger::LedgerError::NotFound(_)) => 404insideApiError::status_code()fluree-db-api/src/error.rs:367—ApiError::is_not_found(), documented as matching "bothApiError::NotFoundandApiError::Ledger(LedgerError::NotFound)"
So the server's two arms are the only place the distinction is lost, and ApiError::status_code() — the API's stated contract for "consumers like fluree-db-server" — is not consulted for this variant.
Evidence
Probed against a server with no ledgers (AdminAuthMode::None, in-process router, alias nosuch:main throughout). Confirmed mismatches:
| Request | Status | Body |
|---|---|---|
POST /v1/fluree/export/nosuch:main |
500 | {"error":"Ledger error: Ledger not found: nosuch:main","status":500,"@type":"err:system/InternalError"} |
POST /v1/fluree/insert (fluree-ledger: nosuch:main) |
500 | {"error":"Ledger error: Ledger not found: nosuch:main","status":500,"@type":"err:system/InternalError"} |
POST /v1/fluree/bm25/sync?t=999 (source head is t=1) |
500 | {"error":"Ledger error: Target t=999 is beyond current head t=1 for 'docs:main'","status":500,"@type":"err:system/InternalError"} |
POST /v1/fluree/bm25/sync on a dropped index |
500 | {"error":"Drop error: Cannot sync retracted graph source: docsearch:main","status":500,"@type":"err:system/InternalError"} |
The fourth row is a different outer variant — ApiError::Drop(_), blanket-mapped at fluree-db-server/src/error.rs:156 and :254 — but the same defect. sync_bm25_index deliberately detects a retracted graph source and refuses (fluree-db-api/src/graph_source/bm25.rs:851-855); asking to sync something the caller already dropped is a client error, best served as 409 Conflict (or 404), not a server fault. It is included here because it is the same blanket-arm pattern and wants the same review, not because it is the same enum.
The third row is a second LedgerError variant with the same root cause and the same fix shape. LedgerError::FutureTime { ledger_id, target_t, head_t } (fluree-db-ledger/src/error.rs:56-61) is unambiguously a client error — the caller asked to sync through a commit that does not exist yet — and the API detects it precisely, naming both the requested t and the actual head. The blanket ApiError::Ledger(_) arm then discards that and reports a server fault. It should be 400.
So the arms swallow at least three distinct client errors across two outer variants, and any future LedgerError variant that represents bad input will be swallowed the same way by default. That argues for enumerating the client-error variants explicitly rather than adding one-off arms as each is discovered.
Routes that answer correctly on the same input, because each translates the error itself:
| Request | Status | Body |
|---|---|---|
GET /v1/fluree/info/nosuch:main |
404 | {"error":"Not found: nosuch:main","status":404,"@type":"err:db/LedgerNotFound"} |
GET /v1/fluree/log/nosuch:main |
404 | {"error":"Not found: nosuch:main","status":404,"@type":"err:db/LedgerNotFound"} |
GET /v1/fluree/context/nosuch:main |
404 | {"error":"Not found: nosuch:main","status":404,"@type":"err:db/LedgerNotFound"} |
POST /v1/fluree/query/nosuch:main |
404 | {"error":"Not found: nosuch:main","status":404,"@type":"err:db/LedgerNotFound"} |
POST /v1/fluree/reindex |
404 | {"error":"Not found: Ledger not found: nosuch:main","status":404,"@type":"err:db/LedgerNotFound"} |
The clearest demonstration that this is a mapping defect rather than intended behavior is the insert path at fluree-db-server/src/routes/transact.rs:1515-1526. The code comment states the intent outright — "Resolve the ledger handle up front so a missing ledger surfaces as a 404 here, before submission" — and the handler even records set_span_error_code(&span, "error:NotFound") on the span. It then wraps the error as ServerError::Api(e) and the blanket arm turns the intended 404 into a 500. The span says NotFound, the client sees InternalError.
The export path is the same shape, without the stated intent: fluree-db-server/src/routes/export.rs:116 wraps the builder's error with .map_err(ServerError::Api)?.
Impact
- Clients cannot distinguish "you asked for something that isn't here" from "the server broke" on the affected routes. A 500 is the wrong signal for a client-side naming mistake and the wrong signal for retry logic, which should not retry a 404 but reasonably may retry a 500.
- The
@typeis also wrong, not just the status:err:system/InternalErrorinstead oferr:db/LedgerNotFound, so consumers keying on the error taxonomy misclassify it too. - 500s on routine client mistakes inflate server-fault metrics and alerting, masking real faults.
- The per-route workaround is load-bearing and easy to forget in new code. Every new handler that loads a ledger must remember to translate, and nothing enforces it; a route author who reasonably assumes the framework classifies errors gets a 500.
Current workarounds in tree
Each of these exists solely to compensate for the blanket arm:
fluree-db-server/src/routes/validate.rs:154-160—if e.is_not_found() { ServerError::not_found(...) } else { ServerError::Api(e) }fluree-db-server/src/routes/ledger.rs:579—Err(ServerError::Api(ref e)) if e.is_not_found() =>fluree-db-server/src/routes/bm25.rs:100-101— added in the BM25 create route for exactly this reasonfluree-db-server/src/routes/query.rs:2078,:2699-2700,:2900—matches!(&e, ServerError::Api(api) if api.is_not_found())
Proposed fix
Split the client-error variants out in both arms of fluree-db-server/src/error.rs, ahead of the existing blanket arms:
- status:
ApiError::Ledger(LedgerError::NotFound(_)) => StatusCode::NOT_FOUND,ApiError::Ledger(LedgerError::FutureTime { .. }) => StatusCode::BAD_REQUEST - code:
ApiError::Ledger(LedgerError::NotFound(_)) => errors::LEDGER_NOT_FOUND;FutureTimewants a request-error code rather thanerrors::INTERNAL
ApiError::Drop(_) needs the same treatment at fluree-db-server/src/error.rs:156 and :254, mapping the retracted-resource case to 409 (or 404) rather than 500.
Keep the existing => 500 arms for every other variant, which really are server-side faults. Reviewing the remaining LedgerError and Drop variants while making this change is worthwhile — the three found here came from probing a handful of endpoints, not from an audit of the enums, so others are likely misclassified too.
Worth considering as part of the same change: whether the server should delegate to ApiError::status_code() for ApiError variants generally instead of maintaining a parallel table that can drift from it. That is a larger change with a wider blast radius, so it should be evaluated separately rather than bundled in — the two-arm fix above is sufficient to close this specific mismatch.
Once the mapping is correct, the per-route is_not_found() translations listed above become redundant. They are harmless (they produce the same status the fixed mapping would) and can be removed in a follow-up sweep rather than in the same commit, so that the mapping fix is reviewable on its own and any behavior change is attributable to one place.
Test plan
- Add an integration test asserting
404+err:db/LedgerNotFoundforPOST /v1/fluree/export/nosuch:mainandPOST /v1/fluree/insertwith a missing ledger. Both currently return 500, so the test fails before the fix and passes after. - Assert that a non-
NotFoundLedgerErrorstill maps to 500, so the fix does not widen into a catch-all 404. - Existing suites that already pin 404 behavior on
/info,/log,/query,/context, and/reindexshould be unaffected, since those routes translate before reaching the mapping.
Related but distinct — not covered by this fix
POST /v1/fluree/branch with {"ledger":"nosuch:main","branch":"b2"} returns 500 with {"error":"Nameservice error: Invalid ID format: Invalid ledger ID format 'nosuch:main:main': expected 'name' or 'name:branch'"}. The alias appears to get its branch suffix appended twice before reaching the nameservice. That is a separate defect — a malformed-input error surfacing as 500 through ApiError::NameService, plus an apparent double-suffix bug — and fixing the ApiError::Ledger arms will not change it. Filed here only so it is not lost; it warrants its own issue.
Activity