Add basic error messages
authorAlexander Ebert <ebert@woltlab.com>
Sun, 5 May 2024 21:05:09 +0000 (23:05 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 8 Jun 2024 10:19:39 +0000 (12:19 +0200)
ts/WoltLabSuite/Core/Component/Attachment/Entry.ts
ts/WoltLabSuite/Core/Component/File/Upload.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Component/Attachment/Entry.js
wcfsetup/install/files/js/WoltLabSuite/Core/Component/File/Upload.js
wcfsetup/install/files/lib/system/endpoint/controller/core/files/PostUpload.class.php
wcfsetup/install/files/lib/system/event/listener/PreloadPhrasesCollectingListener.class.php
wcfsetup/install/files/lib/system/file/processor/FileProcessorPreflightResult.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index 66ecd8a884ff8f5b47a7694fff53b251c75c754d..4c70da94e2c0ee8111c905d959e0c86e59f9d161 100644 (file)
@@ -158,8 +158,18 @@ function fileInitializationFailed(element: HTMLElement, file: WoltlabCoreFileEle
     return;
   }
 
-  // TODO: Add a proper error message, this is for development purposes only.
-  markElementAsErroneous(element, JSON.stringify(file.validationError));
+  let errorMessage: string;
+  switch (file.validationError.param) {
+    case "preflight":
+      errorMessage = getPhrase(`wcf.upload.error.${file.validationError.code}`);
+      break;
+
+    default:
+      errorMessage = "Unrecognized error type: " + JSON.stringify(file.validationError);
+      break;
+  }
+
+  markElementAsErroneous(element, errorMessage);
 }
 
 function markElementAsErroneous(element: HTMLElement, errorMessage: string): void {
index dd4fe9215908a0d22a2cb1a07f5ab0bfa12a287e..b3f0162dfbb9997dfd105b8210bf1b0b90f93039 100644 (file)
@@ -10,6 +10,7 @@ import { generateThumbnails } from "WoltLabSuite/Core/Api/Files/GenerateThumbnai
 import ImageResizer from "WoltLabSuite/Core/Image/Resizer";
 import { AttachmentData } from "../Ckeditor/Attachment";
 import { innerError } from "WoltLabSuite/Core/Dom/Util";
+import { getPhrase } from "WoltLabSuite/Core/Language";
 
 export type CkeditorDropEvent = {
   file: File;
@@ -185,7 +186,7 @@ function validateFile(element: WoltlabCoreFileUploadElement, file: File): boolea
     }
   }
 
-  innerError(element, `TODO: the file extension of '${file.name}' is not allowed`);
+  innerError(element, getPhrase("wcf.upload.error.fileExtensionNotPermitted", { filename: file.name }));
 
   return false;
 }
index 281b43e15e08a00f64b2bd19a5913f45198411a4..0aec122d170e1d83b1638c0335148a66c5b6742a 100644 (file)
@@ -117,8 +117,16 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/FileUtil", "WoltLabSui
         if (file.validationError === undefined) {
             return;
         }
