solidjs / solid-router

A universal router for Solid inspired by Ember and React Router
MIT License
1.11k stars 138 forks source link

`error` is missing from `Submission` #360

Closed urish closed 5 months ago

urish commented 6 months ago

Describe the bug

The README mentions that the Submission type includes an error property:

type Submission<T, U> = {
  input: T;
  result: U;
  error: any;
  pending: boolean
  clear: () => {}
  retry: () => {}
}

const submissions = useSubmissions(action, (input) => filter(input));
const submission = useSubmission(action, (input) => filter(input));

However, looking at the source code, this property is missing (there's also a url property that's absent from the docs):

https://github.com/solidjs/solid-router/blob/f57fe83e988732a32b6ae6b84362ca12246a8583/src/types.ts#L180-L187

I'm not sure whether this is a documentation issue or implementation issue. In any case, I couldn't find a way way to tell if there was an error in my action handler.

Your Example Website or App

-

Steps to Reproduce the Bug or Issue

-

Expected behavior

When calling an action, I expect to have some way to know if the handler threw an error.

Screenshots or Videos

No response

Platform

Irrelevant

Additional context

No response

urish commented 6 months ago

As an interim workaround, I'm currently wrapping all my actions with the following action:


function handleErrors<T, A extends Array<any>>(
  fn: (...args: A) => Promise<T>,
): (...args: A) => Promise<{ data?: T; error?: any }> {
  return async function (...args: A) {
    try {
      return { data: await fn(...args) };
    } catch (error: any) {
      return { error };
    }
  };
}

e.g.:

const someAction = action(
  handleErrors(async (someData: string) => {
     // use fetch() or similar to actually perform the action
  }),
);

Then I can look at result?.error to see if there was an error, and get the actual result from result?.data.

ryansolid commented 6 months ago

Ah. Error was there at one point but it didn't end up making sense in the end. Either the thing fails or it returns something and that can be an error or not. I will update docs.

urish commented 6 months ago

Thanks! If there's no error, then how can one detect that the thing has failed and let the user know?

ryansolid commented 5 months ago

Closed by #363.

Depends on the type of error if it is expected just return whatever Error you want from the action like you were. If it is unexpected we throw at the call site, so either a try catch or ErrorBoundary will do.