🎥 Make videos programmatically with React
Propagate .env error to frontend if OpenAI Key is missing
ASchwad57baf944d76b
diff --git a/packages/template-prompt-to-motion-graphics/src/app/api/generate/route.ts b/packages/template-prompt-to-motion-graphics/src/app/api/generate/route.tsindex 70f60c31f7..8cc053d80a 100644--- a/packages/template-prompt-to-motion-graphics/src/app/api/generate/route.ts+++ b/packages/template-prompt-to-motion-graphics/src/app/api/generate/route.ts@@ -571,9 +571,12 @@ export async function POST(req: Request) {if (!apiKey) {return new Response(- JSON.stringify({ error: "OPENAI_API_KEY environment variable is not set" }),+ JSON.stringify({+ error:+ 'The environment variable "OPENAI_API_KEY" is not set. Add it to your .env file and try again.',+ }),{- status: 500,+ status: 400,headers: { "Content-Type": "application/json" },},);@@ -613,7 +616,7 @@ export async function POST(req: Request) {console.error("Error generating code:", error);return new Response(JSON.stringify({- error: "Failed to generate code. Please check your API key.",+ error: "Something went wrong while trying to reach OpenAI APIs.",}),{ status: 500, headers: { "Content-Type": "application/json" } },);diff --git a/packages/template-prompt-to-motion-graphics/src/app/code-examples/page.tsx b/packages/template-prompt-to-motion-graphics/src/app/code-examples/page.tsxindex f8cb6bed97..47f19ce9d0 100644--- a/packages/template-prompt-to-motion-graphics/src/app/code-examples/page.tsx+++ b/packages/template-prompt-to-motion-graphics/src/app/code-examples/page.tsx@@ -172,6 +172,7 @@ function DemoPageContent() {durationInFrames={selectedExample.durationInFrames}fps={selectedExample.fps}isCompiling={isCompiling}+ isStreaming={false}error={error}/></div>diff --git a/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsx b/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsxindex 28f090109f..5231066f61 100644--- a/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsx+++ b/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsx@@ -35,6 +35,7 @@ function GeneratePageContent() {const [prompt, setPrompt] = useState(initialPrompt);const [hasAutoStarted, setHasAutoStarted] = useState(false);const [hasGeneratedOnce, setHasGeneratedOnce] = useState(false);+ const [apiError, setApiError] = useState<string | null>(null);const { code, Component, error, isCompiling, setCode, compileCode } =useAnimationState(examples[0]?.code || "");@@ -93,6 +94,14 @@ function GeneratePageContent() {const handleStreamingChange = useCallback((streaming: boolean) => {setIsStreaming(streaming);+ // Clear API error when starting a new generation+ if (streaming) {+ setApiError(null);+ }+ }, []);++ const handleApiError = useCallback((errorMessage: string) => {+ setApiError(errorMessage);}, []);// Auto-trigger generation if prompt came from URL@@ -134,7 +143,8 @@ function GeneratePageContent() {fps={fps}isCompiling={isCompiling}isStreaming={isStreaming}- error={error}+ error={apiError || error}+ errorType={apiError ? "api" : "compilation"}/></div>@@ -143,6 +153,7 @@ function GeneratePageContent() {onCodeGenerated={handleCodeChange}onStreamingChange={handleStreamingChange}onStreamPhaseChange={setStreamPhase}+ onError={handleApiError}prompt={prompt}onPromptChange={setPrompt}/>diff --git a/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer.tsx b/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer.tsxindex 12a8816f4f..315445116f 100644--- a/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer.tsx+++ b/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer.tsx@@ -19,12 +19,13 @@ const renderErrorFallback: ErrorFallback = ({ error }) => {};interface AnimationPlayerProps {- Component: React.FC | null;+ Component: React.ComponentType | null;durationInFrames: number;fps: number;isCompiling: boolean;isStreaming: boolean;error: string | null;+ errorType?: "compilation" | "api";}export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({@@ -34,6 +35,7 @@ export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({isCompiling,isStreaming,error,+ errorType = "compilation",}) => {if (isStreaming) {return (@@ -69,6 +71,7 @@ export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({}if (error) {+ const errorTitle = errorType === "api" ? "API Error" : "Compilation Error";return (<div className="flex flex-3 flex-col items-center bg-[#0a0a0a] min-w-0"><div className="w-full max-w-[1200px]">@@ -78,7 +81,7 @@ export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({<div className="w-full aspect-video flex justify-center items-center bg-[#2a1a1a] rounded-lg overflow-hidden shadow-[0_0_60px_rgba(0,0,0,0.5)] border border-[#dc2626]"><div className="text-center max-w-[80%]"><div className="text-[#dc2626] text-base font-semibold mb-2 font-sans">- Compilation Error+ {errorTitle}</div><div className="text-[#f87171] text-sm font-mono whitespace-pre-wrap break-words">{error}diff --git a/packages/template-prompt-to-motion-graphics/src/components/PromptInput.tsx b/packages/template-prompt-to-motion-graphics/src/components/PromptInput.tsxindex 0c490b5a4d..c7d64ad437 100644--- a/packages/template-prompt-to-motion-graphics/src/components/PromptInput.tsx+++ b/packages/template-prompt-to-motion-graphics/src/components/PromptInput.tsx@@ -49,6 +49,7 @@ interface PromptInputProps {onCodeGenerated: (code: string) => void;onStreamingChange?: (isStreaming: boolean) => void;onStreamPhaseChange?: (phase: StreamPhase) => void;+ onError?: (error: string) => void;variant?: "landing" | "editor";prompt?: string;onPromptChange?: (prompt: string) => void;@@ -60,6 +61,7 @@ export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>(onCodeGenerated,onStreamingChange,onStreamPhaseChange,+ onError,variant = "editor",prompt: controlledPrompt,onPromptChange,@@ -89,7 +91,10 @@ export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>(});if (!response.ok) {- throw new Error(`API error: ${response.status}`);+ const errorData = await response.json().catch(() => ({}));+ const errorMessage =+ errorData.error || `API error: ${response.status}`;+ throw new Error(errorMessage);}const reader = response.body?.getReader();@@ -142,6 +147,9 @@ export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>(}} catch (error) {console.error("Error generating code:", error);+ const errorMessage =+ error instanceof Error ? error.message : "An unexpected error occurred";+ onError?.(errorMessage);} finally {setIsLoading(false);onStreamingChange?.(false);