Skip to content

Allow services to throw an error on non-2XX response #648

@rigtigeEmil

Description

@rigtigeEmil

Description

Migrating from the old library, we've leaned into the exception-throwing to handle errors and generically display an issue whenever an error is thrown. Something like this is what we've used up until now, and has been very nice to use.

export const useApiErrorToast = (error: any, onError?: (error: ApiError) => void) => {
	const toast = useToast();
	if (error instanceof ApiError) {
		onError?.();

		if (error.status >= 500) {
			toast.add({
				title: `${error.status}`,
				description: error.body.message,
				color: 'red',
			});
			return;
		}

		if (error.status >= 400 && error.status < 500) {
			toast.add({
				title: `${error.status}`,
				description: error.body.message,
				color: 'orange',
			});
			return;
		}
	}
	toast.add({
		title: 'Something went wrong',
		color: 'red',
	});
};

The new fetch client does not seem to offer similar functionality, nor is there a (to me) obvious way of achieving something similar, especially since the error response is unknown.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions