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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: HttpError imported from @sveltejs/kit is undefined #10267

Closed
ollema opened this issue Jun 27, 2023 · 1 comment
Closed

bug: HttpError imported from @sveltejs/kit is undefined #10267

ollema opened this issue Jun 27, 2023 · 1 comment

Comments

@ollema
Copy link

ollema commented Jun 27, 2023

Describe the bug

Hi!

Not sure if this is a bug - or I missed something in the documentation - but I have the following problem.

I want to fetch some things from an external API in my +page.server.ts. I have some helper functions in $lib/server/ that help me to do that.

In these, if I encounter error in the external API, I throw errors with something along the lines of:

export async function fetch_thing(parameter: string) {
  const response = await fetch(`${base_url}/api/thing/${parameter}`);
  if (!response.ok) {
    const external_error_message = JSON.stringify((await response.json()).detail);
    throw error(
      response.status,
      `could not fetch from external API: ${external_error_message}`
    );
  }
  return thing_schema.parse(await response.json());
}

In my +page.server.ts I then catch these with something along the lines of:

import { HttpError } from '@sveltejs/kit';

...

try {
  await fetch_thing(parameter);
} catch (e) {
  if (e instanceof HttpError && e.status === 422) {
    // do something
  }
}

but that results in:

TypeError: Right-hand side of 'instanceof' is not an object

It turns out that HttpError is undefined!

This is surprising to me:
Screenshot 2023-06-27 at 21 39 42

Is HttpError not meant for consumption so to speak? Am I doing something weird here?

If I'm not supposed to use HttpError then maybe that should be documented somewhere?

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-bmtmun?file=src%2Froutes%2F%2Bpage.server.ts

press Demo

Logs

TypeError: Right-hand side of 'instanceof' is not an object

System Info

System:
    OS: Linux 5.4 Ubuntu 20.04.4 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Xeon(R) Platinum 8268 CPU @ 2.90GHz
    Memory: 25.75 GB / 31.32 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 18.11.0 - ~/.local/share/pnpm/node
    npm: 8.19.2 - ~/.local/share/pnpm/npm
    pnpm: 8.6.3 - ~/.local/share/pnpm/pnpm
  npmPackages:
    @sveltejs/adapter-node: ^1.2.4 => 1.2.4 
    @sveltejs/kit: ^1.20.5 => 1.20.5 
    svelte: ^4.0.0 => 4.0.0 
    vite: ^4.3.9 => 4.3.9 

Severity

annoyance

Additional Information

No response

@gtm-nayan
Copy link
Contributor

Duplicate of #6009

@gtm-nayan gtm-nayan marked this as a duplicate of #6009 Jun 27, 2023
@gtm-nayan gtm-nayan closed this as not planned Won't fix, can't repro, duplicate, stale Jun 27, 2023
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