Now.js Framework Documentation

Now.js Framework Documentation

ErrorManager

EN 15 Dec 2025 06:45

ErrorManager

Overview

ErrorManager is the centralized error management system in Now.js Framework. It supports logging, notifications, and event dispatching.

When to use:

  • Need centralized error handling
  • Need consistent error logging
  • Need to notify users on errors
  • Need to dispatch events on errors

Why use it:

  • ✅ Centralized error handling
  • ✅ Consistent logging format
  • ✅ User notifications
  • ✅ Event dispatching
  • ✅ Debug mode support
  • ✅ Error normalization

Basic Usage

Initialization

ErrorManager.init({
  debug: true,
  notificationDuration: 5000
});

Handle Error

try {
  await riskyOperation();
} catch (error) {
  ErrorManager.handle(error, {
    context: 'DataService.save',
    notify: true,
    logLevel: 'error'
  });
}

Create Custom Error

const error = ErrorManager.createError(
  'User {0} not found in {1}',
  ['john', 'database']
);
throw error;
// Error: "User john not found in database"

Handle Options

ErrorManager.handle(error, {
  // Context - where error occurred
  context: 'ComponentName.methodName',

  // Notify user via NotificationManager
  notify: true,

  // Log level: 'log', 'info', 'warn', 'error'
  logLevel: 'error',

  // Additional data
  data: {
    userId: 123,
    action: 'save'
  },

  // Event type to emit
  type: 'error:custom',

  // Force debug output
  debug: true
});

Configuration

ErrorManager.init({
  // Show stack traces in console
  debug: true,

  // Duration for notification display (ms)
  notificationDuration: 5000
});

API Reference

ErrorManager.init(options)

Initialize ErrorManager

Parameter Type Description
options.debug boolean Enable debug mode
options.notificationDuration number Notification duration (ms)

ErrorManager.handle(error, options)

Handle error

Parameter Type Description
error Error/string Error object or message
options.context string Context where error occurred
options.notify boolean Show notification
options.logLevel string 'log', 'info', 'warn', 'error'
options.data object Additional data
options.type string Event type to emit

Returns: Error - Normalized error object

ErrorManager.createError(message, params)

Create error with parameter substitution

Parameter Type Description
message string Error message with {0}, {1} placeholders
params array Parameters for substitution

Returns: Error

const error = ErrorManager.createError(
  'Cannot find {0} in {1}',
  ['item', 'collection']
);
// Error: "Cannot find item in collection"

ErrorManager.normalizeError(error)

Normalize error to Error object

Parameter Type Description
error any Error, string, or other

Returns: Error

Events

When specifying type in options:

ErrorManager.handle(error, {
  type: 'error:api'
});

// Listen
EventManager.on('error:api', (data) => {
  console.log('API Error:', data.message);
  console.log('Context:', data.context);
});

Real-World Examples

API Error Handling

async function fetchData(url) {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`HTTP ${response.status}`);
    }
    return await response.json();
  } catch (error) {
    ErrorManager.handle(error, {
      context: 'fetchData',
      notify: true,
      type: 'error:fetch',
      data: { url }
    });
    return null;
  }
}

Form Validation Error

function validateForm(data) {
  const errors = [];

  if (!data.email) {
    errors.push('Email is required');
  }

  if (!data.password) {
    errors.push('Password is required');
  }

  if (errors.length > 0) {
    ErrorManager.handle(errors.join(', '), {
      context: 'FormValidation',
      notify: true,
      logLevel: 'warn'
    });
    return false;
  }

  return true;
}

Global Error Handler

// Catch unhandled errors
window.addEventListener('error', (event) => {
  ErrorManager.handle(event.error, {
    context: 'GlobalError',
    notify: true,
    type: 'error:global'
  });
});

// Catch unhandled promise rejections
window.addEventListener('unhandledrejection', (event) => {
  ErrorManager.handle(event.reason, {
    context: 'UnhandledPromise',
    notify: true,
    type: 'error:unhandled'
  });
});

Common Pitfalls

⚠️ 1. Disable Debug in Production

// ❌ Debug enabled in production
ErrorManager.init({ debug: true });

// ✅ Disable in production
ErrorManager.init({
  debug: process.env.NODE_ENV !== 'production'
});

⚠️ 2. Don't Expose Sensitive Data

// ❌ Log sensitive data
ErrorManager.handle(error, {
  data: { password: user.password }
});

// ✅ Log safe data only
ErrorManager.handle(error, {
  data: { userId: user.id }
});