Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SvelteKit: errors from server are treated as success #277

Open
Bubbet opened this issue Oct 24, 2023 · 4 comments
Open

SvelteKit: errors from server are treated as success #277

Bubbet opened this issue Oct 24, 2023 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@Bubbet
Copy link

Bubbet commented Oct 24, 2023

Describe the bug

When you fail(400, {errors: {the shape of yup validator's errors}, values: old_form_data_values}) it doesn't update the page.form, and doesn't call onError so those errors are just lost to the void.
As mentioned in the title, the response does trigger onSuccess.

It does catch actual exceptions in the form action.

Which package/s are you using?

felte (Svelte)

Environment

  • OS: Windows 10
  • Browser: Chrome
  • Version: 1.2.12

To reproduce

Return fail in a sveltekit form action.

Small reproduction example

No response

Screenshots

image
image
image

Additional context

No response

@Bubbet Bubbet added the bug Something isn't working label Oct 24, 2023
@cincarnato
Copy link

I have the same problem. While the error is being fixed, is there any workaround?

@pablo-abc
Copy link
Owner

I've been trying to reproduce this and I can't. But looking at your screenshots more closely, I think your onError function is not transforming the data the server is returning into something Felte understands :o It looks like it is returning the whole response object.

@Bubbet
Copy link
Author

Bubbet commented Nov 10, 2023 via email

@david-brt
Copy link

david-brt commented Nov 21, 2023

I have the same problem. While the error is being fixed, is there any workaround?

Same here. Specifically, I think the problem is as follows:

  • using actions, the SvelteKit docs suggest using their custom fail function
  • fail returns the status as an integer as part of the response body with a key of "status", but will always have an http status of 200
  • onError only gets called if the status is NOT 200

Either I am overlooking something or this is just not being handled right now. In any case, an easy workaround is having a condition inside onSuccess that checks the status key in the response body and handles errors based on that.

Update: I just tested it again with a minimal setup and the error remained. I used the form from the docs but left out the action attribute so the default action gets triggered. My +page.server.ts looks as follows:

import { fail } from '@sveltejs/kit';
import type { Actions } from '@sveltejs/kit';

export const actions: Actions = {
  default: () => {
    const someData = {
      foo: 'hello',
      bar: 'world'
    };
    return fail(400, someData);
  }
};

the returned http status is 200 and the response object looks like the one from @Bubbet:
image

I looked into it a bit more and apparently Felte does infact only look at the http status:

if (response.ok) return response;
throw new FelteSubmitError(
  'An error occurred while submitting the form',
  response
);

However, SvelteKit returns status 200 even on failure. To distinguish both cases, an object with three keys type, status and data gets returned. If type === 'failure', the action failed.

david-brt added a commit to david-brt/miab that referenced this issue Nov 22, 2023
very buggy, see pablo-abc/felte#277 for more info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants