Errors
Handle UploadStuffError codes and client-side upload failures.
UploadStuffError
UploadStuffError is the error you throw from middleware or upload logic to
reject a request with a specific HTTP status. It is exported from
@upload-stuff/server.
import { UploadStuffError } from "@upload-stuff/server";
throw new UploadStuffError({
code: "FORBIDDEN",
message: "Sign in to upload",
cause,
});The instance carries a code, the message, and the status mapped from the
code:
| Property | Type | Notes |
|---|---|---|
code | UploadStuffErrorCode | One of the codes below. |
message | string | Sent to the client as the error body. |
status | number | Derived from code — you never set it directly. |
cause | unknown | Optional underlying error, passed to Error. |
Codes and HTTP statuses
UploadStuffErrorCode is one of:
| Code | HTTP status |
|---|---|
INPUT_VALIDATION_ERROR | 400 |
BAD_REQUEST | 400 |
UNAUTHORIZED | 401 |
FORBIDDEN | 403 |
Response envelope
Every handled error response uses the same JSON envelope:
{ "error": "Sign in to upload" }A thrown UploadStuffError responds with its mapped status. So do the handler's
own 4xx guards — malformed JSON, request-shape validation failures, and unknown
endpoints all return { "error": ... } with status 400. The Node handler
additionally serializes unexpected errors as { "error": "Internal Server Error" } with status 500 — unless you pass next, in which case they are
forwarded to your framework's error handling instead.
Client-side error handling
The client surfaces failures through the onUploadError callback, which
receives a standard Error. For handled failures, error.message is the
server's { error } string:
await client.uploadFiles("avatar", files, {
onUploadError: (error) => {
toast.error(error.message);
},
});uploadFiles — and the React startUpload — also reject with the same
error after onUploadError runs, so attach a .catch (or await in a
try/catch) to avoid unhandled rejections.
In React, pass the same callback to useUploadStuff. Aborting an in-flight
upload triggers onUploadAborted rather than onUploadError.