Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Hello! I juste wanted to ask if it's possible to get the error types to properly handle error cases when making a request. Basically, I'm making a PUT and the request and response body are perfectly typed, but this is not the case of the error(s). Basically, the error is a union type of all possible and declared "wrong cases" (4**).
I wanted something more like switching on error statuses, and get typed-error messages, narrowed by these statuses.
Would it be "dangerous" to assume those types are safe, or is it better to try each possibility of the error union type, then have a fallback where only raw statusCode and text can help ?

You must be logged in to vote

Replies: 3 comments · 4 replies

Comment options

Hi @Ericlm, did you get more info on this topic?

You must be logged in to vote
1 reply
@Ericlm
Comment options

Not for now 😕. It would be great if the errors could be type-safe, with an "unknown error" of type unknown (for handling 5** or others).

Comment options

+1 for this!

I asked an LLM and it suggested to check for the status and then cast the error type that I know will be raised. Like

import type { components } from '$lib/api/schema'; // Your OpenAPI types

type CSVParsingErrorSchema = components['schemas']['CSVParsingErrorSchema'];
type HTTPValidationError = components['schemas']['HTTPValidationError'];

const {
	data,
	error: apiError,
	response
} = await backendClient.GET("/imports/headers/", {
	headers,
	params: {
		query: {
			file_name: fileName
		}
	}
});

if (apiError) {
	const status = apiError.response.status;

	if (status === 400) {
		const errorData = apiError.response.data as CSVParsingErrorSchema;
		console.error("CSV error", errorData.message);
		// Optionally show in UI
		throw error(400, `Ogiltig CSV: ${errorData.message}`);
	}

	if (status === 422) {
		const errorData = apiError.response.data as HTTPValidationError;
		console.error("Validation error", errorData.detail);
		throw error(422, "Valideringsfel vid uppladdning");
	}

	// Fallback
	console.error("Unexpected API error", apiError);
	throw error(500, "Något gick fel");
}

hope you get the feel of it without knowing the details (or Swedish)

You must be logged in to vote
3 replies
@bluenote10
Comment options

Wait, but isn't error or apiError just the payload obtained from the response?

For me apiError.response is just undefined (and doesn't type check, because it's the payload type).

Does this actually work for anyone or is this just an LLM dream?

@officialankan
Comment options

Wait, but isn't error or apiError just the payload obtained from the response?

For me apiError.response is just undefined (and doesn't type check, because it's the payload type).

Does this actually work for anyone or is this just an LLM dream?

Yes, it is undefined but if I know what API route I am hitting and what status code was returned I can cast the response as a known type:

if (status === 422) {
		const errorData = apiError.response.data as HTTPValidationError;
		console.error("Validation error", errorData.detail);
		throw error(422, "Valideringsfel vid uppladdning");
	}

but this is quite tedious

@bluenote10
Comment options

Well, for me it is undefined at runtime!

I.e., there is really no data at all in my case, and casting would be pointless. From reading the source code I also believe it has to be this way, because what is returned as error is the payload of the response, not the response itself.

Are you by any chance using some kind of middleware that actually changes the semantics of error so that you get the full response?

Comment options

I did some research and I found out the following when using fetch:

There are two possible failure modes: Either the request completes successfully but returns an error status, in this case the promise resolves and the response will have status.ok = false Or if a network error occurs the promise is rejected and an error is thrown, to catch this we should wrap it in a try/catch block.

For more specifity you could create handler that checks the thrown error.cause?.code and for the resolved errors you could check the status or statusText

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.