Simplify error messages.
This commit is contained in:
parent
1bcd3ad9f0
commit
23cdbe59a0
3 changed files with 18 additions and 25 deletions
10
src/EEPROMErrorHandler.php
Normal file
10
src/EEPROMErrorHandler.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
namespace EEPROM;
|
||||
|
||||
use Index\Http\{HttpErrorHandler,HttpResponseBuilder,HttpRequest};
|
||||
|
||||
class EEPROMErrorHandler implements HttpErrorHandler {
|
||||
public function handle(HttpResponseBuilder $response, HttpRequest $request, int $code, string $message): void {
|
||||
$response->setContent(sprintf('<!doctype html><title>%1$03d %2$s</title><strong>%1$03d %2$s</strong>', $code, $message));
|
||||
}
|
||||
}
|
|
@ -9,7 +9,9 @@ class RoutingContext {
|
|||
private HttpRouter $router;
|
||||
|
||||
public function __construct(private Config $config) {
|
||||
$this->router = new HttpRouter;
|
||||
$this->router = new HttpRouter(
|
||||
errorHandler: new EEPROMErrorHandler,
|
||||
);
|
||||
$this->router->use('/', $this->middleware(...));
|
||||
}
|
||||
|
||||
|
|
|
@ -53,10 +53,8 @@ class UploadsRoutes implements RouteHandler {
|
|||
$uploadId = $this->uploadsCtx->uploads->resolveLegacyId($uploadId) ?? $uploadId;
|
||||
$uploadSecret = null;
|
||||
} else {
|
||||
if(strlen($uploadId) < 5) {
|
||||
$response->setContent('File not found.');
|
||||
if(strlen($uploadId) < 5)
|
||||
return 404;
|
||||
}
|
||||
|
||||
$uploadSecret = substr($uploadId, -4);
|
||||
$uploadId = XNumber::fromBase62(substr($uploadId, 0, -4));
|
||||
|
@ -65,17 +63,14 @@ class UploadsRoutes implements RouteHandler {
|
|||
try {
|
||||
$uploadInfo = $this->uploadsCtx->uploads->getUpload(uploadId: $uploadId, secret: $uploadSecret);
|
||||
} catch(RuntimeException $ex) {
|
||||
$response->setContent('File not found.');
|
||||
return 404;
|
||||
}
|
||||
|
||||
try {
|
||||
$variantInfo = $this->uploadsCtx->uploads->getUploadVariant($uploadInfo, $uploadVariant);
|
||||
} catch(RuntimeException $ex) {
|
||||
if($isOriginal) {
|
||||
$response->setContent('Data is missing.');
|
||||
if($isOriginal)
|
||||
return 404;
|
||||
}
|
||||
|
||||
try {
|
||||
$poolInfo = $this->poolsCtx->pools->getPool($uploadInfo->poolId);
|
||||
|
@ -94,15 +89,12 @@ class UploadsRoutes implements RouteHandler {
|
|||
}
|
||||
}
|
||||
} catch(RuntimeException $ex) {
|
||||
$response->setContent('Original data is missing.');
|
||||
return 404;
|
||||
}
|
||||
}
|
||||
|
||||
if(!isset($variantInfo)) {
|
||||
$response->setContent('Variant not available.');
|
||||
if(!isset($variantInfo))
|
||||
return 404;
|
||||
}
|
||||
|
||||
if($isOriginal)
|
||||
$this->uploadsCtx->uploads->updateUpload(
|
||||
|
@ -114,20 +106,12 @@ class UploadsRoutes implements RouteHandler {
|
|||
try {
|
||||
$storageInfo = $this->storageCtx->records->getFile($variantInfo->fileId);
|
||||
} catch(RuntimeException $ex) {
|
||||
$response->setContent('Data is missing.');
|
||||
return 404;
|
||||
}
|
||||
|
||||
$denyInfo = $this->denylistCtx->getDenylistEntry($storageInfo);
|
||||
if($denyInfo !== null) {
|
||||
$response->setContent(match($denyInfo->reason) {
|
||||
DenylistReason::Copyright => 'File is unavailable for copyright reasons.',
|
||||
DenylistReason::Rules => 'File was in violation of the rules.',
|
||||
default => 'File was removed for reasons beyond understanding.',
|
||||
});
|
||||
|
||||
if($denyInfo !== null)
|
||||
return $denyInfo->isCopyrightTakedown ? 451 : 410;
|
||||
}
|
||||
|
||||
if($isJson)
|
||||
return $this->uploadsCtx->convertToClientJsonV1($uploadInfo, $variantInfo, $storageInfo);
|
||||
|
@ -273,10 +257,8 @@ class UploadsRoutes implements RouteHandler {
|
|||
$uploadId = $this->uploadsCtx->uploads->resolveLegacyId($uploadId) ?? $uploadId;
|
||||
$uploadSecret = null;
|
||||
} else {
|
||||
if(strlen($uploadId) < 5) {
|
||||
$response->setContent('File not found.');
|
||||
if(strlen($uploadId) < 5)
|
||||
return 404;
|
||||
}
|
||||
|
||||
$uploadSecret = substr($uploadId, -4);
|
||||
$uploadId = XNumber::fromBase62(substr($uploadId, 0, -4));
|
||||
|
@ -285,7 +267,6 @@ class UploadsRoutes implements RouteHandler {
|
|||
try {
|
||||
$uploadInfo = $this->uploadsCtx->uploads->getUpload(uploadId: $uploadId, secret: $uploadSecret);
|
||||
} catch(RuntimeException $ex) {
|
||||
$response->setContent('File not found.');
|
||||
return 404;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue