Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Improve errors when throw on the client side to make it more human-readable

Kiran Kf4448a1644d8
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/analytics/allowed-hostnames.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/analytics/allowed-hostnames.tsx
index d7065aad43..ac91117d2e 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/analytics/allowed-hostnames.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/analytics/allowed-hostnames.tsx
@@ -1,5 +1,6 @@
"use client";
+import { clientAccessCheck } from "@/lib/api/tokens/permissions";
import useWorkspace from "@/lib/swr/use-workspace";
import { Button, CardList, CircleCheck, LoadingSpinner, Trash } from "@dub/ui";
import { cn, validDomainRegex } from "@dub/utils";
@@ -43,7 +44,13 @@ export const AllowedHostnames = () => {
const AddHostnameForm = () => {
const [hostname, setHostname] = useState("");
const [processing, setProcessing] = useState(false);
- const { id, allowedHostnames, mutate } = useWorkspace();
+ const { id, allowedHostnames, mutate, role } = useWorkspace();
+
+ const { error: permissionsError } = clientAccessCheck({
+ action: "workspaces.write",
+ role,
+ customPermissionDescription: "add hostnames",
+ });
const addHostname = async () => {
if (allowedHostnames?.includes(hostname)) {
@@ -115,15 +122,22 @@ const AddHostnameForm = () => {
disabled={!isHostnameValid || hostname.length === 0}
loading={processing}
className="w-40"
+ disabledTooltip={permissionsError || undefined}
/>
</form>
);
};
const AllowedHostnameCard = ({ hostname }: { hostname: string }) => {
- const { id, allowedHostnames, mutate } = useWorkspace();
+ const { id, allowedHostnames, mutate, role } = useWorkspace();
const [processing, setProcessing] = useState(false);
+ const { error: permissionsError } = clientAccessCheck({
+ action: "workspaces.write",
+ role,
+ customPermissionDescription: "delete hostnames",
+ });
+
const deleteHostname = async () => {
if (!confirm("Are you sure you want to delete this hostname?")) {
return;
@@ -183,6 +197,7 @@ const AllowedHostnameCard = ({ hostname }: { hostname: string }) => {
)
}
onClick={deleteHostname}
+ disabledTooltip={permissionsError || undefined}
/>
</div>
</CardList.Card>
diff --git a/apps/web/lib/api/tokens/permissions.ts b/apps/web/lib/api/tokens/permissions.ts
index 8e28e9fa05..03eefffbd4 100644
--- a/apps/web/lib/api/tokens/permissions.ts
+++ b/apps/web/lib/api/tokens/permissions.ts
@@ -8,10 +8,12 @@ export const throwIfNoAccess = ({
permissions,
requiredPermissions,
workspaceId,
+ externalRequest = false,
}: {
permissions: PermissionAction[]; // user or token permissions
requiredPermissions: PermissionAction[];
workspaceId: string;
+ externalRequest?: boolean;
}) => {
if (requiredPermissions.length === 0) {
return;
@@ -25,9 +27,13 @@ export const throwIfNoAccess = ({
return;
}
+ const message = externalRequest
+ ? `The provided key does not have the required permissions for this endpoint on the workspace 'ws_${workspaceId}'. Having the '${missingPermissions.join(" ")}' permission would allow this request to continue.`
+ : "You don't have the necessary permissions to complete this request.";
+
throw new DubApiError({
code: "forbidden",
- message: `The provided key does not have the required permissions for this endpoint on the workspace 'ws_${workspaceId}'. Having the '${missingPermissions.join(" ")}' permission would allow this request to continue.`,
+ message,
});
};
diff --git a/apps/web/lib/auth/workspace-edge.ts b/apps/web/lib/auth/workspace-edge.ts
index c332b3a436..c8ec0aeb29 100644
--- a/apps/web/lib/auth/workspace-edge.ts
+++ b/apps/web/lib/auth/workspace-edge.ts
@@ -305,6 +305,7 @@ export const withWorkspaceEdge = (
permissions,
requiredPermissions,
workspaceId: workspace.id,
+ externalRequest: Boolean(apiKey),
});
// beta feature checks
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index db7c64e08b..d9f76c51e5 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -322,6 +322,7 @@ export const withWorkspace = (
permissions,
requiredPermissions,
workspaceId: workspace.id,
+ externalRequest: Boolean(apiKey),
});
}