Improve error handling in flux API

This commit is contained in:
Adw8 2025-01-30 19:04:26 +05:30
parent 41470fc895
commit 2f7fbac703
4 changed files with 13 additions and 9 deletions

View File

@ -25,18 +25,19 @@ declare global {
// Initialize global quotes service // Initialize global quotes service
(global as any).quotesService = quotesService (global as any).quotesService = quotesService
app.prepare().then(() => { app.prepare().then(async() => {
const server = createServer(async (req, res) => { const server = createServer(async (req, res) => {
const parsedUrl = parse(req.url!, true); const parsedUrl = parse(req.url!, true);
handle(req, res, parsedUrl); handle(req, res, parsedUrl);
}); });
await quotesService.fetchAndCacheQuotes(); // Initial store
server.listen(port, () => { server.listen(port, () => {
console.log(`> Server listening at http://localhost:${port}`); console.log(`> Server listening at http://localhost:${port}`);
}); });
// Initial call and interval setup // Interval setup
quotesService.fetchAndCacheQuotes(); // Initial store setInterval(async () => await quotesService.fetchAndCacheQuotes(), 5 * 60 * 1000); // Update cache every 5 minutes
setInterval(() => quotesService.fetchAndCacheQuotes(), 5 * 60 * 1000); // Update cache every 5 minutes
}); });

View File

@ -91,14 +91,17 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
if (!imageUrl) { if (!imageUrl) {
console.error('No image URL in response:', result) console.error('No image URL in response:', result)
throw new Error('No image URL in response') return NextResponse.json(
{ error: 'No image URL in response: ', result },
{ status: 400 }
)
} }
return NextResponse.json({ imageUrl }) return NextResponse.json({ imageUrl })
} catch (error) { } catch (error) {
console.error('Flux generation error:', error) console.error('Flux generation error:', error)
return NextResponse.json( return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to generate image' }, { error: 'Failed to generate image' },
{ status: 500 } { status: 500 }
) )
} }

View File

@ -4,8 +4,8 @@ import { NextRequest, NextResponse } from 'next/server';
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
try { try {
const amountOfMTM: BN[] = (global as any).quotesService.getMTMAmountsFor1USDC(); const amountOfMTM: BN[] = (global as any).quotesService.getMTMAmountsFor1USDC();
const amountOfMTMAsString = amountOfMTM.map(amount => amount.toString()); const latestMTMAmount = amountOfMTM[amountOfMTM.length - 1].toString();
return NextResponse.json(amountOfMTMAsString); return NextResponse.json(latestMTMAmount);
} catch (error) { } catch (error) {
return NextResponse.json({ error: 'Failed to fetch quotes' }, { status: 500 }); return NextResponse.json({ error: 'Failed to fetch quotes' }, { status: 500 });
} }

View File

@ -26,7 +26,7 @@ const Page: React.FC = (): React.ReactElement => {
const data = await response.json(); const data = await response.json();
// Convert the string back to BN // Convert the string back to BN
const price = new BN(data[data.length - 1]); const price = new BN(data);
setPriceMTMFor1USDC(price); setPriceMTMFor1USDC(price);
} catch (error) { } catch (error) {
console.error('Failed to fetch price:', error); console.error('Failed to fetch price:', error);