Skip to content

Commit

Permalink
Add basic error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dtdesign committed May 5, 2024
1 parent 330d683 commit cf380da
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 19 deletions.
14 changes: 12 additions & 2 deletions ts/WoltLabSuite/Core/Component/Attachment/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion ts/WoltLabSuite/Core/Component/File/Upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
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')]
Expand All @@ -35,7 +37,11 @@ public function __invoke(ServerRequestInterface $request, array $variables): Res

$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public function __invoke(PreloadPhrasesCollecting $event): void

$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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,4 @@ public function toString(): string
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}");
}
}
3 changes: 3 additions & 0 deletions wcfsetup/install/lang/de.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5543,6 +5543,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>
Expand Down
3 changes: 3 additions & 0 deletions wcfsetup/install/lang/en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5545,6 +5545,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>
Expand Down

0 comments on commit cf380da

Please sign in to comment.