Skip to content

useMongocampError ​

Turns an error thrown by a MongoCamp API call into a displayable message.

ts
const { describeError } = useMongocampError()
const { kind, message } = describeError(error, 'nuxtUiMongocamp.databases.errorLoad')
kindWhen
networkThe generated client threw a FetchError — the server is unreachable, or the browser hid a rejected response (this is what an expired session looks like)
unauthorizedHTTP 401
notFoundHTTP 404 — the collection, document, file, user, role, job or index does not exist (any more); message "It does not exist (any more). It may have been deleted or renamed." (nuxtUiMongocamp.errors.notFound)
forbiddenHTTP 403 — including a request the user has no grant for: the server answers that with a 401, which this module turns into a 403 before it reaches the error mapping (see useMongocampClientApi)
otherAnything else; uses the error's own message, then the optional fallback i18n key, then nuxtUiMongocamp.errors.generic

describeError is synchronous and only looks at the error object. The notAllowed and invalid kinds below come from describeServerError.

describeServerError ​

ts
const { describeServerError } = useMongocampError()
const { kind, message } = await describeServerError(error, 'nuxtUiMongocamp.roles.errorDelete')

The server sends CORS headers on every error, so the response body { code, msg } is readable. describeServerError reads it (one clone of the response, best effort) and says what the server said:

kindWhenMessage
notAllowedcode 4031 — the server refuses an operator ($function, $out, $merge, $accumulator, $where, $currentOp)"The server does not allow the operator $function." — not a missing permission, although the status is 403
invalidHTTP 400 with issues (server 2.3: a declared schema, a before-hook or the collection's validator refused the document)"The document is not valid: name: is required; qty: must be at least 1" — the first five fields, then "and n more"; issues ({ path, message }[]) is on the result
invalidHTTP 400 with a message (a filter or body the server rejects)the server's message
notFoundHTTP 404 with a message ("could not find document", "the collection … does not exist")the server's message
othercode 4092 (409, server 2.3: a versioned declared collection was changed since the document was read)"Someone else changed this document since it was loaded. …"
otherany other status ≥ 400 with a message (a 500, a 409)the server's message
network, unauthorized, forbiddenunchangedthe fixed messages above (a plain 403 stays "no permission")

Anything it cannot read — no response, a non-JSON body (a proxy's 503), no msg — falls back to describeError. The message never contains the database server's address: MongoDB's own text is extracted from a failed command (parseServerError) and every host:port is removed. The reading itself is readServerError in utils/serverError.ts.

Messages come from the nuxtUiMongocamp.errors.* locale keys (English and German). MongocampErrorAlert, the aggregation builder, the import wizard, the document save/delete dialogs of MongocampCollectionData and the delete/clone/save dialogs of the users, roles, jobs and grants components use describeServerError.

Server 2.5 ​

  • tooLarge: a 413 (BODY_MAX_BYTES, GRIDFS_UPLOAD_MAX_BYTES, IMPORT_MAX_BYTES). Server 2.6 codes get their own message in describeServerError: 4131 body (errors.tooLargeBody), 4132 upload (tooLargeUpload), 4133 import (tooLargeImport); another 413 keeps the server's text, which names the limit (tooLargeDetail).
  • requestId: a 5xx description carries the answer's x-request-id. The generic text of an unexpected error (INTERNAL_ERROR_MESSAGE) becomes errors.internal; a database error keeps its message.
  • A refused filter operator ($where, $function, $accumulator in a filter or update, 403/4031) is notAllowed with the operator, like a refused aggregation stage.

Server 2.10 / 2.11 ​

kindWhenMessage
rateLimited429 (RATE_LIMIT_LOGIN / RATE_LIMIT_REQUESTS, code 4291) — also from describeError"… Try again in 7 s." with retryAfter (seconds, from Retry-After); without the header the text without a time. An external server must expose Retry-After through CORS_HEADERS_EXPOSED for the browser to read it
accessDeniedcode 4033 (403): an access rule or a field rule of a declared collection, or drafts=true without the write grant"The access rules of this collection do not allow this." / "You may not read the field salary, …" / "You may not change these fields: salary" (issues on the result) / "Working copies (drafts) …" — a missing grant stays forbidden
unsupportedType415: code 4151 (a declared bucket's types, checked by content) or 4152 (an image variant of a file that is no image)"The bucket only accepts these types of file: image/png, text/plain." / "The file is not an image, …"
linkInvalid / linkExpireda signed file link: 403 code 4034 (changed or revoked) / 410"The link is not valid …" / "The link has expired."
dependencyMissing501 code 5013 (image variants without sharp)names the package; no request id
tooLarge413 code 4134: a batch over 1000 operationserrors.tooLargeBatch

Server 3.0 ​

kindWhenMessage
unauthorized401 code 4013 (disabled account) / 4014 (a refresh token used twice — the session was ended)says why; Log in again stays
emailUnverified403 code 4035 (AUTH_REQUIRE_VERIFIED_EMAIL)"The e-mail address is not confirmed yet. …"
emailTaken409 code 4093"Another user already has this e-mail address."
authLinkInvalid400 code 4403: a used, expired or unknown verification/reset/invitation/login link or code"… Ask for a new one."
busy503 code 5031: image variants busy"Try again in a moment." (no request id)

MongocampLogin names a disabled account and an unverified address (both only after the right password).

MongocampErrorAlert keeps Retry disabled and counts down while retryAfter runs. MongocampCollectionData treats a 4033 on a filtered request that names an unreadable field like a rejected filter (Clear filters): the server's fields/detail still lists fields a field rule hides, so the filter builder can offer one.

Released under the MIT License.