UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

188 lines 6.63 kB
/** * @module SharedLock */ import {} from "../../namespace/_module.js"; /** * The error is thrown when trying to acquire a semaphore slot, but all slots are already taken. * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export class LimitReachedReaderSemaphoreError extends Error { static create(key, cause) { return new LimitReachedReaderSemaphoreError(`Key "${key.get()}" has reached the limit`, cause); } /** * Note: Do not instantiate `LimitReachedReaderSemaphoreError` directly via the constructor. Use the static `create()` factory method instead. * The constructor remains public only to maintain compatibility with errorPolicy types and prevent type errors. * @internal */ constructor(message, cause) { super(message, { cause }); this.name = LimitReachedReaderSemaphoreError.name; } } /** * The error is thrown when trying to referesh a semaphore slot that is already expired. * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export class FailedRefreshReaderSemaphoreError extends Error { static create(key, slotId, cause) { return new FailedRefreshReaderSemaphoreError(`Failed to refresh slot "${slotId}" of key "${key.get()}"`, cause); } /** * Note: Do not instantiate `FailedRefreshReaderSemaphoreError` directly via the constructor. Use the static `create()` factory method instead. * The constructor remains public only to maintain compatibility with errorPolicy types and prevent type errors. * @internal */ constructor(message, cause) { super(message, { cause }); this.name = FailedRefreshReaderSemaphoreError.name; } } /** * The error is thrown when trying to release a semaphore slot that is already expired. * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export class FailedReleaseReaderSemaphoreError extends Error { static create(key, slotId, cause) { return new FailedReleaseReaderSemaphoreError(`Failed to release slot "${slotId}" of key "${key.get()}"`, cause); } /** * Note: Do not instantiate `FailedReleaseReaderSemaphoreError` directly via the constructor. Use the static `create()` factory method instead. * The constructor remains public only to maintain compatibility with errorPolicy types and prevent type errors. * @internal */ constructor(message, cause) { super(message, { cause }); this.name = FailedReleaseReaderSemaphoreError.name; } } /** * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export const READER_SEMAPHORE_ERRORS = { ReachedLimit: LimitReachedReaderSemaphoreError, FailedRefresh: FailedRefreshReaderSemaphoreError, FailedRelease: FailedReleaseReaderSemaphoreError, }; /** * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export function isReaderSemaphoreError(value) { for (const ErrorClass of Object.values(READER_SEMAPHORE_ERRORS)) { if (!(value instanceof ErrorClass)) { return false; } } return true; } /** * The error is thrown when trying to acquire a lock that is owned by a different owner. * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export class FailedAcquireWriterLockError extends Error { static create(key, cause) { return new FailedAcquireWriterLockError(`Key "${key.get()}" already acquired`, cause); } /** * Note: Do not instantiate `FailedAcquireWriterLockError` directly via the constructor. Use the static `create()` factory method instead. * The constructor remains public only to maintain compatibility with errorPolicy types and prevent type errors. * @internal */ constructor(message, cause) { super(message, { cause }); this.name = FailedAcquireWriterLockError.name; } } /** * The error is thrown when trying to release a lock that is owned by a different owner. * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export class FailedReleaseWriterLockError extends Error { static create(key, lockId, cause) { return new FailedReleaseWriterLockError(`Unonwed release on key "${key.get()}" by owner "${lockId}"`, cause); } /** * Note: Do not instantiate `FailedReleaseWriterLockError` directly via the constructor. Use the static `create()` factory method instead. * The constructor remains public only to maintain compatibility with errorPolicy types and prevent type errors. * @internal */ constructor(message, cause) { super(message, { cause }); this.name = FailedReleaseWriterLockError.name; } } /** * The error is thrown when trying to referesh a lock that is owned by a different owner. * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export class FailedRefreshWriterLockError extends Error { static create(_key, lockId, cause) { return new FailedRefreshWriterLockError(`Unonwed refresh on key "${_key.get()}" by owner "${lockId}"`, cause); } /** * Note: Do not instantiate `FailedRefreshWriterLockError` directly via the constructor. Use the static `create()` factory method instead. * The constructor remains public only to maintain compatibility with errorPolicy types and prevent type errors. * @internal */ constructor(message, cause) { super(message, { cause }); this.name = FailedRefreshWriterLockError.name; } } /** * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export const WRITER_LOCK_ERRORS = { FailedAcquire: FailedAcquireWriterLockError, FailedRelease: FailedReleaseWriterLockError, FailedRefresh: FailedRefreshWriterLockError, }; /** * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export function isWriterLockError(value) { for (const ErrorClass of Object.values(WRITER_LOCK_ERRORS)) { if (!(value instanceof ErrorClass)) { return false; } } return true; } /** * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export const SHARED_LOCK_ERRORS = { ...READER_SEMAPHORE_ERRORS, ...WRITER_LOCK_ERRORS, }; /** * * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Errors */ export function isSharedLockError(value) { return isReaderSemaphoreError(value) || isWriterLockError(value); } //# sourceMappingURL=shared-lock.errors.js.map