Errors
When the data API refuses a request, or a request can't complete, core throws a CommentsRequestError. Its message is always safe to show a visitor, in the site's language where the server wrote it, and the rest says what went wrong in a form your code can act on. The thread store throws these from its actions, keeps one in state.error when a thread couldn't load, and puts its details in the comments:request-error event.
import { isCommentsRequestError } from '@verbb/comments-core';
try {
await store.vote(commentUid, 'upvote');
} catch (error) {
if (!isCommentsRequestError(error)) throw error;
if (error.code === 'rate_limited') {
showNotice(`Too many votes. Try again in ${error.retryAfter ?? 60} seconds.`);
} else if (error.category === 'auth') {
showSignInPrompt();
} else {
showNotice(error.message);
}
}What an Error Holds
| Property | What it is |
|---|---|
message | A sentence for the visitor. |
code | The first error's code, from the table below. |
category | A broad group, such as validation or auth. |
status | The HTTP status, or 0 when there was no response. |
errors | Every error the server gave, each with its own code, category, message, fieldErrors, recoverable and retryAfter. |
fieldErrors | Every error's field messages merged, keyed by field as content, guest.name, guest.email or fields.handle, ready for a form. |
recoverable | Whether trying again, after a refresh or a wait, can succeed. |
retryAfter | Seconds to wait before trying again, for rate_limited, or null. |
operationId | The write's ID, so a retry can reuse it, as Client and Transports explains. |
detail | What went wrong for a developer, such as the API versions that didn't match. Never shown to visitors. |
error.has(code) checks every error rather than only the first, which matters for a post refused for more than one reason.
Handling Codes by Category
New codes may be added in any release, so handle a code you don't recognise by its category. Every code belongs to one.
| Category | What it covers |
|---|---|
validation | Something posted didn't pass, with fieldErrors saying where. Show the messages beside the fields. |
protection | The spam protection refused the post or couldn't run. |
rate | Too many requests from one address. Wait retryAfter seconds. |
auth | Who's asking couldn't be established, or isn't allowed from this origin. The sign-in provider recovers from the ones it can by itself. |
policy | The person isn't allowed to do this, such as in a closed thread. |
state | What was asked about doesn't exist, or changed in the meantime. |
request | The request itself was wrong, usually a mistake in code rather than anything a visitor did. |
server | The server failed or answered with something other than the data API's JSON. |
network | The request never reached the server, or took too long. |
Every Code
The HTTP status is the one the server answers with. Codes without one are raised by core itself, when a request never reached the server or the answer wasn't the data API's JSON. COMMENTS_ERROR_CODES holds the same list, with each code's category and status.
| Code | Category | HTTP status | When it happens |
|---|---|---|---|
validation_failed | validation | 422 | Something posted didn’t pass validation, such as an empty comment or a required field left out. fieldErrors says which. |
file_type_not_allowed | validation | 422 | An upload’s type isn’t allowed, or its contents don’t match its extension. |
file_too_large | validation | 422 | An upload is larger than the field or the server allows. |
too_many_files | validation | 422 | More files than a field takes, or a guest’s address has too many uploads waiting to be posted. |
protection_required | protection | 422 | A write arrived without the spam protection evidence it needs, or with a preparation that isn’t valid. |
protection_expired | protection | 422 | The protection preparation ran out or was already used. Core fetches a new one and tries once more by itself. |
protection_unavailable | protection | 503 | The spam protection service couldn’t be reached. Trying again later can work. |
challenge_failed | protection | 422 | The visitor didn’t pass a challenge, such as Turnstile’s. Never retried automatically. |
possible_spam | protection | 422 | Protect judged the comment to be spam. Never retried automatically. |
rate_limited | rate | 429 | Too many requests from one address. retryAfter says how many seconds to wait. |
sign_in_required | auth | 401 | The action needs a signed-in member, or a request on another site came without a member token. |
token_expired | auth | 401 | A member token ran out. memberToken() fetches the next one and tries once more. |
token_revoked | auth | 401 | A member token was revoked or isn’t known, or its member changed their password since it was minted. |
session_expired | auth | 401 | A guest’s session token ran out, changed or moved. The sign-in provider starts a new session and tries once more. |
origin_not_allowed | auth | 403 | The request’s origin doesn’t fit its profile, or isn’t listed in Other Sites with what it needs. |
csrf_failed | auth | 403 | A cookie sign-in’s write came without a valid CSRF token. cookieSession() reads a new token and tries once more. |
signature_invalid | auth | 401 | A signed member-token or revoke-member-tokens request from a site’s own server has a missing, wrong, old or repeated signature. |
user_not_eligible | auth | 403 | A member-token request named a user who can’t have one, such as a suspended or unknown user. |
forbidden | policy | 403 | The person can’t do this to this comment, such as voting where voting is off or editing someone else’s comment. |
blocked | policy | 403 | The person is blocked from taking part. |
closed | policy | 403 | The thread is closed to new comments. |
edit_window_passed | policy | 403 | The comment can no longer be edited or deleted by its author. |
not_found | state | 404 | The owner, comment or thread doesn’t exist or isn’t public, or has comments turned off. |
conflict | state | 409 | The comment changed since it was loaded. Load it again before saving. |
limit_exceeded | request | 400 | A request asked for too many items at once. |
invalid_cursor | request | 400 | A page’s cursor isn’t valid any more, such as after the sort or limit changed. Load the thread again. |
method_not_allowed | request | 405 | The action was called with the wrong HTTP method. |
unsupported_api_version | request | 400 | The server and the package read different versions of the JSON format, usually a page left open across an upgrade. Reloading fixes it. |
server_error | server | 500 | The server failed, or answered with an error page rather than the data API’s JSON. |
network_error | network | None, raised by core | The request never reached the server, or was abandoned after the timeout. |
invalid_response | server | None, raised by core | The answer wasn’t the data API’s JSON, such as a proxy’s page or a body without apiVersion. |
invalid_request | request | 400 | The request couldn’t be read, such as a body that isn’t a JSON object or a profile the API doesn’t know. |
The plugin's Data API reference shows the error response these come from.