-        // TODO: Add a proper error message, this is for development purposes only.
-        markElementAsErroneous(element, JSON.stringify(file.validationError));
+        let errorMessage;
+        switch (file.validationError.param) {
+            case "preflight":
+                errorMessage = (0, Language_1.getPhrase)(`wcf.upload.error.${file.validationError.code}`);
+                break;
+            default:
+                errorMessage = "Unrecognized error type: " + JSON.stringify(file.validationError);
+                break;
+        }
+        markElementAsErroneous(element, errorMessage);
     }
     function markElementAsErroneous(element, errorMessage) {
         element.classList.add("attachment__item--error");
index 89da9480f7542881cab881802ad5e85bcb6f9070..3bb938236fea1d83edd642041fa3844247d6b1f9 100644 (file)
@@ -1,4 +1,4 @@
-define(["require", "exports", "tslib", "WoltLabSuite/Core/Helper/Selector", "WoltLabSuite/Core/Api/Files/Upload", "WoltLabSuite/Core/Api/Files/Chunk/Chunk", "WoltLabSuite/Core/Api/Files/GenerateThumbnails", "WoltLabSuite/Core/Image/Resizer", "WoltLabSuite/Core/Dom/Util"], function (require, exports, tslib_1, Selector_1, Upload_1, Chunk_1, GenerateThumbnails_1, Resizer_1, Util_1) {
+define(["require", "exports", "tslib", "WoltLabSuite/Core/Helper/Selector", "WoltLabSuite/Core/Api/Files/Upload", "WoltLabSuite/Core/Api/Files/Chunk/Chunk", "WoltLabSuite/Core/Api/Files/GenerateThumbnails", "WoltLabSuite/Core/Image/Resizer", "WoltLabSuite/Core/Dom/Util", "WoltLabSuite/Core/Language"], function (require, exports, tslib_1, Selector_1, Upload_1, Chunk_1, GenerateThumbnails_1, Resizer_1, Util_1, Language_1) {
     "use strict";
     Object.defineProperty(exports, "__esModule", { value: true });
     exports.setup = void 0;
@@ -116,7 +116,7 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/Helper/Selector", "Wol
                 return true;
             }
         }
-        (0, Util_1.innerError)(element, `TODO: the file extension of '${file.name}' is not allowed`);
+        (0, Util_1.innerError)(element, (0, Language_1.getPhrase)("wcf.upload.error.fileExtensionNotPermitted", { filename: file.name }));
         return false;
     }
     function setup() {
index 6a01dde60b2c416983a37e2c65cb4dc16ea8b8f8..bed6010927d2246861493d4079ec90e75a5aff7f 100644 (file)
@@ -10,9 +10,11 @@ use wcf\data\file\temporary\FileTemporaryAction;
 use wcf\http\Helper;
 use wcf\system\endpoint\IController;
 use wcf\system\endpoint\PostRequest;
+use wcf\system\exception\PermissionDeniedException;
 use wcf\system\exception\SystemException;
 use wcf\system\exception\UserInputException;
 use wcf\system\file\processor\FileProcessor;
+use wcf\system\file\processor\FileProcessorPreflightResult;
 use wcf\util\JSON;
 
 #[PostRequest('/core/files/upload')]
@@ -35,7 +37,11 @@ final class PostUpload implements IController
 
         $validationResult = $fileProcessor->acceptUpload($parameters->filename, $parameters->fileSize, $decodedContext);
         if (!$validationResult->ok()) {
-            throw new UserInputException('filename', $validationResult->toString());
+            match ($validationResult) {
+                FileProcessorPreflightResult::InsufficientPermissions => throw new PermissionDeniedException(),
+                FileProcessorPreflightResult::InvalidContext => throw new UserInputException('context', 'invalid'),
+                default => throw new UserInputException('preflight', $validationResult->toString()),
+            };
         }
 
         $numberOfChunks = FileTemporary::getNumberOfChunks($parameters->fileSize);
index 2cfe2aca7617f7a197031a4fbf56aa76c07e1190..8a5625fada29b26037a1427fd598a6857e0372a5 100644 (file)
@@ -149,6 +149,9 @@ final class PreloadPhrasesCollectingListener
 
         $event->preload('wcf.style.changeStyle');
 
+        $event->preload('wcf.upload.error.fileExtensionNotPermitted');
+        $event->preload('wcf.upload.error.fileSizeTooLarge');
+
         $event->preload('wcf.user.activityPoint');
         $event->preload('wcf.user.language');
         $event->preload('wcf.user.panel.settings');
index 55cd38bf5dca1c4c2bcde058ff90f271172c96b6..27645d04fcbffd8066f2ee8ec4a202c6a6fa8ddd 100644 (file)
@@ -36,15 +36,4 @@ enum FileProcessorPreflightResult
             self::Passed => 'passed',
         };
     }
-
-    public function toErrorMessage(): string
-    {
-        if ($this->ok()) {
-            throw new \RuntimeException("Cannot invoke `toErrorMessage()` on a successful result.");
-        }
-
-        $phraseSuffix = $this->toString();
-
-        return WCF::getLanguage()->get("wcf.file.preflight.error.{$phraseSuffix}");
-    }
 }
index e133002bdf40edcc5b3d8fe04208960529b7164c..5b3a865e2c98d8ce69141a1512349fbc966fd2f1 100644 (file)
@@ -5545,6 +5545,9 @@ Benachrichtigungen auf <a href="{link isHtmlEmail=true}{/link}">{PAGE_TITLE|phra
                <item name="wcf.upload.error.noImage"><![CDATA[Die hochgeladene Datei ist kein Bild.]]></item>
                <item name="wcf.upload.error.reachedRemainingLimit"><![CDATA[{if LANGUAGE_USE_INFORMAL_VARIANT}Du hast{else}Sie haben{/if} zu viele Dateien ausgewählt. {if LANGUAGE_USE_INFORMAL_VARIANT}Du kannst{else}Sie können{/if} nur noch {#$maxFiles} weitere Datei{if $maxFiles != 1}en{/if} hochladen.]]></item>
                <item name="wcf.upload.error.uploadFailed"><![CDATA[Beim Hochladen der Datei ist ein unbekannter Fehler aufgetreten.]]></item>
+               
+               <item name="wcf.upload.error.fileExtensionNotPermitted"><![CDATA[Der Dateityp von „{$filename}“ ist unzulässig.]]></item>
+               <item name="wcf.upload.error.fileSizeTooLarge"><![CDATA[Die Datei ist zu groß.]]></item>
        </category>
 </import>
 <delete>
index b662b327ef609d5dfe63746ae2b3fdcb63392a8b..d5c418662b2daba8e40b58ba77913ddd9661320a 100644 (file)
@@ -5547,6 +5547,9 @@ your notifications on <a href="{link isHtmlEmail=true}{/link}">{PAGE_TITLE|phras
                <item name="wcf.upload.error.noImage"><![CDATA[The uploaded file is no image.]]></item>
                <item name="wcf.upload.error.reachedRemainingLimit"><![CDATA[You have selected too many files. You can only upload {#$maxFiles} more file{if $maxFiles != 1}s{/if}.]]></item>
                <item name="wcf.upload.error.uploadFailed"><![CDATA[An unknown error occurred during the upload.]]></item>
+
+               <item name="wcf.upload.error.invalidFileExtension"><![CDATA[The file type of “{$filename}” is not permitted.]]></item>
+               <item name="wcf.upload.error.fileSizeTooLarge"><![CDATA[The file is too large.]]></item>
        </category>
 </import>
 <delete>