Created
January 23, 2024 21:10
-
-
Save kassiogluten/83b6b7ac99a854907cbf3c8bbe2fe792 to your computer and use it in GitHub Desktop.
Valida erros de apis, e retorna em string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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