Skip to content

Commit 9cec7fb

Browse files
improved error handling
1 parent cf21a9b commit 9cec7fb

10 files changed

+108
-8
lines changed

Module/ContentsDatabase.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,11 @@ function SetContent($contentPath)
373373
}
374374

375375
$this->openedTime = time();
376-
// 読み込む前に更新日時を取得
377-
$this->modifiedTime = filemtime($filePath);
376+
$this->modifiedTime = @filemtime($filePath); // 読み込む前に更新日時を取得
377+
if($this->modifiedTime === false) {
378+
Debug::LogWarning('Cannot get content modified time. content path: ' . $contentPath );
379+
return false;
380+
}
378381

379382
$text = $this->ReadFile($filePath);
380383
if($text === false){
@@ -526,20 +529,20 @@ static function ReadFile($filePath)
526529
{
527530
if(is_dir($filePath))
528531
{
529-
Debug::LogError("[ReadFile] Fail > Directory'{$filePath}'が読み込まれました.");
532+
Debug::LogWarning("[ReadFile] Fail > Directory'{$filePath}' was given.");
530533
return false;
531534
}
532-
535+
533536
//file読み込み
534-
$fp = fopen($filePath, "r");
537+
$fp = @fopen($filePath, "r");
535538
if($fp === false){
536-
Debug::LogError("[ReadFile] Fail > file'{$filePath}'を開けませんでした.");
539+
Debug::LogWarning("[ReadFile] Fail > cannot open file'{$filePath}'.");
537540
fclose($fp);
538541
return false;
539542
}
540543

541544
if(!flock($fp, LOCK_SH)){
542-
Debug::LogError("[ReadFile] Fail > file'{$filePath}'をロックできませんでした.");
545+
Debug::LogWarning("[ReadFile] Fail > cannot lock file'{$filePath}'.");
543546
fclose($fp);
544547
return false;
545548
}

Module/ContentsViewerUtils.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ function GetDecodedText($content) {
319319
!array_key_exists('textUpdatedTime', $cache->data) ||
320320
($cache->data['textUpdatedTime'] < $content->modifiedTime)
321321
) {
322-
323322
$text = [];
324323
$context = ContentTextParser::CreateContext($content->path);
325324
ContentTextParser::$contentLinks = [];

Module/ErrorHandling.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
namespace ErrorHandling;
3+
4+
require_once dirname(__FILE__) . "/Debug.php";
5+
6+
$_SEVERITY_TO_STRING = [
7+
E_ERROR => 'E_ERROR',
8+
E_WARNING => 'E_WARNING',
9+
E_PARSE => 'E_PARSE',
10+
E_NOTICE => 'E_NOTICE',
11+
E_CORE_ERROR => 'E_CORE_ERROR',
12+
E_CORE_WARNING => 'E_CORE_WARNING',
13+
E_COMPILE_ERROR => 'E_COMPILE_ERROR',
14+
E_COMPILE_WARNING => 'E_COMPILE_WARNING',
15+
E_USER_ERROR => 'E_USER_ERROR',
16+
E_USER_WARNING => 'E_USER_WARNING',
17+
E_USER_NOTICE => 'E_USER_NOTICE',
18+
E_STRICT => 'E_STRICT',
19+
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
20+
E_DEPRECATED => 'E_DEPRECATED',
21+
E_USER_DEPRECATED => 'E_USER_DEPRECATED',
22+
];
23+
24+
function StyledErrorHandler($severity, $message, $file, $line, array $context) {
25+
// error was suppressed with the @-operator
26+
if (0 === error_reporting()) { return false;}
27+
28+
global $_SEVERITY_TO_STRING;
29+
$severityString = $_SEVERITY_TO_STRING[$severity] ?? 'E_UNKNOWN';
30+
31+
OutputDebugLog($severity, $message, $file, $line, $context);
32+
echo "<div style='color: #000; background-color: #ff7777bf; border: 1px solid #dc6363; font-size: 90%; margin: 0 0 .5em; padding: .4em; overflow: hidden; border-radius: 5px;'><b>{$severityString}</b>: {$message} in <b>{$file}</b> on line <b>{$line}</b></div>";
33+
}
34+
35+
function PlainErrorHandler($severity, $message, $file, $line, array $context) {
36+
// error was suppressed with the @-operator
37+
if (0 === error_reporting()) { return false;}
38+
39+
global $_SEVERITY_TO_STRING;
40+
$severityString = $_SEVERITY_TO_STRING[$severity] ?? 'E_UNKNOWN';
41+
42+
OutputDebugLog($severity, $message, $file, $line, $context);
43+
echo "<br><b>{$severityString}</b>: {$message} in <b>{$file}</b> on line <b>{$line}</b><br>";
44+
}
45+
46+
function OutputDebugLog($severity, $message, $file, $line, array $context) {
47+
global $_SEVERITY_TO_STRING;
48+
$severityString = $_SEVERITY_TO_STRING[$severity] ?? 'E_UNKNOWN';
49+
\Debug::LogError("
50+
RuntimeError Occured:
51+
Severity: {$severityString}
52+
Message : {$message}
53+
File : {$file}
54+
Line : {$line}
55+
");
56+
}
57+
58+
function SeverityToString($severity) {
59+
switch($severity)
60+
{
61+
case E_ERROR: return 'E_ERROR';
62+
case E_WARNING: return 'E_WARNING';
63+
case E_PARSE: return 'E_PARSE';
64+
case E_NOTICE: return 'E_NOTICE';
65+
case E_CORE_ERROR: return 'E_CORE_ERROR';
66+
case E_CORE_WARNING: return 'E_CORE_WARNING';
67+
case E_COMPILE_ERROR: return 'E_COMPILE_ERROR';
68+
case E_COMPILE_WARNING: return 'E_COMPILE_WARNING';
69+
case E_USER_ERROR: return 'E_USER_ERROR';
70+
case E_USER_WARNING: return 'E_USER_WARNING';
71+
case E_USER_NOTICE: return 'E_USER_NOTICE';
72+
case E_STRICT: return 'E_STRICT';
73+
case E_RECOVERABLE_ERROR: return 'E_RECOVERABLE_ERROR';
74+
case E_DEPRECATED: return 'E_DEPRECATED';
75+
case E_USER_DEPRECATED: return 'E_USER_DEPRECATED';
76+
}
77+
return 'E_UNKNOWN';
78+
}

Service/contents-database-edit-service.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22
require_once dirname(__FILE__) . "/../Module/Authenticator.php";
3+
require_once dirname(__FILE__) . "/../Module/ErrorHandling.php";
4+
5+
set_error_handler('ErrorHandling\PlainErrorHandler');
36

47
Authenticator::RequireLoginedSession();
58

Service/contents-search-service.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
require_once dirname(__FILE__) . "/../Module/SearchEngine.php";
66
require_once dirname(__FILE__) . "/../Module/ContentsDatabaseManager.php";
77
require_once dirname(__FILE__) . "/../Module/ContentsViewerUtils.php";
8+
require_once dirname(__FILE__) . "/../Module/ErrorHandling.php";
9+
10+
set_error_handler('ErrorHandling\PlainErrorHandler');
811

912
ServiceUtils\RequirePostMethod();
1013

Service/file-management-service.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
require_once dirname(__FILE__) . "/../Module/Debug.php";
44
require_once dirname(__FILE__) . "/../Module/Authenticator.php";
5+
require_once dirname(__FILE__) . "/../Module/ErrorHandling.php";
6+
7+
set_error_handler('ErrorHandling\PlainErrorHandler');
58

69

710
Authenticator::RequireLoginedSession();

Service/mail-service.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
require_once dirname(__FILE__) . "/../CollabCMS.php";
44
require_once dirname(__FILE__) . "/../Module/Debug.php";
5+
require_once dirname(__FILE__) . "/../Module/ErrorHandling.php";
6+
7+
set_error_handler('ErrorHandling\PlainErrorHandler');
58

69
// mb_internal_encoding("utf-8");
710

Service/outlinetext-decode-service.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
require_once dirname(__FILE__) . "/../Module/ContentsDatabaseManager.php";
55
require_once dirname(__FILE__) . "/../Module/OutlineText.php";
66
require_once dirname(__FILE__) . "/../Module/Utils.php";
7+
require_once dirname(__FILE__) . "/../Module/ErrorHandling.php";
8+
9+
set_error_handler('ErrorHandling\PlainErrorHandler');
710

811
if (!isset($_POST['plainText'])) {
912
exit();

Service/related-search-service.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
require_once dirname(__FILE__) . '/../Module/Debug.php';
33
require_once dirname(__FILE__) . '/../Module/ServiceUtils.php';
4+
require_once dirname(__FILE__) . "/../Module/ErrorHandling.php";
5+
6+
set_error_handler('ErrorHandling\PlainErrorHandler');
47

58
ServiceUtils\RequirePostMethod();
69

index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
require_once(MODULE_DIR . '/Authenticator.php');
99
require_once(MODULE_DIR . '/ContentsDatabaseManager.php');
1010
require_once(MODULE_DIR . '/Localization.php');
11+
require_once(MODULE_DIR . '/ErrorHandling.php');
1112

13+
set_error_handler('ErrorHandling\StyledErrorHandler');
1214

1315
// 古いURLのリダイレクト
1416
if (isset($_GET['content'])) {

0 commit comments

Comments
 (0)