Skip to content

Instantly share code, notes, and snippets.

@kassiogluten
Created January 23, 2024 21:10
Show Gist options
  • Save kassiogluten/83b6b7ac99a854907cbf3c8bbe2fe792 to your computer and use it in GitHub Desktop.
Save kassiogluten/83b6b7ac99a854907cbf3c8bbe2fe792 to your computer and use it in GitHub Desktop.
Valida erros de apis, e retorna em string
export function handleError(error: unknown): string {
let message: string;
if (error instanceof Error) {
console.log("if");
message = error.message;
} else if (error && typeof error === "object" && "message" in error) {
console.log("if");
message = String(error.message);
} else if (error && typeof error === "object" && "msg" in error) {
console.log("if");
message = String(error.msg);
} else if (typeof error === "string") {
console.log("if");
message = error;
} else {
console.log("if");
message = "Erro desconhecido";
}
console.log("handleError", { error, message });
return message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment