Reference

Error Codes

All error codes and how to handle them

Every error thrown by permly has a .code property and a .name property. Catch errors with isPermissionsError() to work around instanceof issues when two copies of the package are loaded.

Error Classes

PERMISSION_DENIED

Class: PermissionDeniedError

User doesn't have the required permission.

import { perms } from "./permly.js";
import { PermissionDeniedError } from "permly";

await perms.sync();
try {
  // @ts-expect-error admin.delete is not in the permission config
  await perms.user(1).can("admin.delete");
  throw new Error("expected PermissionDeniedError");
} catch (err) {
  if (err instanceof PermissionDeniedError) {
    err.missing; // ["admin.delete"] ✓
  } else throw err;
}

Common causes:

  • User hasn't been assigned the required permission
  • User hasn't been assigned a role that grants the permission
  • Permission name is misspelled (check strict mode)

How to fix:

  • Assign the required permission to the user: await perms.user(userId).givePermission("admin.delete")
  • Or assign a role that has the permission: await perms.user(userId).assignRole("admin")

PERMISSION_NOT_FOUND

Class: PermissionNotFoundError

Permission doesn't exist (not in config or database).

import { perms } from "./permly.js";
import { PermissionNotFoundError } from "permly";

await perms.sync();
try {
  // @ts-expect-error posts.foo does not exist in permissions config
  await perms.user(1).can("posts.foo");
  throw new Error("expected PermissionNotFoundError");
} catch (err) {
  if (err instanceof PermissionNotFoundError) {
    err.permission; // "posts.foo" ✓
    typeof err.suggestion; // "string" | "undefined" ✓
  } else throw err;
}

Common causes:

  • Permission name is misspelled
  • Permission is in config but perms.sync() hasn't run to create it
  • Using strict mode and permission isn't in config

How to fix:

  • Check the spelling in your config
  • Run await perms.sync() after creating the permissions instance
  • If using strict mode, add the permission to your config

ROLE_NOT_FOUND

Class: RoleNotFoundError

Role doesn't exist (not in config or database).

import { perms } from "./permly.js";
import { RoleNotFoundError } from "permly";

await perms.sync();
try {
  // @ts-expect-error superadmin role does not exist in roles config
  await perms.user(1).assignRole("superadmin");
  throw new Error("expected RoleNotFoundError");
} catch (err) {
  if (err instanceof RoleNotFoundError) {
    err.role; // "superadmin" ✓
    typeof err.suggestion; // "string" | "undefined" ✓
  } else throw err;
}

Common causes:

  • Role name is misspelled
  • Role is in config but perms.sync() hasn't run to create it
  • Using strict mode and role isn't in config

How to fix:

  • Check the spelling in your config
  • Run await perms.sync() after creating the permissions instance
  • If using strict mode, add the role to your config

INVALID_INPUT

Class: InvalidInputError

Invalid input to a permly function.

import { perms } from "./permly.js";
import { InvalidInputError } from "permly";

await perms.sync();
try {
  await perms.user("").can("posts.create");
  throw new Error("expected InvalidInputError");
} catch (err) {
  if (err instanceof InvalidInputError) {
    typeof err.message; // "string" ✓
  } else throw err;
}

Common causes:

  • Empty string as user ID, role name, or permission name
  • Null or undefined where a string is required
  • Calling permly methods before sync()

How to fix:

  • Ensure user IDs are valid strings
  • Ensure permission and role names are in your config
  • Call await perms.sync() before using permly

LOCK_TIMEOUT

Class: PermissionsError

Database couldn't acquire a lock in time (usually during concurrent sync operations).

import { perms, setupPermissions } from "./permly.js";

await setupPermissions();
try {
  await perms.sync();
  // LOCK_TIMEOUT is rare with memory adapter, but the code pattern is valid
  true; // ✓
} catch (err: any) {
  if (err?.code === "LOCK_TIMEOUT") {
    "Sync in progress, try again"; // ✓
  }
}

Common causes:

  • Multiple processes running perms.sync() at the same time
  • Database lock conflict with another permly instance
  • Database is slow to grant locks

How to fix:

  • Retry after a short delay: await new Promise(r => setTimeout(r, 1000)); await perms.sync();
  • Ensure only one process runs sync() at startup
  • Check database performance and connection pool settings
  • For Galera/MariaDB cluster, ensure wsrep_sync_wait=7 for consistency

Handling Errors

Check any permly error

import { perms } from "./permly.js";
import { isPermissionsError } from "permly";

await perms.sync();
try {
  await perms.user(1).can("admin.users");
  throw new Error("expected permly error");
} catch (err) {
  if (isPermissionsError(err)) {
    typeof err.code; // "string" ✓
  } else throw err;
}

Check specific error type

import { perms } from "./permly.js";
import { isPermissionDeniedError } from "permly";

await perms.sync();
try {
  await perms.user(1).can("admin.users");
  throw new Error("expected PermissionDeniedError");
} catch (err) {
  if (isPermissionDeniedError(err)) {
    typeof err.missing; // "object" ✓
  } else throw err;
}

Handle in Express middleware

import { isPermissionsError, isPermissionDeniedError } from "permly";

const errorHandler = (err: any, req: any, res: any, next: any) => {
  if (isPermissionsError(err)) {
    if (isPermissionDeniedError(err)) {
      return res.status(403).json({ error: "Forbidden", missing: err.missing });
    }
    if (err.code === "LOCK_TIMEOUT") {
      return res.status(503).json({ error: "Service temporarily unavailable" });
    }
  }
  next(err);
};
typeof errorHandler; // "function" ✓

Error Messages

Errors include helpful messages with suggestions when available:

import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";

const perms = createPermissions({
  adapter: memoryAdapter(),
  permissions: ["posts.create"],
  roles: ["admin"],
  strict: false
});
await perms.sync();
try {
  // @ts-expect-error admi is not the correct role name (typo demonstration)
  await perms.user(1).assignRole("admi");
  throw new Error("expected RoleNotFoundError");
} catch (err: any) {
  err.message.includes("admin"); // true ✓
}