Getting 'currently unable to handle this request' error when rendering inertia component in Laravel 8 with vue.js

huangapple go评论62阅读模式
英文:

Getting 'currently unable to handle this request' error when rendering inertia component in Laravel 8 with vue.js

问题

在 Laravel 8 中,从 app/Exceptions/Handler.php 渲染惯性组件时出现"当前无法处理此请求"错误。

版本:

  • @inertiajs/vue2 版本: 1.0.3
  • @inertiajs/inertia-laravel 版本: 0.6.9

在尝试实现惯性错误处理时,我遇到了重定向问题,错误输出如下:

> 此页面无法正常工作
> bugfinder.test 目前无法处理此请求。
> HTTP 错误 500

我的处理程序代码如下:

public function render($request, Throwable $e)
{
    $response = parent::render($request, $e);
    if (!app()->environment(['local', 'testing']) && in_array($response->status(), [500, 503, 404, 403])) {
        return Inertia::render('Frontend/Error', ['status' => $response->status()])
            ->toResponse($request->all())
            ->setStatusCode($response->status());
    } elseif ($response->status() === 419) {
        return back()->with([
            'message' => '页面已过期,请重试。',
        ]);
    }
    return $response;
}

有人能提供有关此问题的任何建议吗?提前感谢。

英文:

Getting currently unable to handle this request. error while rendering inertia component from app/Exceptions/Handler.php in Laravel 8

Version:

  • @inertiajs/vue2 version: 1.0.3
  • @inertiajs/inertia-laravel version: 0.6.9

I'm getting redirection issue while trying to implement inertia error handling. I'm getting exact below output.

> This page isn’t working
> bugfinder.test is currently unable to handle this request.
> HTTP ERROR 500

My handler code is look like.

  public function render($request, Throwable $e)
    {
        $response = parent::render($request, $e);
        if (!app()->environment(['local', 'testing']) && in_array($response->status(), [500, 503, 404, 403])) {
            return Inertia::render('Frontend/Error', ['status' => $response->status()])
                ->toResponse($request->all())
                ->setStatusCode($response->status());
        } elseif ($response->status() === 419) {
            return back()->with([
                'message' => 'The page expired, please try again.',
            ]);
        }
        return $response;
    }

Can anyone give any shot about this? Thanks in advance.

答案1

得分: 1

我已经解决了。以下是正确的代码。

public function render($request, Throwable $e)
{
    $response = parent::render($request, $e);
    if (!app()->environment(['local', 'testing']) && in_array($response->status(), [500, 503, 404, 403])) {
        return Inertia::render('Frontend/Error', ['status' => $response->status()])
            ->rootView('frontend.layouts.app');
    } else if ($response->status() === 419) {
        return back()->with([
            'message' => 'The page has expired, please try again.',
        ]);
    }
    return $response;
}
英文:

I have solved it. Here is the correct one.

public function render($request, Throwable $e)
{
    $response = parent::render($request, $e);
    if (!app()->environment(['local', 'testing']) && in_array($response->status(), [500, 503, 404, 403])) {
        return Inertia::render('Frontend/Error', ['status' => $response->status()])
            ->rootView('frontend.layouts.app');
    } else if ($response->status() === 419) {
         return back()->with([
        'message' => 'The page has expired, please try again.',
    ]);
    }
    return $response;
}

答案2

得分: 0

你不能简单地返回一个数组或JSON响应,在Inertiajs中,你必须渲染视图。如果你想要使用axios代替。

return Inertia::render('Users', [
    'users' => $users
]);

问题在这里:return $response;
在这里渲染一个视图。不知何故,你的条件没有得到满足。

英文:

You can't simply return an array or json response , in Inertiajs you have to render view. If you want to user axios instead.

return Inertia::render('Users', [
            'users' => $users
        ]);

Problem is here : return $response;
Render a view here. Some how your condition is not getting true.

答案3

得分: -3

检查 Laravel 日志:第一步是检查 Laravel 日志以获取有关错误的更多信息。默认情况下,Laravel 日志存储在 storage/logs 目录中。查找任何错误消息或堆栈跟踪,以帮助识别问题的原因。

启用调试模式:如果在日志中没有看到详细的错误消息,您可以在 Laravel 应用程序中启用调试模式。打开 .env 文件并将 APP_DEBUG 变量设置为 true。这将在浏览器中直接显示详细的错误消息,可以帮助识别问题。

检查 Inertia::render 方法:确保 Inertia::render 方法被正确调用,指定的视图存在。仔细检查 Frontend/Error 组件的路径和名称,以确保其正确。还要验证组件文件是否位于预期位置。

验证版本:确保已安装了正确版本的 @inertiajs/vue2 和 @inertiajs/inertia-laravel 包。检查是否存在已知的兼容性问题或这些包的必需更新。您可以参考每个包的官方文档或GitHub存储库获取更多信息。

验证响应状态码:检查您的 if 条件中的响应状态码是否准确。确保条件正确评估所需的状态码。如果需要,可以暂时删除条件或修改它们以准确定位问题。

检查应用程序环境:确保在 .env 文件中正确设置了应用程序环境。验证环境检查(app()->environment(['local', 'testing']))是否按预期工作。

英文:

Check the Laravel logs: The first step is to check the Laravel logs to get more information about the error. By default, Laravel logs are stored in the storage/logs directory. Look for any error messages or stack traces that can help identify the cause of the issue.

Enable debug mode: If you're not seeing detailed error messages in the logs, you can enable debug mode in your Laravel application. Open the .env file and set the APP_DEBUG variable to true. This will display detailed error messages directly in the browser, which can help identify the problem.

Check the Inertia::render method: Make sure that the Inertia::render method is being called correctly and that the specified view exists. Double-check the path and name of the Frontend/Error component to ensure it is correct. Also, verify that the component file exists in the expected location.

Verify the versions: Ensure that you have the correct versions of the @inertiajs/vue2 and @inertiajs/inertia-laravel packages installed. Check if there are any known compatibility issues or required updates for these packages. You can refer to the official documentation or GitHub repository of each package for more information.

Verify the response status codes: Check if the response status codes in your if conditions are accurate. Make sure that the conditions are correctly evaluating the desired status codes. If needed, you can temporarily remove the conditions or modify them to pinpoint the issue.

Check the application environment: Ensure that the application environment is correctly set in the .env file. Verify that the environment check (app()->environment(['local', 'testing'])) is working as expected.

huangapple
  • 本文由 发表于 2023年5月29日 01:47:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定