Skip to content

[Question] How to handle errors/merge props on Optional/WhenVisible? #2632

@iquad

Description

@iquad

Inertia adapter(s) affected

  • React
  • Vue 3
  • Svelte
  • Not Applicable

JS package version

2.0.0

Backend stack (optional)

Laravel 12.29
PHP 8.3.26

Describe the problem

While using whenVisible and optional, it's not clear how to handle errors. While using session() and adding Inertia::always on middleware, putting session values inside optional callback are not accessible until next request, not that same request. Inertia::merge(['errors' => 'foo bar']) or Inertia::deepMerge not working either. onError not handling 4xx/5xx errors, so it's not possible to catch on client side either.

What is the proper way to handle errors on whenVisible/optional?

Steps to reproduce

App\Http\Controllers\UserController:

public function detail(Request $request)
{
    return Inertia::render('User/Detail, [
        'id' => $user->id,
        'other' => Inertia::optional(fn () => ExternalService::getUserData($user->id)),
        'withCatch' => Inertia::optional(function () use ($user) {
            try {
                return ExternalService::getUserData($user->id);
            } catch (\Throwable $th) {
                // how to merge error to response?
                // tried session()->put(), not accessible for next request
                // Inertia::merge or Inertia::mergeDeep not working either.
           }
        }),
    ]);
}

resources/Pages/User/Detail.vue:

<WhenVisible
            v-if="missingKeys.length > 0"
            :params="{
              data: {
                test: 'true',
              },
              preserveUrl: true,
              only: missingKeys,
              onError: (error) => {
                console.log('teeeeest'); // not being triggered on 5xx/4xx errors.
              },
              onStart: () => {
                console.log('im started');
              },
              onFinish: () => {
                console.log('im finished');
              },
            }"
          >

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions