kit icon indicating copy to clipboard operation
kit copied to clipboard

HttpError and Redirect import errors

Open denovodavid opened this issue 3 years ago • 2 comments

Describe the bug

I'm building my own adapter with typescript, and I use tsc -b to build it. Since the new routing change, I get a type error when building (see reproduction).

Also: HttpError and Redirect classes are not available to import, which they probably should be since we indirectly throw them and might want to handle them differently if thrown around with other errors.

See also Rich's comment here: https://github.com/sveltejs/kit/issues/5952#issuecomment-1218844057

Also also, not directly related but still weird typing issue: I had to add @types/estree to dependencies to appease svelte, seems related to: https://github.com/sveltejs/svelte/issues/3397

Reproduction

https://stackblitz.com/edit/node-sfis81?file=index.ts

Logs

No response

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (8) x64 Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz    
    Memory: 1.41 GB / 15.75 GB
  Binaries:
    Node: 16.16.0 - ~\AppData\Local\nvs\default\node.EXE     
    npm: 8.11.0 - ~\AppData\Local\nvs\default\npm.CMD        
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (104.0.1293.54)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @sveltejs/kit: 1.0.0-next.420 => 1.0.0-next.420 
    svelte: ^3.49.0 => 3.49.0 
    vite: ^3.0.8 => 3.0.8

Severity

serious, but I can work around it

Additional Information

No response

denovodavid avatar Aug 18 '22 01:08 denovodavid

For what it's worth, I support exposing these. "You can throw it but you can't catch it" seems kind of like an antipattern. Even better, an export like isRedirect/isHttpError so that we can maintain encapsulation of those classes:

export const isRedirect = (err: unknown): err is Redirect => { /* implementation */ }

My workaround for now (for my purposes):


class Wrap {
	public obj;
	constructor(x) {
		this.obj = x;
	}
}

throw new Wrap(redirect(status, data));
// or 
throw new Wrap(error(500, ""));


...


} catch (e) {
	if (e instanceof Wrap) {
		throw e.obj;
	}
...
}

pjebs avatar Dec 09 '22 22:12 pjebs

Is there any other recommended way to catch an HttpError? A fetch request to my backend API can throw HttpErrors and Redirects. I'd like to handle errors and throw Redirects but I don't see a way to do this as of now.

mike-lloyd03 avatar Oct 01 '23 04:10 mike-lloyd03