> ## Documentation Index
> Fetch the complete documentation index at: https://inertiajs-vi.tuantq.online/llms.txt
> Use this file to discover all available pages before exploring further.

# Xử lý lỗi

<Warning>Đây là tài liệu Inertia.js v1, phiên bản không còn được duy trì tích cực. Vui lòng tham khảo [tài liệu v3](/v3/getting-started/index).</Warning>

## Môi trường phát triển

Một trong những lợi thế khi làm việc với framework phía máy chủ mạnh là bạn được sử dụng sẵn cơ chế xử lý exception tích hợp. Ví dụ, Laravel đi kèm [Ignition](https://github.com/facade/ignition), một công cụ báo lỗi trực quan hiển thị stack trace được định dạng rõ ràng trong môi trường phát triển local.

Khó khăn nằm ở chỗ khi bạn thực hiện request XHR (như Inertia vẫn làm) và gặp lỗi phía máy chủ, thông thường bạn phải mở tab Network trong devtools của trình duyệt để lần tìm và chẩn đoán vấn đề.

Inertia giải quyết vấn đề này bằng cách hiển thị mọi response không phải Inertia trong một modal. Nhờ vậy, bạn vẫn có trải nghiệm báo lỗi trực quan quen thuộc dù request được thực hiện qua XHR.

## Production

Trong production, bạn nên trả về response lỗi Inertia phù hợp thay vì dựa vào cơ chế báo lỗi qua modal dành cho môi trường phát triển. Để làm vậy, bạn cần cập nhật exception handler mặc định của framework để trả về một trang lỗi tùy chỉnh.

Khi xây dựng ứng dụng Laravel, bạn có thể thực hiện việc này bằng phương thức exception `respond` trong file `bootstrap/app.php` của ứng dụng.

```php theme={null}
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Inertia\Inertia;

->withExceptions(function (Exceptions $exceptions) {
    $exceptions->respond(function (Response $response, Throwable $exception, Request $request) {
        if (! app()->environment(['local', 'testing']) && in_array($response->getStatusCode(), [500, 503, 404, 403])) {
            return Inertia::render('Error', ['status' => $response->getStatusCode()])
                ->toResponse($request)
                ->setStatusCode($response->getStatusCode());
        } elseif ($response->getStatusCode() === 419) {
            return back()->with([
                'message' => 'The page expired, please try again.',
            ]);
        }

        return $response;
    });
})
```

Bạn có thể nhận thấy ví dụ trên trả về page component `Error`. Bạn cần thực sự tạo component này để dùng làm trang lỗi chung cho ứng dụng. Sau đây là một ví dụ component lỗi có thể dùng làm điểm khởi đầu.

<CodeGroup>
  ```vue Vue 2 icon="vuejs" theme={null}
  <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: '403: 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>
  ```

  ```vue Vue 3 icon="vuejs" theme={null}
  <script setup>
  import { computed } from 'vue'

  const props = defineProps({ status: Number })

  const title = computed(() => {
      return {
          503: '503: Service Unavailable',
          500: '500: Server Error',
          404: '404: Page Not Found',
          403: '403: Forbidden',
      }[props.status]
  })

  const description = computed(() => {
      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.',
      }[props.status]
  })
  </script>

  <template>
      <div>
          <H1>{{ title }}</H1>
          <div>{{ description }}</div>
      </div>
  </template>
  ```

  ```jsx React icon="react" theme={null}
  export default function ErrorPage({ status }) {
      const title = {
          503: '503: Service Unavailable',
          500: '500: Server Error',
          404: '404: Page Not Found',
          403: '403: Forbidden',
      }[status]

      const description = {
          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.',
      }[status]

      return (
          <div>
              <H1>{title}</H1>
              <div>{description}</div>
          </div>
      )
  }
  ```

  ```html Svelte icon="s" theme={null}
  <script>
      export let status

      $: title = {
          503: '503: Service Unavailable',
          500: '500: Server Error',
          404: '404: Page Not Found',
          403: '403: Forbidden',
      }[status]

      $: description = {
          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.',
      }[status]
  </script>

  <div>
      <H1>{title}</H1>
      <div>{description}</div>
  </div>
  ```
</CodeGroup>

***

## Tài liệu chính thức

Bài dịch này được đối chiếu từ [tài liệu Inertia.js v1 chính thức](https://inertiajs.com/docs/v1/advanced/error-handling). Nếu có khác biệt do phiên bản hoặc cập nhật mới, hãy ưu tiên tài liệu chính thức làm nguồn tham chiếu.
