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:

- [Catch errors](https://docs.cloud.google.com/workflows/docs/reference/syntax/catching-errors)
- [Make an HTTP request](https://docs.cloud.google.com/workflows/docs/http-requests)

## 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](https://docs.cloud.google.com/docs/samples?product=workflows).