All files / types errors.ts

100% Statements 35/35
94.44% Branches 17/18
100% Functions 7/7
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250                                                                                                                                                  12x                                           11x                                         3x                                       4x                                         3x                                               3x                                     9x         9x   3x 3x   1x 1x 1x 1x   1x 1x 1x   2x 2x 1x   2x   1x 1x 1x 1x   1x 1x 1x 1x       9x 1x       9x 9x 9x     9x    
/**
 * Error types for the Resume Converter application
 * All errors include context information and stack trace for debugging
 * Validates: Requirements 8.1
 */
 
export interface BaseError {
  readonly message: string;
  readonly context: Record<string, unknown>;
  readonly stack: string | undefined;
  readonly timestamp: string;
}
 
export interface ParseError extends BaseError {
  readonly type: 'parse';
  readonly line: number;
  readonly column: number;
  readonly source: string;
}
 
export interface ValidationError extends BaseError {
  readonly type: 'validation';
  readonly field: string;
  readonly expected: string;
  readonly received: string;
}
 
export interface TransformError extends BaseError {
  readonly type: 'transform';
  readonly section: string;
  readonly operation: string;
}
 
export interface OutputError extends BaseError {
  readonly type: 'output';
  readonly format: 'pdf' | 'html';
  readonly cause: Error | undefined;
}
 
export interface ConfigError extends BaseError {
  readonly type: 'config';
  readonly path: string;
  readonly expected: string;
  readonly received: string;
}
 
export interface LintError extends BaseError {
  readonly type: 'lint';
  readonly file: string;
  readonly line: number;
  readonly column: number;
  readonly ruleId: string;
  readonly severity: 'error' | 'warning';
}
 
export type ResumeConverterError =
  | ParseError
  | ValidationError
  | TransformError
  | OutputError
  | ConfigError
  | LintError;
 
/**
 * Creates a ParseError with full context
 */
export function createParseError(
  message: string,
  line: number,
  column: number,
  source: string,
  context: Record<string, unknown> = {},
): ParseError {
  return {
    type: 'parse',
    message,
    line,
    column,
    source,
    context,
    stack: new Error().stack,
    timestamp: new Date().toISOString(),
  };
}
 
/**
 * Creates a ValidationError with full context
 */
export function createValidationError(
  message: string,
  field: string,
  expected: string,
  received: string,
  context: Record<string, unknown> = {},
): ValidationError {
  return {
    type: 'validation',
    message,
    field,
    expected,
    received,
    context,
    stack: new Error().stack,
    timestamp: new Date().toISOString(),
  };
}
 
/**
 * Creates a TransformError with full context
 */
export function createTransformError(
  message: string,
  section: string,
  operation: string,
  context: Record<string, unknown> = {},
): TransformError {
  return {
    type: 'transform',
    message,
    section,
    operation,
    context,
    stack: new Error().stack,
    timestamp: new Date().toISOString(),
  };
}
 
/**
 * Creates an OutputError with full context
 */
export function createOutputError(
  message: string,
  format: 'pdf' | 'html',
  cause?: Error,
  context: Record<string, unknown> = {},
): OutputError {
  return {
    type: 'output',
    message,
    format,
    cause,
    context,
    stack: new Error().stack,
    timestamp: new Date().toISOString(),
  };
}
 
/**
 * Creates a ConfigError with full context
 */
export function createConfigError(
  message: string,
  path: string,
  expected: string,
  received: string,
  context: Record<string, unknown> = {},
): ConfigError {
  return {
    type: 'config',
    message,
    path,
    expected,
    received,
    context,
    stack: new Error().stack,
    timestamp: new Date().toISOString(),
  };
}
 
/**
 * Creates a LintError with full context
 */
export function createLintError(
  message: string,
  file: string,
  line: number,
  column: number,
  ruleId: string,
  severity: 'error' | 'warning',
  context: Record<string, unknown> = {},
): LintError {
  return {
    type: 'lint',
    message,
    file,
    line,
    column,
    ruleId,
    severity,
    context,
    stack: new Error().stack,
    timestamp: new Date().toISOString(),
  };
}
 
/**
 * Formats an error for logging with full context and stack trace
 * Validates: Requirements 8.1
 */
export function formatErrorForLogging(error: ResumeConverterError): string {
  const lines: string[] = [
    `[${error.timestamp}] ${error.type.toUpperCase()} ERROR: ${error.message}`,
  ];
 
  // Add type-specific details
  switch (error.type) {
    case 'parse':
      lines.push(`  Location: ${error.source}:${error.line}:${error.column}`);
      break;
    case 'validation':
      lines.push(`  Field: ${error.field}`);
      lines.push(`  Expected: ${error.expected}`);
      lines.push(`  Received: ${error.received}`);
      break;
    case 'transform':
      lines.push(`  Section: ${error.section}`);
      lines.push(`  Operation: ${error.operation}`);
      break;
    case 'output':
      lines.push(`  Format: ${error.format}`);
      if (error.cause) {
        lines.push(`  Cause: ${error.cause.message}`);
      }
      break;
    case 'config':
      lines.push(`  Path: ${error.path}`);
      lines.push(`  Expected: ${error.expected}`);
      lines.push(`  Received: ${error.received}`);
      break;
    case 'lint':
      lines.push(`  File: ${error.file}:${error.line}:${error.column}`);
      lines.push(`  Rule: ${error.ruleId}`);
      lines.push(`  Severity: ${error.severity}`);
      break;
  }
 
  // Add context if present
  if (Object.keys(error.context).length > 0) {
    lines.push(`  Context: ${JSON.stringify(error.context)}`);
  }
 
  // Add stack trace
  Eif (error.stack != null) {
    lines.push(`  Stack trace:`);
    lines.push(error.stack.split('\n').slice(1).join('\n'));
  }
 
  return lines.join('\n');
}