reinhardt-utils 0.1.0-rc.22

Utility functions aggregator for Reinhardt
Documentation
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Development error pages with enhanced debugging information
//!
//! Provides detailed error pages during development with stack traces,
//! source code context, and helpful debugging information.

use reinhardt_core::security::xss::escape_html;
use std::backtrace::{Backtrace, BacktraceStatus};
use std::error::Error;
use std::fmt;

/// Development error handler
///
/// Generates detailed error pages with stack traces and debugging information.
/// Should only be enabled in development environments.
pub struct DevelopmentErrorHandler {
	/// Show full stack traces
	show_stack_trace: bool,
	/// Show source code context
	show_source: bool,
	/// Maximum lines of source code to show
	source_context_lines: usize,
}

impl DevelopmentErrorHandler {
	/// Create a new development error handler with default settings
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_utils::staticfiles::DevelopmentErrorHandler;
	///
	/// let handler = DevelopmentErrorHandler::new();
	/// ```
	pub fn new() -> Self {
		Self {
			show_stack_trace: true,
			show_source: true,
			source_context_lines: 5,
		}
	}

	/// Enable or disable stack trace display
	pub fn with_stack_trace(mut self, enable: bool) -> Self {
		self.show_stack_trace = enable;
		self
	}

	/// Enable or disable source code display
	pub fn with_source_context(mut self, enable: bool) -> Self {
		self.show_source = enable;
		self
	}

	/// Set the number of source context lines to show
	pub fn with_context_lines(mut self, lines: usize) -> Self {
		self.source_context_lines = lines;
		self
	}

	/// Format an error into an HTML error page
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_utils::staticfiles::DevelopmentErrorHandler;
	/// use std::io;
	///
	/// let handler = DevelopmentErrorHandler::new();
	/// let error = io::Error::new(io::ErrorKind::NotFound, "File not found");
	/// let html = handler.format_error(&error);
	/// assert!(html.contains("File not found"));
	/// ```
	pub fn format_error(&self, error: &dyn Error) -> String {
		let mut html = String::new();

		html.push_str("<!DOCTYPE html>\n");
		html.push_str("<html>\n");
		html.push_str("<head>\n");
		html.push_str("  <meta charset=\"utf-8\">\n");
		html.push_str("  <title>Development Error</title>\n");
		html.push_str("  <style>\n");
		html.push_str(&self.error_page_styles());
		html.push_str("  </style>\n");
		html.push_str("</head>\n");
		html.push_str("<body>\n");

		html.push_str("  <div class=\"error-container\">\n");
		html.push_str("    <h1>Development Error</h1>\n");

		// Main error message
		html.push_str("    <div class=\"error-message\">\n");
		html.push_str(&format!(
			"      <p><strong>Error:</strong> {}</p>\n",
			escape_html(&error.to_string())
		));
		html.push_str("    </div>\n");

		// Stack trace
		if self.show_stack_trace {
			html.push_str(&self.format_stack_trace(error));
		}

		// Error chain
		if let Some(source) = error.source() {
			html.push_str("    <div class=\"error-chain\">\n");
			html.push_str("      <h2>Caused by:</h2>\n");
			html.push_str("      <ul>\n");

			let mut current = Some(source);
			while let Some(err) = current {
				html.push_str(&format!(
					"        <li>{}</li>\n",
					escape_html(&err.to_string())
				));
				current = err.source();
			}

			html.push_str("      </ul>\n");
			html.push_str("    </div>\n");
		}

		html.push_str("  </div>\n");
		html.push_str("</body>\n");
		html.push_str("</html>\n");

		html
	}

	/// Format a simple error message (plain text)
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_utils::staticfiles::DevelopmentErrorHandler;
	/// use std::io;
	///
	/// let handler = DevelopmentErrorHandler::new();
	/// let error = io::Error::new(io::ErrorKind::NotFound, "File not found");
	/// let text = handler.format_error_text(&error);
	/// assert!(text.contains("Error: File not found"));
	/// ```
	pub fn format_error_text(&self, error: &dyn Error) -> String {
		let mut text = String::new();

		text.push_str("Development Error\n");
		text.push_str("================\n\n");
		text.push_str(&format!("Error: {}\n", error));

		if let Some(source) = error.source() {
			text.push_str("\nCaused by:\n");

			let mut current = Some(source);
			while let Some(err) = current {
				text.push_str(&format!("  - {}\n", err));
				current = err.source();
			}
		}

		text
	}

