-
-
Notifications
You must be signed in to change notification settings - Fork 171
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
Strongly Typed Errors #405
Comments
This would be a BIG DX improvement. I am willing to take a stab a this. |
we also have this open here: supabase/supabase#12739 We have a few options:
I'm thinking that |
Oops, I completely missed supabase/supabase#12739. |
Yet another aspect to consider for DX is error handling. With typescript errors in catch are either of type 1. Errors as classesclass SupabaseError extends Error {
constructor(
public override message: string,
public code: string
) {
super(message);
}
}
const dispatchRequest = (): void => {
throw new SupabaseError('The request timed out', 'NETWORK_FAILURE');
} Handle the error: try {
dispatchRequest();
} catch(err: unknown) {
if (err instanceof SupabaseError) {
switch (err.code) {
case 'NETWORK_FAILURE':
// handle network errors
break;
}
}
// re throw unhandled error
throw err;
} 2. Errors as objectsinterface SupabaseError {
code: string
message: string
}
const dispatchRequest = (): void => {
throw { code: 'NETWORK_FAILURE', message: 'The request timed out' } as SupabaseError;
} Handle the error: try {
dispatchRequest();
} catch(err: unknown) {
if (err.code === 'NETWORK_FAILURE') // object is of type unkown
if (typeof err === 'object' && err.code === 'NETWORK_FAILURE') // object is possibly null
if (typeof err === 'object' && err !== null && err.code === 'NETWORK_FAILURE') // property code does not exist on type object
if (typeof err === 'object' && err !== null && 'code' in err && err.code === 'NETWORK_FAILURE') // still: property code does not exist on type object
} Custom Typeguard to the rescue: const isSupabaseError = (target: unknown): target is SupabaseError => {
return (
typeof target === 'object'
&& target !== null
&& 'code' in target
&& 'message' in target
);
}
try {
dispatchRequest();
} catch(err: unknown) {
if (isSupabaseError(err)) {
switch (err.code) {
case 'NETWORK_FAILURE':
// handle network errors
break;
}
}
// re throw unhandled error
throw err;
} Closely looking at |
Hey everyone, we've taken this feedback into consideration and this is now available in the |
Feature request
Is your feature request related to a problem? Please describe.
It is currently impossible to differentiate errors, e.g: when a user signs up there is a multitude of possible errors (network errors, database errors, etc) and although you can easily check if an error occured (
if (res.error !== null) { /**/ }
) it is not possible to know which error happened.Describe the solution you'd like
Enum error types:
Describe alternatives you've considered
I am not aware of any alternative.
The text was updated successfully, but these errors were encountered: