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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: falsy infer a http request when there is ok and Error in the return data strucutre #362

Open
LatentDream opened this issue Mar 4, 2024 · 2 comments

Comments

@LatentDream
Copy link

Describe the feature / bug 馃摑:

Bug using Error in the return data strucutre of a promise.
While there is no error raise, the toast take the error branch with the following message HTTP error! status: undefined

Steps to reproduce the bug 馃攣:

promise data structure return

export type Result<T, E = Error> =
  | { ok: true; value: T }
  | { ok: false; error: E };

export function Ok<T, E>(value: T): Result<T, E> {
  return { ok: true, value };
}
export function Err<T, E>(error: E): Result<T, E> {
  return { ok: false, error };
}

Code to reproduce the bug

async function whatWillHappen(): Promise<Result<null, Error>> {
  return Err(new Error("Not implemented"));
}

export function useSaveProject() {
  const handle = async () => {
    toast.promise(whatWillHappen, {
    loading: "Saving project...",
    success: (result) => {
      if (result.ok) {
        return "Project saved";
      } else {
        return `Result Error: ${result.error}`;
      }
    },
    error: (e) => `Error Raise: ${e}`,
    });
  };

  return (handle);
}

This return:
Error Raise: HTTP error! status: undefined

@LatentDream
Copy link
Author

There is a miss understanding. The library infer that all data structure with .ok and .error is a http request:
problem here

@LatentDream LatentDream changed the title Bug using Error in the return data strucutre Bug: falsy infer a http request when there is ok and Error in the return data strucutre Mar 4, 2024
@dclark27
Copy link

dclark27 commented Mar 27, 2024

We catch errors in our backend and return a structured response with an error key to indicate an error. It'd be great to be able to escape hatch by throwing an error and propagating the specific message to the error toast.

toast.promise(response, {
	loading: messages.saveLoading,
	success: (response) => {
		if ('errorKey' in response) {
			const errorMessage = getTranslatedError(response.errorKey, lang);
			throw new Error(errorMessage ?? messages.fallbackErrorMessage);
		} else {
			return selectedEntity === ''
				? messages.successRemovedDescription
				: messages.successDescription;
		}
	},
        // catch error and deconstruct the message
	error: (e) => e.message ?? messages.fallbackErrorMessage
});

Edit: just did a little more digging, pretty easy to achieve this if anyone else stumbles here by using the id returned by the loading toast:

const id = toast.loading(messages.saveLoading);

const response = await request(request, configLocator, lang);

if ('errorKey' in response) {
	const errorMessage = getTranslatedError(response.errorKey, lang);
	toast.error(errorMessage ?? messages.fallbackErrorMessage, { id });
} else {
	toast.success(
		selectedEntity === ''
			? messages.successRemovedDescription
			: messages.successDescription,
		{ id },
	);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants