laravel-efficient-uuid
laravel-efficient-uuid copied to clipboard
`Malformed UTF-8 characters` error when a QueryException is thrown
I've faced the same problem described here and here (but that package has been abandoned).
The problem occurs because the exception can't be formatted to normal JSON data since it contains binary. The problem can be solved by doing these changes in illuminate\database\QueryException.php:
$this->message = $this->formatMessage($sql, $bindings, $previous);
To:
$this->message = utf8_encode($this->formatMessage($sql, $bindings, $previous));
But it's defiantly not a good idea. Any idea about solving it?
Have had this issue myself, too.
Can't think of a good enough reason to propose changing queryexception at the framework end.
Custom exception handler? Seems like quite a jump for what isn't an especially common issue.
Hi there, I'm having this issue too. What I did to have the original error message in the JSON response is this:
In app/Exceptions/Handler.php
public function render($request, Throwable $exception)
{
if ($exception instanceof QueryException && $request->isJson()) {
$message = mb_convert_encoding($exception->getMessage(), 'ASCII');
throw new Exception($message);
}
return parent::render($request, $exception);
}
This is working quite well as a workaround.
Just another workaround:
In app/Exceptions/Handler.php override prepareJsonResponse method
protected function prepareJsonResponse($request, Throwable $e)
{
$exceptionArray = $this->convertExceptionToArray($e);
$exceptionArray['message'] = mb_convert_encoding($exceptionArray['message'], 'ASCII');
return new JsonResponse(
$exceptionArray,
$this->isHttpException($e) ? $e->getStatusCode() : 500,
$this->isHttpException($e) ? $e->getHeaders() : [],
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
);
}
A solution to this issue could be:
-
Create a custom Hander extending
Illuminate\Foundation\Exceptions\Handlerinside this package with theprepareJsonResponsemethod suggested above; -
Bind it to
Illuminate\Contracts\Debug\ExceptionHandlerin the package service provider, using a class defined on config file;
// src/LaravelEfficientUuidServiceProvider.php
public function boot()
{
$this->app->bind(ExceptionHandler::class, config('dyryndadb.error_handler'));
}
- Provide a publishable config file so the developer can use their own implementation if it conflicts with any other already existing error handler customization.
return [
...
'error_handler' => Dyrynda\Database\Exceptions\Handler::class,
];
@michaeldyrynda and others, would something like this be a welcomed PR or do you think this is too much aggressive.
I'd much rather document this and provide one concrete way to address it.
Providing an exception handler is tricky because users of the package possibly won't be able to just use it due to modification of their own app's handler.
Hi there, I'm having this issue too. What I did to have the original error message in the JSON response is this:
In
app/Exceptions/Handler.phppublic function render($request, Throwable $exception) { if ($exception instanceof QueryException) { $message = mb_convert_encoding($exception->getMessage(), 'ASCII'); throw new Exception($message); } return parent::render($request, $exception); }This is working quite well as a workaround.
Worked for me too. But I needed to remove $request->isJson() on if.