Skip to main content

Advanced Error Logging

Coralite provides a centralised onError configuration callback. By default, it will use console.warn and console.log for non-critical information and throw for ERR level. However, for production pipelines or advanced build tools, you might want to integrate custom loggers or external error tracking systems.

Building a Custom CLI Logger #

You can use tools like chalk to build beautifully styled console outputs for your builds.

javascript
Code copied!
  // coralite.config.js
  import { defineConfig } from 'coralite-scripts';
  import chalk from 'chalk';
  
  export default defineConfig({
    // ... other configs
    onError: ({ level, message, error }) => {
      switch (level) {
        case 'ERR':
          console.error(chalk.red.bold(`[FATAL BUILD ERROR] ${message}`));
          if (error && error.stack) {
              console.error(chalk.red(error.stack));
          }
          // Since we are overriding the default throw, we should manually exit
          process.exit(1); 
        case 'WARN':
          console.warn(chalk.yellow(`[WARNING] ${message}`));
          break;
        case 'LOG':
        default:
          console.log(chalk.blue(`[INFO] ${message}`));
          break;
      }
    }
  });

Integrating Sentry during the Build #

If you're running Coralite as part of a CI/CD pipeline, capturing errors directly to a service like Sentry ensures your DevOps team gets real-time stack traces for build failures.

javascript
Code copied!
  // coralite.config.js
  import { defineConfig } from 'coralite-scripts';
  import * as Sentry from '@sentry/node';
  
  // Initialize Sentry early
  Sentry.init({
    dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
    tracesSampleRate: 1.0,
  });
  
  export default defineConfig({
    // ... other configs
    onError: ({ level, message, error }) => {
      if (level === 'ERR') {
        const sentryError = error || new Error(message);
        
        // Capture the exception in Sentry
        Sentry.captureException(sentryError, {
            tags: { phase: 'build', framework: 'coralite' },
            extra: { originalMessage: message }
        });
        
        // We still want to log to the console for local output
        console.error(`[Build Failed] ${message}`);
        
        // Give Sentry 2 seconds to flush before breaking the build
        Sentry.flush(2000).then(() => {
            process.exit(1);
        });
      } else if (level === 'WARN') {
          // You could use captureMessage for warnings if desired
          // Sentry.captureMessage(message, 'warning');
          console.warn(message);
      } else {
          console.log(message);
      }
    }
  });

Remember that when overriding the 'ERR' behavior, Coralite relies on you to halt the process (e.g., via process.exit(1)) or throw manually if an unrecoverable failure occurs during your custom logic.

Start Building with Coralite!

Use the scaffolding script to get jump started into your next project with Coralite

Copied commandline!