TheEdoRan / next-safe-action

Type safe and validated Server Actions in your Next.js (App Router) project.

Home Page:https://next-safe-action.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FEATURE] [v7] Generic type for `serverError`

Yanick64 opened this issue · comments

Are you using the latest version of this library?

  • I verified that the issue exists in the latest next-safe-action release

Is there an existing issue for this?

  • I have searched the existing issues and found nothing that matches

Suggest an idea

I'm missing a way to return a serverError other than just a string. From my API, for example, I get back a code and message and I would like to get this back in my serverError. This is now only allowed to be of string type, unfortunately.

So I would like to make something like the following possible, where the serverError in my action/useAction would then also be of these different types and not just string.

export const action = createSafeActionClient({
	handleReturnedServerError(e) {
		if (e instanceof FetchError) {
			return {
				code: e.response?._data?.code,
				message: e.response?._data?.message,
				additional: e.response?._data?.additional,
			};
		}

		if (e instanceof CustomOtherError) {
			return {
				instance: e.response?._data?.instance,
				response: e.response?._data?.response,
			}
		}

		return DEFAULT_SERVER_ERROR;
	}
});

This feature will land in v7 stable, and will also be available in next channel shortly, when #93 gets merged.