SDK
Errors
What the SDK throws, and how to fail closed.
| Error | Thrown when |
|---|---|
PeretynError | The runtime answered with an error status. It has status and body. assert throws it with 403 on a deny. |
PeretynUnavailableError | The SDK couldn't reach the runtime. |
PeretynTimeoutError | The call ran out of timeoutMs. It extends PeretynUnavailableError. |
Fail closed or fail open
When Peretyn is unreachable, only you know whether your application should allow or deny. So the SDK throws instead of guessing.
This function denies whenever Peretyn can't answer:
import { peretyn, PeretynUnavailableError } from '@peretyn/sdk';
import type { schema } from './schema';
const authz = peretyn<typeof schema>({ token: process.env.PERETYN_TOKEN! });
async function canView(userId: string, documentId: string): Promise<boolean> {
try {
const { allowed } = await authz.check({
subject: { type: 'user', id: userId },
relation: 'can_view',
object: { type: 'document', id: documentId },
});
return allowed;
} catch (error) {
if (error instanceof PeretynUnavailableError) return false;
throw error;
}
}
console.log((await canView('alice', 'roadmap')) ? 'allowed' : 'denied');The script prints allowed, because Alice owns the roadmap and the runtime is up. While the runtime is down, canView returns false.
Return true in place of false to fail open. Any other error still throws, because it means that the request itself was wrong.