Skip to content

Commit

Permalink
If LegacyIdException is thrown provide proper solution (#2291)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Feb 10, 2024
1 parent b9e9231 commit 57e3503
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Exceptions\Handlers\AccessDBDenied;
use App\Exceptions\Handlers\AdminSetterHandler;
use App\Exceptions\Handlers\InstallationHandler;
use App\Exceptions\Handlers\LegacyIdExceptionHandler;
use App\Exceptions\Handlers\MigrationHandler;
use App\Exceptions\Handlers\NoEncryptionKey;
use App\Exceptions\Handlers\ViteManifestNotFoundHandler;
Expand Down Expand Up @@ -108,6 +109,7 @@ class Handler extends ExceptionHandler
AdminSetterHandler::class,
MigrationHandler::class,
ViteManifestNotFoundHandler::class,
LegacyIdExceptionHandler::class,
];

/** @var array<int,class-string<\Throwable>> */
Expand Down
47 changes: 47 additions & 0 deletions app/Exceptions/Handlers/LegacyIdExceptionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Exceptions\Handlers;

use App\Contracts\Exceptions\Handlers\HttpExceptionHandler;
use App\Exceptions\ModelDBException;
use Illuminate\Database\QueryException;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as HttpException;

/**
* Class LegacyIdExceptionHandler.
*
* If SQLSTATE: Numeric value out of range: 1264 is throw it means we are interacting with 32 bit DB.
* Advise to set config parameter.
*/
class LegacyIdExceptionHandler implements HttpExceptionHandler
{
/**
* {@inheritDoc}
*/
public function check(HttpException $e): bool
{
do {
if (
($e instanceof QueryException || $e instanceof ModelDBException) &&
str_contains($e->getMessage(), 'Numeric value out of range: 1264')
) {
return true;
}
} while ($e = $e->getPrevious());

return false;
}

/**
* {@inheritDoc}
*/
public function renderHttpException(SymfonyResponse $defaultResponse, HttpException $e): SymfonyResponse
{
return response()->view('error.error', [
'code' => $e->getStatusCode(),
'type' => class_basename($e),
'message' => 'SQLSTATE: Numeric value out of range: 1264 for column \'legacy_id\'. To fix, please set `force_32bit_ids` to `1` in your Settings => More.',
], $e->getStatusCode(), $e->getHeaders());
}
}

0 comments on commit 57e3503

Please sign in to comment.