Relever les erreurs
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Met en œuvre un gestionnaire d'exceptions personnalisé basé sur le code d'état HTTP renvoyé par une requête GET. Le workflow détecte une exception potentielle et renvoie un message d'erreur prédéfini.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[],[],null,["# Catch errors\n\nImplements a custom exception handler based on the HTTP status code returned by a GET request. The workflow catches a potential exception and returns a predefined error message.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Catch errors](/workflows/docs/reference/syntax/catching-errors)\n- [Make an HTTP request](/workflows/docs/http-requests)\n\nCode sample\n-----------\n\n### YAML\n\n # Use a custom exception handler to catch exceptions and return predefined\n # error messages; if the exception isn't recognized, the workflow\n # execution fails and throws the exception returned by the GET request\n - read_item:\n try:\n call: http.get\n args:\n url: https://example.com/someapi\n auth:\n type: OIDC\n result: API_response\n except:\n as: e\n steps:\n - known_errors:\n switch:\n - condition: ${not(\"HttpError\" in e.tags)}\n next: connection_problem\n - condition: ${e.code == 404}\n next: url_not_found\n - condition: ${e.code == 403}\n next: auth_problem\n - unhandled_exception:\n raise: ${e}\n - url_found:\n return: ${API_response.body}\n - connection_problem:\n return: \"Connection problem; check URL\"\n - url_not_found:\n return: \"Sorry, URL wasn't found\"\n - auth_problem:\n return: \"Authentication error\"\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=workflows)."]]