Skip to content

Commit

Permalink
Improved error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
N2D4 committed Jun 18, 2024
1 parent fd017a7 commit fe5fdaa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
6 changes: 3 additions & 3 deletions apps/backend/src/route-handlers/smart-request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,21 @@ async function parseAuth(req: NextRequest): Promise<SmartRequestAuth | null> {
case "client": {
if (!publishableClientKey) throw new KnownErrors.ClientAuthenticationRequired();
const isValid = await checkApiKeySet(projectId, { publishableClientKey });
if (!isValid) throw new KnownErrors.InvalidPublishableClientKey();
if (!isValid) throw new KnownErrors.InvalidPublishableClientKey(projectId);
projectAccessType = "key";
break;
}
case "server": {
if (!secretServerKey) throw new KnownErrors.ServerAuthenticationRequired();
const isValid = await checkApiKeySet(projectId, { secretServerKey });
if (!isValid) throw new KnownErrors.InvalidSecretServerKey();
if (!isValid) throw new KnownErrors.InvalidSecretServerKey(projectId);
projectAccessType = "key";
break;
}
case "admin": {
if (!superSecretAdminKey) throw new KnownErrors.AdminAuthenticationRequired();
const isValid = await checkApiKeySet(projectId, { superSecretAdminKey });
if (!isValid) throw new KnownErrors.InvalidSuperSecretAdminKey();
if (!isValid) throw new KnownErrors.InvalidSuperSecretAdminKey(projectId);
projectAccessType = "key";
break;
}
Expand Down
6 changes: 3 additions & 3 deletions apps/dashboard/src/route-handlers/smart-request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,21 @@ async function parseAuth(req: NextRequest): Promise<SmartRequestAuth | null> {
case "client": {
if (!publishableClientKey) throw new KnownErrors.ClientAuthenticationRequired();
const isValid = await checkApiKeySet(projectId, { publishableClientKey });
if (!isValid) throw new KnownErrors.InvalidPublishableClientKey();
if (!isValid) throw new KnownErrors.InvalidPublishableClientKey(projectId);
projectAccessType = "key";
break;
}
case "server": {
if (!secretServerKey) throw new KnownErrors.ServerAuthenticationRequired();
const isValid = await checkApiKeySet(projectId, { secretServerKey });
if (!isValid) throw new KnownErrors.InvalidSecretServerKey();
if (!isValid) throw new KnownErrors.InvalidSecretServerKey(projectId);
projectAccessType = "key";
break;
}
case "admin": {
if (!superSecretAdminKey) throw new KnownErrors.AdminAuthenticationRequired();
const isValid = await checkApiKeySet(projectId, { superSecretAdminKey });
if (!isValid) throw new KnownErrors.InvalidSuperSecretAdminKey();
if (!isValid) throw new KnownErrors.InvalidSuperSecretAdminKey(projectId);
projectAccessType = "key";
break;
}
Expand Down
27 changes: 18 additions & 9 deletions packages/stack-shared/src/known-errors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,31 +240,40 @@ const RequestTypeWithoutProjectId = createKnownErrorConstructor(
const InvalidPublishableClientKey = createKnownErrorConstructor(
InvalidProjectAuthentication,
"INVALID_PUBLISHABLE_CLIENT_KEY",
() => [
(projectId: string) => [
401,
"The publishable key is not valid for the given project. Does the project and/or the key exist?",
`The publishable key is not valid for the project ${JSON.stringify(projectId)}. Does the project and/or the key exist?`,
{
projectId,
},
] as const,
() => [] as const,
(json: any) => [json.projectId] as const,
);

const InvalidSecretServerKey = createKnownErrorConstructor(
InvalidProjectAuthentication,
"INVALID_SECRET_SERVER_KEY",
() => [
(projectId: string) => [
401,
"The secret server key is not valid for the given project. Does the project and/or the key exist?",
`The secret server key is not valid for the project ${JSON.stringify(projectId)}. Does the project and/or the key exist?`,
{
projectId,
},
] as const,
() => [] as const,
(json: any) => [json.projectId] as const,
);

const InvalidSuperSecretAdminKey = createKnownErrorConstructor(
InvalidProjectAuthentication,
"INVALID_SUPER_SECRET_ADMIN_KEY",
() => [
(projectId: string) => [
401,
"The super secret admin key is not valid for the given project. Does the project and/or the key exist?",
`The super secret admin key is not valid for the project ${JSON.stringify(projectId)}. Does the project and/or the key exist?`,
{
projectId,
},
] as const,
() => [] as const,
(json: any) => [json.projectId] as const,
);

const InvalidAdminAccessToken = createKnownErrorConstructor(
Expand Down

0 comments on commit fe5fdaa

Please sign in to comment.