Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-limits-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@softzweig/spindle-core": minor
---

Add the `LIMITED` error code for rate limits and quotas, with `ServiceError.limited()` and `RepositoryError.limited()`, and export `SpindleFailureDetails` as the shared details type.
25 changes: 15 additions & 10 deletions packages/core/src/repository/repository.error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SpindleError, SpindleErrorCode } from "../result.js";
import { SpindleError, SpindleErrorCode, type SpindleFailureDetails } from "../result.js";

/**
* A repository failure a caller is expected to branch on.
Expand Down Expand Up @@ -30,47 +30,52 @@ import { SpindleError, SpindleErrorCode } from "../result.js";
* @human
*/
export class RepositoryError extends SpindleError {
constructor(code: SpindleErrorCode, message: string, details?: Record<string, unknown>) {
constructor(code: SpindleErrorCode, message: string, details?: SpindleFailureDetails) {
super(code, message, details);
this.name = "RepositoryError";
}

static of(code: SpindleErrorCode, message: string, details?: Record<string, unknown>) {
static of(code: SpindleErrorCode, message: string, details?: SpindleFailureDetails) {
return new RepositoryError(code, message, details);
}

/** No row matches the id an update was addressed to. */
static notFound(message: string, details?: Record<string, unknown>) {
static notFound(message: string, details?: SpindleFailureDetails) {
return RepositoryError.of(SpindleErrorCode.NOT_FOUND, message, details);
}

/** The row breaks a column rule the backend enforces, such as a not-null constraint. */
static invalidInput(message: string, details?: Record<string, unknown>) {
static invalidInput(message: string, details?: SpindleFailureDetails) {
return RepositoryError.of(SpindleErrorCode.INVALID_INPUT, message, details);
}

/** The caller is not allowed to read or write these rows. */
static forbidden(message: string, details?: Record<string, unknown>) {
static forbidden(message: string, details?: SpindleFailureDetails) {
return RepositoryError.of(SpindleErrorCode.FORBIDDEN, message, details);
}

/** Existing state contradicts the write, such as a duplicate unique key. */
static conflict(message: string, details?: Record<string, unknown>) {
static conflict(message: string, details?: SpindleFailureDetails) {
return RepositoryError.of(SpindleErrorCode.CONFLICT, message, details);
}

/** The write is not allowed in the current state, such as deleting a claimed row. */
static preconditionFailed(message: string, details?: Record<string, unknown>) {
static preconditionFailed(message: string, details?: SpindleFailureDetails) {
return RepositoryError.of(SpindleErrorCode.PRECONDITION_FAILED, message, details);
}

/** The backend cannot be reached; the same write may succeed later. */
static unavailable(message: string, details?: Record<string, unknown>) {
static unavailable(message: string, details?: SpindleFailureDetails) {
return RepositoryError.of(SpindleErrorCode.UNAVAILABLE, message, details);
}

/** A failure the adapter could not classify. */
static unknown(message: string, details?: Record<string, unknown>) {
static unknown(message: string, details?: SpindleFailureDetails) {
return RepositoryError.of(SpindleErrorCode.UNKNOWN, message, details);
}

/** A rate limit or quota was exceeded. */
static limited(message: string, details?: SpindleFailureDetails) {
return RepositoryError.of(SpindleErrorCode.LIMITED, message, details);
}
}
7 changes: 6 additions & 1 deletion packages/core/src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export type SpindleResult<T> = SpindleSuccess<T> | SpindleFailure;

export type SpindleErrorCode = (typeof SpindleErrorCode)[keyof typeof SpindleErrorCode];

/** The free-form details every Spindle failure carries. */
export type SpindleFailureDetails = Record<string, unknown>;

/**
* Failure categories Spindle reports to callers.
*
Expand All @@ -33,6 +36,8 @@ export const SpindleErrorCode = Object.freeze({
UNAVAILABLE: "unavailable",
/** A failure the caller could not classify. */
UNKNOWN: "unknown",
/** A rate limit or any quota has been exceeded. */
LIMITED: "limited",
});

/**
Expand All @@ -49,7 +54,7 @@ export abstract class SpindleError extends Error {
constructor(
readonly code: SpindleErrorCode,
message: string,
readonly details?: Record<string, unknown>,
readonly details?: SpindleFailureDetails,
) {
super(message);
}
Expand Down
38 changes: 21 additions & 17 deletions packages/core/src/service/service.error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SpindleError, SpindleErrorCode } from "../result.js";
import { SpindleError, SpindleErrorCode, type SpindleFailureDetails } from "../result.js";

/** What an authorization hook may answer: only `true` permits, a string refuses with its reason. */
export type AuthorizationVerdict = boolean | undefined | null | string;
Expand All @@ -15,47 +15,51 @@ export type AuthorizationVerdict = boolean | undefined | null | string;
* @human
*/
export class ServiceError extends SpindleError {
constructor(code: SpindleErrorCode, message: string, details?: Record<string, unknown>) {
constructor(code: SpindleErrorCode, message: string, details?: SpindleFailureDetails) {
super(code, message, details);
this.name = "ServiceError";
}

static of(code: SpindleErrorCode, message: string, details?: Record<string, unknown>) {
static of(code: SpindleErrorCode, message: string, details?: SpindleFailureDetails) {
return new ServiceError(code, message, details);
}

static notFound(message: string, details?: Record<string, unknown>) {
static notFound(message: string, details?: SpindleFailureDetails) {
return ServiceError.of(SpindleErrorCode.NOT_FOUND, message, details);
}

static invalidInput(message: string, details?: Record<string, unknown>) {
static invalidInput(message: string, details?: SpindleFailureDetails) {
return ServiceError.of(SpindleErrorCode.INVALID_INPUT, message, details);
}

static forbidden(message: string, details?: Record<string, unknown>) {
static forbidden(message: string, details?: SpindleFailureDetails) {
return ServiceError.of(SpindleErrorCode.FORBIDDEN, message, details);
}

static conflict(message: string, details?: Record<string, unknown>) {
static conflict(message: string, details?: SpindleFailureDetails) {
return ServiceError.of(SpindleErrorCode.CONFLICT, message, details);
}

static preconditionFailed(message: string, details?: Record<string, unknown>) {
static preconditionFailed(message: string, details?: SpindleFailureDetails) {
return ServiceError.of(SpindleErrorCode.PRECONDITION_FAILED, message, details);
}

static unavailable(message: string, details?: Record<string, unknown>) {
static unavailable(message: string, details?: SpindleFailureDetails) {
return ServiceError.of(SpindleErrorCode.UNAVAILABLE, message, details);
}

static unknown(message: string, details?: Record<string, unknown>) {
static unknown(message: string, details?: SpindleFailureDetails) {
return ServiceError.of(SpindleErrorCode.UNKNOWN, message, details);
}

static limited(message: string, details?: SpindleFailureDetails) {
return ServiceError.of(SpindleErrorCode.LIMITED, message, details);
}

static assert(
condition: unknown,
message: string,
details?: Record<string, unknown>,
details?: SpindleFailureDetails,
code: SpindleErrorCode = SpindleErrorCode.PRECONDITION_FAILED,
): asserts condition {
if (condition) return;
Expand All @@ -65,39 +69,39 @@ export class ServiceError extends SpindleError {
static assertFound(
condition: unknown,
message: string,
details?: Record<string, unknown>,
details?: SpindleFailureDetails,
): asserts condition {
ServiceError.assert(condition, message, details, SpindleErrorCode.NOT_FOUND);
}

static assertConflict(
condition: unknown,
message: string,
details?: Record<string, unknown>,
details?: SpindleFailureDetails,
): asserts condition {
ServiceError.assert(condition, message, details, SpindleErrorCode.CONFLICT);
}

static assertAllowed(
condition: unknown,
message: string,
details?: Record<string, unknown>,
details?: SpindleFailureDetails,
): asserts condition {
ServiceError.assert(condition, message, details, SpindleErrorCode.FORBIDDEN);
}

static assertValidInput(
condition: unknown,
message: string,
details?: Record<string, unknown>,
details?: SpindleFailureDetails,
): asserts condition {
ServiceError.assert(condition, message, details, SpindleErrorCode.INVALID_INPUT);
}

static assertAvailable(
condition: unknown,
message: string,
details?: Record<string, unknown>,
details?: SpindleFailureDetails,
): asserts condition {
ServiceError.assert(condition, message, details, SpindleErrorCode.UNAVAILABLE);
}
Expand All @@ -108,7 +112,7 @@ export class ServiceError extends SpindleError {
*/
static assertPermitted(
verdict: AuthorizationVerdict,
details?: Record<string, unknown>,
details?: SpindleFailureDetails,
): asserts verdict is true {
if (verdict === true) return;

Expand Down
1 change: 1 addition & 0 deletions packages/core/test/repository/repository.error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("RepositoryError", () => {
["preconditionFailed", SpindleErrorCode.PRECONDITION_FAILED],
["unavailable", SpindleErrorCode.UNAVAILABLE],
["unknown", SpindleErrorCode.UNKNOWN],
["limited", SpindleErrorCode.LIMITED],
] as const)("%s builds a %s error", ([factory, code]) => {
const details = { id: 1 };
const error = RepositoryError[factory]("Message", details);
Expand Down
1 change: 1 addition & 0 deletions packages/core/test/result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("SpindleErrorCode", () => {
PRECONDITION_FAILED: "precondition_failed",
UNAVAILABLE: "unavailable",
UNKNOWN: "unknown",
LIMITED: "limited",
});
expect(Object.isFrozen(SpindleErrorCode)).toBe(true);
});
Expand Down
1 change: 1 addition & 0 deletions packages/core/test/service/service.error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe("ServiceError", () => {
["preconditionFailed", SpindleErrorCode.PRECONDITION_FAILED],
["unavailable", SpindleErrorCode.UNAVAILABLE],
["unknown", SpindleErrorCode.UNKNOWN],
["limited", SpindleErrorCode.LIMITED],
] as const)("%s builds a %s error", ([factory, code]) => {
const details = { field: "id" };
const error = ServiceError[factory]("Message", details);
Expand Down
Loading