Effect CommunityEC
Effect Communityโ€ข3y agoโ€ข
88 replies
softgrip

Throwing Unchecked Errors in Sveltekit's Page.Server.ts Load Function

How can I throw an unchecked error from within an effect? I'm trying to do a basic flow within Sveltekit's page.server.ts load function.

Their mechanism for handling 404s is to throw what is essentially a "redirect object". I'm trying to do the same thing, but I'm not sure how to throw an error from within an effect.

I'm missing some piece of the puzzle surrounding how to manage defects.

Ideally, this redirect is "expected" behavior when the user doesn't exist, so I'd like to keep it in the effect, and handle as a caught tag.

works (default way of doing the 404 redirect)

// this is the standard sveltekit 404 redirect
export const load = async () => {
  const user = null;

  if (!user) throw error(404, 'Not found'); // error() is a framework supplied method.  This is it's recommended use.

  return { form : {}}
}


doesnt work

// my attempt at Effect
const getUserFail = (): Effect.Effect<never, UserNotFoundError, User> =>
  Effect.fail(new UserNotFoundError({ message: 'User not found' }));

const loadAndValidate:Effect.Effect<never, never, Promise<Validation... etc>> = pipe(
  getUserFail(),
  Effect.catchTags({
    UserNotFoundError: (err) => Effect.die(error(404, `Not found - ${err.message}`))  // <-- am I able to throw here?
  }),
  Effect.map((user) => superValidate(user, UserSchema)),
);

export const load = async () => {
  const form = await Effect.runPromise(loadAndValidate);
  return { form };
};


I get a pretty unhelpful error message in the vite logs related to TypeError: Cannot read properties of undefined (reading 'trim') when attempting the Effect version on failure.

If I surrouned the Effect.runPromise with a try/catch, I can see the following

{"message":"Not found - User not found"}
    at /src/routes/effect-ts/+page.server.ts:33:55
    at /src/routes/effect-ts/+page.server.ts:32:25
    at /src/routes/effect-ts/+page.server.ts:35:25
    at /src/routes/effect-ts/+page.server.ts:39:46
Was this page helpful?