3.0 KiB
3.0 KiB
error-codes.ts
(no submodules)
Purpose
Defines the stable error-code registry for @suaveplan/api-plugin-file-upload. Each failure path in the plugin throws a FileUploadError carrying one of 11 UPLOAD.* codes; consumers branch on error.code to render localised messages, decide on retry behaviour, or surface user-facing copy. The registry is the single source of truth for every observable failure mode the plugin exposes — the spec, the integration tests, and downstream consumer code all reference it by name.
Features
- 11 codes registered via
defineCodes("UPLOAD", [...])so the cause chain, OTel span events, and i18n keys all share the same string prefix. FileUploadErrorextendsSuaveplanError<UploadErrorCode>for typedinstanceofnarrowing.- Public constructor so call sites in routes, the assembler, and the storage adapters can throw directly without a factory helper.
- Documented per-code semantics so reviewers can confirm every
### Requirementin the spec maps to exactly one production call site.
Usage: Basic
import {
FileUploadError,
UPLOAD_CODES,
} from "@suaveplan/api-plugin-file-upload/error-codes/error-codes";
if (size > limit) {
throw new FileUploadError(
UPLOAD_CODES.TOO_LARGE,
`Upload exceeds singleShotLimitBytes`,
{ context: { statusCode: 413, size, limit } }
);
}
Usage: Advanced
import { FileUploadError } from "@suaveplan/api-plugin-file-upload";
try {
await uploadService.complete(id, declaredHash, principalId);
} catch (err) {
if (err instanceof FileUploadError) {
switch (err.uploadCode) {
case "UPLOAD.HASH_MISMATCH":
return c.json({ retry: true, reason: "hash-mismatch" }, 422);
case "UPLOAD.INCOMPLETE":
return c.json({ retry: true, reason: "missing-chunks" }, 409);
default:
throw err;
}
}
throw err;
}
API Reference
UPLOAD_CODES— readonly registry of 11 string constants prefixedUPLOAD..UploadErrorCode— union type derived fromUPLOAD_CODESvalues.FileUploadError extends SuaveplanError<UploadErrorCode>— concrete error class. Constructor signature(code: UploadErrorCode, message: string, options?: SuaveplanErrorOptions). Carries anuploadCodefield mirroringcodefor ergonomic narrowing.
Implementation Notes
- The registry is intentionally closed at 11 codes; adding a new code requires bumping the spec delta and threading the new code through a real production path. This is enforced by
error-codes.spec-conformance.test.ts. causecarries the originalZodErrorforINVALID_INPUTand the underlyingErrorforSTORAGE_FAILURE. Consumers may walk the cause chain viaSuaveplanError.traverseCauses()from@suaveplan/error.context.statusCodeis set by every throw site so the@suaveplan/apierror-handler translates it to the right HTTP status without per-code mapping at the handler layer.