	fn format_stack_trace(&self, _error: &dyn Error) -> String {
		let mut html = String::new();

		html.push_str("    <div class=\"stack-trace\">\n");
		html.push_str("      <h2>Stack Trace</h2>\n");
		html.push_str("      <pre>\n");

		let backtrace = Backtrace::capture();
		if backtrace.status() == BacktraceStatus::Captured {
			html.push_str(&escape_html(&format!("{}\n", backtrace)));
		} else {
			html.push_str("Stack trace not available (compile with RUST_BACKTRACE=1)\n");
		}

		html.push_str("      </pre>\n");
		html.push_str("    </div>\n");

		html
	}

	fn error_page_styles(&self) -> String {
		r#"
    body {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
      margin: 0;
      padding: 20px;
      background: #f5f5f5;
      color: #333;
    }
    .error-container {
      max-width: 1200px;
      margin: 0 auto;
      background: white;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.1);
      padding: 40px;
    }
    h1 {
      color: #d32f2f;
      margin-top: 0;
      border-bottom: 2px solid #d32f2f;
      padding-bottom: 10px;
    }
    h2 {
      color: #555;
      margin-top: 30px;
      font-size: 1.2em;
    }
    .error-message {
      background: #ffebee;
      border-left: 4px solid #d32f2f;
      padding: 15px;
      margin: 20px 0;
    }
    .error-message p {
      margin: 0;
    }
    .error-chain {
      background: #fff3e0;
      border-left: 4px solid #ff9800;
      padding: 15px;
      margin: 20px 0;
    }
    .error-chain ul {
      margin: 10px 0 0 0;
      padding-left: 20px;
    }
    .error-chain li {
      margin: 5px 0;
    }
    .stack-trace {
      background: #f5f5f5;
      border: 1px solid #ddd;
      border-radius: 4px;
      padding: 15px;
      margin: 20px 0;
    }
    .stack-trace pre {
      margin: 10px 0 0 0;
      overflow-x: auto;
      font-family: "Courier New", monospace;
      font-size: 0.9em;
      line-height: 1.5;
    }
"#
        .to_string()
	}
}

impl Default for DevelopmentErrorHandler {
	fn default() -> Self {
		Self::new()
	}
}

/// Error type for development server operations
#[derive(Debug)]
pub struct DevServerError {
	message: String,
	source: Option<Box<dyn Error + Send + Sync>>,
}

impl DevServerError {
	/// Create a new development server error
	pub fn new(message: impl Into<String>) -> Self {
		Self {
			message: message.into(),
			source: None,
		}
	}

	/// Create an error with a source
	pub fn with_source(
		message: impl Into<String>,
		source: impl Error + Send + Sync + 'static,
	) -> Self {
		Self {
			message: message.into(),
			source: Some(Box::new(source)),
		}
	}
}

impl fmt::Display for DevServerError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "{}", self.message)
	}
}

impl Error for DevServerError {
	fn source(&self) -> Option<&(dyn Error + 'static)> {
		self.source
			.as_ref()
			.map(|e| e.as_ref() as &(dyn Error + 'static))
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use std::io;

	#[test]
	fn test_new_handler() {
		let handler = DevelopmentErrorHandler::new();
		assert!(
			handler.show_stack_trace,
			"DevelopmentErrorHandler::new() should enable stack trace by default"
		);
		assert!(
			handler.show_source,
			"DevelopmentErrorHandler::new() should enable source context by default"
		);
		assert_eq!(
			handler.source_context_lines, 5,
			"DevelopmentErrorHandler::new() should set source_context_lines to 5 by default. Got: {}",
			handler.source_context_lines
		);
	}

	#[test]
	fn test_handler_builder() {
		let handler = DevelopmentErrorHandler::new()
			.with_stack_trace(false)
			.with_source_context(false)
			.with_context_lines(10);

		assert!(
			!handler.show_stack_trace,
			"with_stack_trace(false) should disable stack trace. Got: {}",
			handler.show_stack_trace
		);
		assert!(
			!handler.show_source,
			"with_source_context(false) should disable source context. Got: {}",
			handler.show_source
		);
		assert_eq!(
			handler.source_context_lines, 10,
			"with_context_lines(10) should set source_context_lines to 10. Got: {}",
			handler.source_context_lines
		);
	}

	#[test]
	fn test_format_error_html() {
		let handler = DevelopmentErrorHandler::new();
		let error = io::Error::new(io::ErrorKind::NotFound, "File not found");

		let html = handler.format_error(&error);

		// Verify HTML document structure
		assert!(
			html.starts_with("<!DOCTYPE html>\n"),
			"HTML output should start with DOCTYPE declaration. Got: {}",
			&html[..100.min(html.len())]
		);
		assert!(
			html.ends_with("</html>\n"),
			"HTML output should end with </html> tag. Got last 100 chars: {}",
			&html[html.len().saturating_sub(100)..]
		);

		// Strictly verify existence of important HTML elements
		assert!(
			html.contains("<title>Development Error</title>"),
			"HTML should contain <title> element with 'Development Error'. HTML head section: {}",
			html.split("</head>")
				.next()
				.unwrap_or("")
				.get(..500)
				.unwrap_or("")
		);
		assert!(
			html.contains("<h1>Development Error</h1>"),
			"HTML should contain <h1> element with 'Development Error'. HTML body: {}",
			html.split("<body>")
				.nth(1)
				.and_then(|s| s.get(..500))
				.unwrap_or("")
		);
		assert!(
			html.contains("<div class=\"error-message\">"),
			"HTML should contain error-message div. Error sections found: {:?}",
			html.match_indices("<div")
				.map(|(i, _)| &html[i..i.saturating_add(100).min(html.len())])
				.collect::<Vec<_>>()
		);

		// Accurately verify error message
		assert!(
			html.contains("<strong>Error:</strong> File not found"),
			"HTML should contain error message with 'File not found'. Error message section: {}",
			html.split("<div class=\"error-message\">")
				.nth(1)
				.and_then(|s| s.split("</div>").next())
				.unwrap_or("Error message div not found")
		);
	}

	#[test]
	fn test_format_error_text() {
		let handler = DevelopmentErrorHandler::new();
		let error = io::Error::new(io::ErrorKind::NotFound, "File not found");

		let text = handler.format_error_text(&error);

		// Accurately verify plain text structure
		assert!(
			text.starts_with("Development Error\n"),
			"Text output should start with 'Development Error\\n'. Got: {}",
			text.lines().next().unwrap_or("")
		);
		assert!(
			text.contains("================\n\n"),
			"Text output should contain separator line followed by blank line. Got first 100 chars: {}",
			&text[..100.min(text.len())]
		);

		// Accurately verify error message
		assert!(
			text.contains("Error: File not found\n"),
			"Text output should contain 'Error: File not found\\n'. Lines found: {:?}",
			text.lines().collect::<Vec<_>>()
		);
	}

	#[test]
	fn test_format_with_disabled_stack_trace() {
		let handler = DevelopmentErrorHandler::new().with_stack_trace(false);
		let error = io::Error::new(io::ErrorKind::NotFound, "File not found");

		let html = handler.format_error(&error);

		// Strictly verify that entire stack trace section does not exist
		assert!(
			!html.contains("<div class=\"stack-trace\">"),
			"HTML should NOT contain stack-trace div when stack trace is disabled. Div elements found: {:?}",
			html.match_indices("<div")
				.map(|(i, _)| &html[i..i.saturating_add(50).min(html.len())])
				.collect::<Vec<_>>()
		);
		assert!(
			!html.contains("<h2>Stack Trace</h2>"),
			"HTML should NOT contain 'Stack Trace' heading when disabled. H2 elements found: {:?}",
			html.match_indices("<h2>")
				.map(|(i, _)| &html[i..i.saturating_add(50).min(html.len())])
				.collect::<Vec<_>>()
		);
	}

	#[test]
	fn test_dev_server_error_new() {
		let error = DevServerError::new("Test error");
		assert_eq!(
			error.to_string(),
			"Test error",
			"DevServerError::to_string() should return the error message. Got: {}",
			error
		);
		assert!(
			error.source().is_none(),
			"DevServerError created with new() should not have a source error"
		);
	}

	#[test]
	fn test_dev_server_error_with_source() {
		let source = io::Error::new(io::ErrorKind::NotFound, "File not found");
		let error = DevServerError::with_source("Failed to load file", source);

		assert_eq!(
			error.to_string(),
			"Failed to load file",
			"DevServerError::to_string() should return the error message. Got: {}",
			error
		);
		assert!(
			error.source().is_some(),
			"DevServerError created with with_source() should have a source error"
		);
	}

	#[test]
	fn test_error_page_styles() {
		let handler = DevelopmentErrorHandler::new();
		let styles = handler.error_page_styles();

		// Verify accurate existence of CSS selectors
		assert!(
			styles.contains("body {"),
			"CSS should contain 'body' selector. Selectors found: {:?}",
			styles
				.match_indices('{')
				.filter_map(|(i, _)| {
					styles[..i]
						.rsplit_once('\n')
						.or_else(|| styles[..i].rsplit_once(' '))
						.map(|(_, selector)| selector.trim())
				})
				.collect::<Vec<_>>()
		);
		assert!(
			styles.contains(".error-container {"),
			"CSS should contain '.error-container' selector. Class selectors found: {:?}",
			styles
				.lines()
				.filter(|line| line.trim().starts_with('.'))
				.collect::<Vec<_>>()
		);
		assert!(
			styles.contains(".error-message {"),
			"CSS should contain '.error-message' selector. Class selectors found: {:?}",
			styles
				.lines()
				.filter(|line| line.trim().starts_with('.'))
				.collect::<Vec<_>>()
		);
		assert!(
			styles.contains(".error-chain {"),
			"CSS should contain '.error-chain' selector. Class selectors found: {:?}",
			styles
				.lines()
				.filter(|line| line.trim().starts_with('.'))
				.collect::<Vec<_>>()
		);
		assert!(
			styles.contains(".stack-trace {"),
			"CSS should contain '.stack-trace' selector. Class selectors found: {:?}",
			styles
				.lines()
				.filter(|line| line.trim().starts_with('.'))
				.collect::<Vec<_>>()
		);

		// Verify important style properties
		assert!(
			styles.contains("font-family:"),
			"CSS should contain font-family property. Properties found: {:?}",
			styles
				.lines()
				.filter(|line| line.contains(':'))
				.take(10)
				.collect::<Vec<_>>()
		);
		assert!(
			styles.contains("background:"),
			"CSS should contain background property. Properties found: {:?}",
			styles
				.lines()
				.filter(|line| line.contains(':'))
				.take(10)
				.collect::<Vec<_>>()
		);
	}

	#[test]
	fn test_default() {
		let handler = DevelopmentErrorHandler::default();
		assert!(
			handler.show_stack_trace,
			"DevelopmentErrorHandler::default() should enable stack trace by default"
		);
		assert!(
			handler.show_source,
			"DevelopmentErrorHandler::default() should enable source context by default"
		);
	}

	#[test]
	fn test_format_error_html_escapes_xss() {
		// Arrange
		let handler = DevelopmentErrorHandler::new().with_stack_trace(false);
		let xss_payload = "<script>alert('xss')</script>";
		let error = io::Error::other(xss_payload);

		// Act
		let html = handler.format_error(&error);

		// Assert
		assert!(
			!html.contains(xss_payload),
			"HTML output must not contain unescaped script tags. Found raw payload in: {}",
			html.split("<div class=\"error-message\">")
				.nth(1)
				.and_then(|s| s.split("</div>").next())
				.unwrap_or("error-message div not found")
		);
		assert!(
			html.contains("&lt;script&gt;"),
			"HTML output should contain escaped script tag. Error section: {}",
			html.split("<div class=\"error-message\">")
				.nth(1)
				.and_then(|s| s.split("</div>").next())
				.unwrap_or("error-message div not found")
		);
	}

	#[test]
	fn test_format_error_html_escapes_xss_in_error_chain() {
		// Arrange
		let handler = DevelopmentErrorHandler::new().with_stack_trace(false);
		let xss_payload = "<img src=x onerror=alert(1)>";
		let source = io::Error::other(xss_payload);
		let error = DevServerError::with_source("outer error", source);

		// Act
		let html = handler.format_error(&error);

		// Assert
		assert!(
			!html.contains(xss_payload),
			"HTML output must not contain unescaped XSS payload in error chain"
		);
		assert!(
			html.contains("&lt;img"),
			"HTML should contain escaped img tag in error chain"
		);
	}
}