Skip to content

implement a global 'hook' for actions #13474

Open
@turbosheep44

Description

@turbosheep44

Describe the problem

I have some error handling logic which gets reused in practically every action, however there is currently no great way to reuse it.

Most of my actions look something like this:

export const actions = {
	async default(event) {
		const req: MyRequestType = await getFormData(event);
		const client = new RpcClient(event.locals.transport);

                let response;
		try {
			response = await client.createSomething(req);
		} catch (err) {
                        // common error handling here
                        if (err instanceof RpcError) {
                           return fail(400);
                        }

                        // fallback for unknown errors
                        return fail(500);
		}

		redirect(303, `/thing/${response.id}`);
	}
};

I've simplified the handling a bit, but you get the idea - for most requests everything inside the catch block is the same.

Currently, the best way I have come up with to handle this case is to wrap every action with an error handler method.

export const actions = {
	default: errorHandler(async (event) => {
		const req: MyRequestType = await getFormData(event);
		const client = new RpcClient(event.locals.transport);

		const response = await client.createSomething(req);

		redirect(303, `/thing/${response.id}`);
	})
};

function errorHandler<H extends (event: E) => Promise<T>, E, T>(
	handler: H
): (event: E) => Promise<T | ActionFailure> {
	return async (event: E) => {
		try {
			return await handler(event);
		} catch (err) {
			if (isRedirect(err)) throw err;

                        // common error handling here
                        if (err instanceof RpcError) {
                           return fail(400);
                        }

                        // fallback for unknown errors
                        return fail(500);
		}
	};
}

This solution works, but requires me to wrap every action with errorHandler and I'm not sure if i will run into a problem with the types at some point.

Describe the proposed solution

I would like to see something like the handle function in hooks.server.ts which could transform the response from any action. I would put my error handling logic in this handle so it applies to every action without having to remember to wrap everything with errorHandler.

Alternatives considered

Actions already use the handle from hooks.server.ts, however that handle requires that I return a Response so I loose the ability to use fail and progressively enhance forms.

Importance

would make my life easier

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature / enhancementNew feature or requestneeds-decisionNot sure if we want to do this yet, also design work needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions