1<?php 2 3namespace ComboStrap; 4 5/** 6 * This class is here to help 7 * create report from an exception (HTML mostly to 8 * be send to the browser) 9 */ 10class ExceptionReporter 11{ 12 13 private \Exception $e; 14 15 public function __construct(\Exception $e) 16 { 17 $this->e = $e; 18 } 19 20 21 public static function createForException(\Exception $e): ExceptionReporter 22 { 23 return new ExceptionReporter($e); 24 } 25 26 public function getExceptionTraceAsHtml() 27 { 28 return str_replace("\n", "<br/>", $this->e->getTraceAsString()); 29 } 30 31 public function getHtmlPage($reporterMessage): string 32 { 33 34 if (Identity::isManager()) { 35 $errorMessage = $this->e->getMessage(); 36 $errorTrace = $this->getExceptionTraceAsHtml(); 37 $errorHtml = <<<EOF 38<br/> 39<p>Error (only seen by manager):</p> 40<p>$errorMessage</p> 41<p>$errorTrace</p> 42EOF; 43 } else { 44 $errorHtml = "<br/><p>The error was logged in the log file. Errors are only visible by managers</p>"; 45 } 46 return <<<EOF 47<html lang="en"> 48<head> 49<title>Error</title> 50</head> 51<body> 52<h1>An error has occurred</h1> 53<p>$reporterMessage</p> 54$errorHtml 55</body> 56EOF; 57 } 58} 59