Catch errors

Implements 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.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

YAML

  # Use a custom exception handler to catch exceptions and return predefined 
 # error messages; if the exception isn't recognized, the workflow 
 # execution fails and throws the exception returned by the GET request 
 - 
  
 read_item 
 : 
  
 try 
 : 
  
 call 
 : 
  
 http.get 
  
 args 
 : 
  
 url 
 : 
  
 https://example.com/someapi 
  
 auth 
 : 
  
 type 
 : 
  
 OIDC 
  
 result 
 : 
  
 API_response 
  
 except 
 : 
  
 as 
 : 
  
 e 
  
 steps 
 : 
  
 - 
  
 known_errors 
 : 
  
 switch 
 : 
  
 - 
  
 condition 
 : 
  
 ${not("HttpError" in e.tags)} 
  
 next 
 : 
  
 connection_problem 
  
 - 
  
 condition 
 : 
  
 ${e.code == 404} 
  
 next 
 : 
  
 url_not_found 
  
 - 
  
 condition 
 : 
  
 ${e.code == 403} 
  
 next 
 : 
  
 auth_problem 
  
 - 
  
 unhandled_exception 
 : 
  
 raise 
 : 
  
 ${e} 
 - 
  
 url_found 
 : 
  
 return 
 : 
  
 ${API_response.body} 
 - 
  
 connection_problem 
 : 
  
 return 
 : 
  
 "Connection 
  
 problem; 
  
 check 
  
 URL" 
 - 
  
 url_not_found 
 : 
  
 return 
 : 
  
 "Sorry, 
  
 URL 
  
 wasn't 
  
 found" 
 - 
  
 auth_problem 
 : 
  
 return 
 : 
  
 "Authentication 
  
 error" 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: