From 2a18baee7e9586ff81550e86b3a93040736c6df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 11 Oct 2021 16:06:23 +0200 Subject: [PATCH] Fix Inco reader mail handler --- app/Jobs/XmlJobs.php | 83 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/app/Jobs/XmlJobs.php b/app/Jobs/XmlJobs.php index 02f6888c..4e9f782f 100644 --- a/app/Jobs/XmlJobs.php +++ b/app/Jobs/XmlJobs.php @@ -316,14 +316,91 @@ abstract class XmlJobs extends XArcadiaFlux protected function sendErrorParseEmailToServiceWeb(Throwable $e) { - Mail::send('emails.exception', ['exception' => $e], function ($m) { + Mail::send([ + 'html' => 'exception-handler::emails.exception', + 'text' => 'exception-handler::emails.exception-plain' + ], [ + 'appName' => config('app.name'), + 'code' => $e->getCode(), + 'line' => $e->getLine(), + 'emessage' => $this->getAnonymizedMessage($e->getMessage()), + 'file' => str_replace(\realpath(\sprintf('%s/../', \app_path())), '', $e->getFile()), + 'stackTrace' => $this->getAnonymizedStackTrace($e), + 'uri' => app()->runningInConsole() ? + 'CONSOLE' : ( + (request() and request()->fullUrl()) ? + request()->fullUrl() : 'none') + ], function ($m) { + /** @var \Illuminate\Mail\Message $m */ $m->to( config('mail.contactsList.serviceweb'), - 'Le Gaulois Exception Handler' - )->subject('Could not eat INCO, because of its toxicity !'); + sprintf('%s Exception Handler', config('app.name')) + )->subject('Need help !, an error occured in your application, please check Logs ASAP'); }); } + /** + * Anonymize at least Sql queries + * + * @param string $message + * @return string + */ + private function getAnonymizedMessage(string $message): string + { + $message = \preg_replace('/^(.*)( \(SQL:.*\))$/', '$1', $message) ?? $message; + return $message; + } + + /** + * Anonymize stack trace + * + * @param \Exception $e + * @return string + */ + private function getAnonymizedStackTrace(Throwable $e): string + { + $projectPath = \realpath(\sprintf('%s/../', \app_path())); + $traceStack = $e->getTrace(); + $traceStr = ''; + $maxStackStage = 0; + foreach ($traceStack as $stackStage => $stack) { + $maxStackStage = $stackStage; + $traceStr .= \sprintf( + '#%s %s(%d): ', + $stackStage, + str_replace($projectPath, '', $stack['file']), + $stack['line'] + ); + $traceStr .= "{$stack['class']}->{$stack['function']}("; + $traceArgs = ''; + if (isset($stack['args'])) { + $argsCount = count($stack['args']); + for ($i = 0; $i < $argsCount; $i++) { + $type = \gettype($stack['args'][$i]); + switch ($type) { + case 'object': + $traceArgs .= sprintf('Object(%s)', \get_class($stack['args'][$i])); + break; + case 'array': + $traceArgs .= sprintf('Array(%d)', \count($stack['args'][$i])); + break; + case 'string': + $traceArgs .= sprintf('String(%d)', \strlen($stack['args'][$i])); + break; + default: + $traceArgs .= \gettype($stack['args'][$i]); + } + $traceArgs .= ($i !== ($argsCount - 1) ? ',' : ''); + } + $traceStr .= $traceArgs; + } + $traceStr .= ")\n"; + } + $maxStackStage++; + $traceStr .= "#$maxStackStage {main}"; + return $traceStr; + } + protected function sendInvalidXmlEmail(string $filename, Throwable $e) { $message = $e->getPrevious()->getMessage();