Coder Social home page Coder Social logo

Comments (6)

reinink avatar reinink commented on July 1, 2024 1

I guess we could do something like this:

public function render($request, Exception $exception)
{
    return Inertia::renderErrorResponse(
        string $component,
        Response $response,
        array $codes
    );
}

For example:

public function render($request, Exception $exception)
{
    return Inertia::renderErrorResponse(
        'Error',
        parent::render($request, $exception),
        [500, 503, 404, 403]
    );
}

Again, not sure that this is worth adding to the framework, since it really doesn't require that much code to just do this yourself.

from inertia-laravel.

reinink avatar reinink commented on July 1, 2024

I just updated the docs with a pretty straight forward solution:

use Inertia\Inertia;

public function render($request, Exception $exception)
{
    $response = parent::render($request, $exception);

    if ($request->header('X-Inertia') && in_array($response->status(), [500, 503, 404, 403])) {
        return Inertia::render('Error', ['status' => $response->status()])
            ->toResponse($request)
            ->setStatusCode($response->status());
    }

    return $response;
}

Basically I decided it was an act of futility to try and figure out every possible exception that could get thrown. Instead, I just let Laravel generate the correct Response, and I use that decide whether I want to show an Inertia error page.

A corresponding error page component could look like this:

<template>
  <div>
    <h1>{{ title }}</h1>
    <div>{{ description }}</div>
  </div>
</template>

<script>
export default {
  props: {
    status: Number,
  },
  computed: {
    title() {
      return {
        503: '503: Service Unavailable',
        500: '500: Server Error',
        404: '404: Page Not Found',
        403: '404: Forbidden',
      }[this.status]
    },
    description() {
      return {
        503: 'Sorry, we are doing some maintenance. Please check back soon.',
        500: 'Whoops, something went wrong on our servers.',
        404: 'Sorry, the page you are looking for could not be found.',
        403: 'Sorry, you are forbidden from accessing this page.',
      }[this.status]
    },
  },
}
</script>

This gives developers tons of flexibility to create their own custom designed error pages.

I'm kind of happy with this as a final solution. I'm not sure this library needs to do anything more...although I'm still open to it.

from inertia-laravel.

reinink avatar reinink commented on July 1, 2024

Closing this, because I think the above solution is actually perfect. πŸ‘Œ

from inertia-laravel.

kevnk avatar kevnk commented on July 1, 2024

I've implemented the solution outlined in the docs, but anytime I run into a 403, it's still showing laravel's stubbed 403 page... Any ideas why this might be happening?

from inertia-laravel.

kevnk avatar kevnk commented on July 1, 2024

Never mind... 🀦 I was testing locally so I got rid of the !app()->environment(['local', 'testing']) condition and I can see everything now...

from inertia-laravel.

cristian807 avatar cristian807 commented on July 1, 2024

Hello!

I have gotten an error when taking my app to production in a shared hosting (cpanel).

The error is that when I try to render a view, it does not render as such, but instead displays the data coming from the controller in a modal.

How can I solve this?

Thank you.

from inertia-laravel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.