hooks.server.ts
1 import type { Handle, HandleServerError } from '@sveltejs/kit'; 2 3 export const handle: Handle = async ({ event, resolve }) => { 4 try { 5 const response = await resolve(event); 6 return response; 7 } catch (error) { 8 console.error('Server error:', error); 9 10 // Return a custom error page 11 return new Response(` 12 <!DOCTYPE html> 13 <html lang="es"> 14 <head> 15 <meta charset="UTF-8"> 16 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 17 <title>Error - YaCy UI</title> 18 <style> 19 body { 20 font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; 21 background-color: #f8f9fa; 22 color: #333; 23 display: flex; 24 justify-content: center; 25 align-items: center; 26 height: 100vh; 27 margin: 0; 28 } 29 .container { 30 text-align: center; 31 background-color: white; 32 padding: 3rem; 33 border-radius: 8px; 34 box-shadow: 0 4px 16px rgba(0,0,0,0.1); 35 max-width: 80%; 36 } 37 h1 { color: #e63946; margin-bottom: 1rem; } 38 p { line-height: 1.6; margin: 1rem 0; } 39 .btn { 40 display: inline-block; 41 padding: 0.5rem 1.5rem; 42 background-color: #1d3557; 43 color: white; 44 text-decoration: none; 45 border-radius: 4px; 46 margin-top: 2rem; 47 transition: background-color 0.3s; 48 } 49 .btn:hover { background-color: #457b9d; } 50 </style> 51 </head> 52 <body> 53 <div class="container"> 54 <h1>Error en el servidor</h1> 55 <p>Lo sentimos, ha ocurrido un error mientras procesábamos tu solicitud.</p> 56 <p>Por favor, intenta nuevamente o comunícate con soporte si el problema persiste.</p> 57 <a href="/" class="btn">Volver al inicio</a> 58 </div> 59 </body> 60 </html> 61 `, { 62 status: 500, 63 headers: { 64 'content-type': 'text/html' 65 } 66 }); 67 } 68 } 69 70 export const handleError: HandleServerError = ({ error, event }) => { 71 console.error('Server error:', error); 72 73 // Log the error with additional context 74 const errorMessage = error instanceof Error ? error.message : 'Unknown error'; 75 76 console.error(`Error en ${event.url.pathname}:`, errorMessage); 77 78 return { 79 message: "Se ha producido un error en el servidor. Por favor, intenta más tarde.", 80 code: 'ERROR_SERVIDOR' 81 }; 82 }