Slightly improve error handling

This commit is contained in:
Stan Wohlwend 2024-05-20 01:49:10 +02:00
parent 040ec072f3
commit 36752d2d39
6 changed files with 11 additions and 9 deletions

View File

@ -2,7 +2,7 @@
import React from "react";
import { useUser } from "@stackframe/stack";
import { throwErr } from "@stackframe/stack-shared/dist/utils/errors";
import { StackAssertionError, throwErr } from "@stackframe/stack-shared/dist/utils/errors";
import { cacheFunction } from "@stackframe/stack-shared/dist/utils/caches";
import { CurrentUser, StackAdminApp } from "@stackframe/stack";
@ -39,7 +39,7 @@ export function AdminAppProvider(props: { projectId: string, children: React.Rea
export function useAdminApp() {
const stackAdminApp = React.useContext(StackAdminAppContext);
if (!stackAdminApp) {
throw new Error("useAdminApp must be used within a AdminInterfaceProvider");
throw new StackAssertionError("useAdminApp must be used within an AdminInterfaceProvider");
}
return stackAdminApp;

View File

@ -45,7 +45,7 @@ export const GET = deprecatedSmartRouteHandler(async (req: NextRequest, options:
const cookie = cookies().get("stack-oauth");
if (!cookie) {
throw new Error("stack-oauth cookie not found");
throw new StatusError(StatusError.BadRequest, "stack-oauth cookie not found");
}
let decoded: Awaited<ReturnType<typeof jwtSchema.validate>>;

View File

@ -5,6 +5,7 @@ import { deprecatedSmartRouteHandler } from "@/route-handlers/smart-route-handle
import { deprecatedParseRequest } from "@/route-handlers/smart-request";
import { hashPassword } from "@stackframe/stack-shared/dist/utils/password";
import { KnownErrors } from "@stackframe/stack-shared";
import { StatusError } from "@stackframe/stack-shared/dist/utils/errors";
const postSchema = yup.object({
body: yup.object({
@ -40,7 +41,7 @@ export const POST = deprecatedSmartRouteHandler(async (req: NextRequest) => {
}
if (!password) {
throw new Error("Password is required when onlyVerify is false");
throw new StatusError(StatusError.BadRequest, "Password is required when onlyVerify is false");
}
await prismaClient.projectUser.update({

View File

@ -11,7 +11,7 @@ import { getProject } from "@/lib/projects";
import { validateUrl } from "@/utils/url";
import { getPasswordError } from "@stackframe/stack-shared/dist/helpers/password";
import { getApiKeySet, publishableClientKeyHeaderSchema } from "@/lib/api-keys";
import { StatusError, captureError } from "@stackframe/stack-shared/dist/utils/errors";
import { StackAssertionError, StatusError, captureError } from "@stackframe/stack-shared/dist/utils/errors";
import { KnownErrors } from "@stackframe/stack-shared";
import { createTeamOnSignUp } from "@/lib/users";
@ -51,7 +51,7 @@ export const POST = deprecatedSmartRouteHandler(async (req: NextRequest) => {
const project = await getProject(projectId);
if (!project) {
throw new Error("Project not found"); // This should never happen, make typescript happy
throw new StackAssertionError("Project not found. This should never happen"); // This should never happen, make typescript happy
}
if (!project.evaluatedConfig.credentialEnabled) {

View File

@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server";
import * as yup from "yup";
import { StatusError } from "@stackframe/stack-shared/dist/utils/errors";
import { StackAssertionError, StatusError } from "@stackframe/stack-shared/dist/utils/errors";
import { deprecatedSmartRouteHandler } from "@/route-handlers/smart-route-handler";
import { deprecatedParseRequest } from "@/route-handlers/smart-request";
import { checkApiKeySet, publishableClientKeyHeaderSchema, superSecretAdminKeyHeaderSchema } from "@/lib/api-keys";
@ -50,7 +50,7 @@ const handler = deprecatedSmartRouteHandler(async (req: NextRequest, options: {
const project = await getProject(projectId);
if (!project) {
throw new Error("Project not found but the API key was valid? Something weird happened");
throw new StackAssertionError("Project not found but the API key was valid? Something weird happened");
}
const clientProject: ClientProjectJson = {
id: project.id,

View File

@ -12,6 +12,7 @@ import {
import { cn } from "@/lib/utils";
import { Label, SpanLabel } from "@/components/ui/label";
import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors";
const Form = FormProvider;
@ -47,7 +48,7 @@ const useFormField = () => {
const fieldState = getFieldState(fieldContext.name, formState);
if (!fieldContext) {
throw new Error("useFormField should be used within <FormField>");
throw new StackAssertionError("useFormField should be used within <FormField>");
}
const { id } = itemContext;