Files
clean-architecture-frontend…/src/features/reference-feature/application/reference-error.ts
T

72 lines
2.1 KiB
TypeScript

import {
defineAppError,
type AppError,
} from "../../../platform/errors/app-error.ts";
import type { ApiFailure } from "../../../contracts/errors.ts";
/**
* Spec §17–§19. The reference feature's own domain errors.
*
* This union is local to the feature. It satisfies the platform `AppError`
* envelope structurally; nothing central enumerates it, so adding or removing
* this feature never edits a shared error type.
*/
export type ReferenceError =
| (AppError & {
readonly kind: "reference.not-found";
readonly category: "domain";
readonly severity: "warning";
readonly retryability: "never";
})
| (AppError & {
readonly kind: "reference.conflict";
readonly category: "domain";
readonly severity: "warning";
readonly retryability: "never";
})
| (AppError & {
readonly kind: "reference.rejected";
readonly category: "validation";
readonly severity: "warning";
readonly retryability: "never";
});
export const REFERENCE_ERRORS = Object.freeze({
NOT_FOUND: defineAppError({
kind: "reference.not-found",
category: "domain",
severity: "warning",
retryability: "never",
}),
CONFLICT: defineAppError({
kind: "reference.conflict",
category: "domain",
severity: "warning",
retryability: "never",
}),
REJECTED: defineAppError({
kind: "reference.rejected",
category: "validation",
severity: "warning",
retryability: "never",
}),
} satisfies Record<string, ReferenceError>);
/**
* Interprets a transport-normalized failure in this feature's own vocabulary.
* Anything that is not a reference domain outcome stays `null`: the platform
* error mapping, not this feature, owns transport/contract failures.
*/
export function toReferenceError(failure: ApiFailure): ReferenceError | null {
switch (failure.kind) {
case "NOT_FOUND":
return REFERENCE_ERRORS.NOT_FOUND;
case "CONFLICT":
return REFERENCE_ERRORS.CONFLICT;
case "VALIDATION_REJECTED":
return REFERENCE_ERRORS.REJECTED;
default:
return null;
}
}