Skip to content

Instantly share code, notes, and snippets.

@maxgfr
Last active February 17, 2023 17:16
Show Gist options
  • Save maxgfr/c375cfaa107de89b1a33f97a464248a3 to your computer and use it in GitHub Desktop.
Save maxgfr/c375cfaa107de89b1a33f97a464248a3 to your computer and use it in GitHub Desktop.
export async function createUser() {
const {user, error} = await api.createUser();
if(error) {
throw new UserError({
name: "CREATE_USER_ERROR",
message: "Failed to create user",
cause: error
})
}
}
export class ErrorBase<T extends string> extends Error {
name: T;
message: string;
cause: any;
constructor(error: ErrorWithCause<T>) {
super();
this.name = error.name;
this.message = error.message;
this.cause = error.cause;
}
}
interface ErrorWithCause<T> {
name: T;
message: string;
cause: any;
}
class UserError extends ErrorBase<"GET_USER_ERROR" | "CREATE_USER_ERROR"> {}
try {
await createUser();
}
catch(e) {
if(e instanceof UserError && e.name === "CREATE_USER_ERROR") {
toast(e.message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment